2 years ago
#11348
AssSoonAsPossiblee
unexpected situation while reading arguments from telnet
Explanation:
I want to send a message from telnet and do something to the server according to this message, but unexpected situations occur when comparing the message from telnet.
I want the server to be stopped when a "quit" message is sent from telnet, but when I print the incoming message to the screen, the strcmp() function does not perform its function, even though message equals "quit".
Code:
#include<stdio.h>
#include<string.h> // for strlen
#include<sys/socket.h>
#include<arpa/inet.h> // for inet_addr
#include<unistd.h> // for write
int main(int argc, char *argv[])
{
int socket_desc , new_socket , c;
struct sockaddr_in server , client;
char message[1000];
char received_message[1000];
// Create socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1)
{
puts("Could not create socket");
return 1;
}
server.sin_family = AF_INET; //IPv4 Internet protocols
server.sin_addr.s_addr = INADDR_ANY; //IPv4 local host address
server.sin_port = htons(8895); // server will listen to 8895 port
// Bind
if(bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0)
{
puts("Binding failed");
return 1;
}
puts("Socket is binded");
// Listen
listen(socket_desc, 3);
// Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
new_socket= accept(socket_desc,(struct sockaddr *)&client,(socklen_t*)&c);
if (new_socket<0)
{
puts("Accept failed");
return 1;
}
puts("Connection accepted");
//message = "\n\nHello client, I received your connection. Bye now\n\n";
int choice = 1;
while(choice != 0){
recv(new_socket, received_message, 1000, 0);
int ret = strcmp(received_message,"quit");
printf("\nreturn value of strcmp():%d\n",ret);
printf("\nreceived message:%s",received_message);
if(ret == 0){
choice = 0;
}
}
close(socket_desc);
close(new_socket);
return 0;
}
Output:
Server Console:
what's wrong with this code? Can anyone help?
c
sockets
websocket
telnet
0 Answers
Your Answer