I have made a simple server that will print to the terminal when a message has been received along with the message. The client I made just sends the message (doesn’t receive) when I telnet and send messages to the server it works great and prints exactly what I sent. However when I write the C Client code found below its as if nothing is sent.
here is my compile output for the code below with -wall -wextra -pedantic
./client.c: In function ‘main’:
./client.c:17:4: warning: implicit declaration of function ‘bzero’ [-Wimplicit-function-declaration]
./client.c:19:4: warning: implicit declaration of function ‘inet_addr’ [-Wimplicit-function-declaration]
./client.c:28:2: warning: implicit declaration of function ‘strlen’ [-Wimplicit-function-declaration]
./client.c:28:41: warning: incompatible implicit declaration of built-in function ‘strlen’ [enabled by default]
./client.c:22:8: warning: unused variable ‘val’ [-Wunused-variable]
./client.c:10:15: warning: unused variable ‘n’ [-Wunused-variable]
./client.c:8:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
./client.c:8:26: warning: unused parameter ‘argv’ [-Wunused-parameter]
soup@soup-XPS-8300:/home/c_dev/p2pcrypt_server/v0.1.0/src/tool_tests/libev
–
/* Sample TCP client */
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
int main(int argc, char**argv)
{
int sockfd,n;
struct sockaddr_in servaddr;
char * sendline;
sendline = "hello";
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=htons(8018);
int connect_status = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
printf("%d\n", connect_status);
int send_status = send(sockfd,sendline,strlen(sendline),0);
printf("%d\n", send_status);
printf("END\n");
return 0;
}
Updated code
Connect returns “0” (success) and send returns “5” (i think thats how many successfull characters it sent)
Here is the successfully working code on my end it seems the \r\n was needed and all the great suggestions that was pointed out. Thanks mates!