I am trying to make a simple echo program which includes a client and a server utilizing TCP protocol under Socket Programming. For this purpose I am using C to write both server.c and client.c.
The issue I am getting is in server.c program where I am trying to reveive a message from the client, after the connection has been made but keeps on failing. This happens in the recv() API that is in the server program. Here are the two programs:
1.) Server.c:
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/ip.h>
int main(){
//Let's create a socket here.
int serverSoc = socket(PF_INET, SOCK_STREAM,0);
struct sockaddr_in serverInfo;
serverInfo.sin_family = PF_INET;
serverInfo.sin_port = htons(8000);
serverInfo.sin_addr.s_addr = INADDR_ANY;
socklen_t addrLen = (socklen_t)sizeof(serverInfo);
//We need to bind the socket to the port number. So, here we make use of the in-built struct
if(bind(serverSoc,(struct sockaddr*)&serverInfo,addrLen)<0){
printf("The binding of the server socket with the given port has been unsuccessful\n.");
return -1;
}
//After binding the socket, here we listen for connections from the clients
if(listen(serverSoc, 1)<0){ //We used 1 here in the int backlog because that specifies the number of pending connections that the queue will hold. This will help in the case when multiple clients try to connect to the server.
printf("We have an error in connecting to the client(s)\n");
return -1;
}
if(accept(serverSoc,(struct sockaddr*)&serverInfo,&addrLen)<0){
printf("We have an error accepting the connections from the client(s).\n");
return -1;
}
char recvBuff[50];
if(recv(serverSoc,&recvBuff,sizeof(recvBuff),0)<0){
printf("We have an error receiveing messages from the client.\n");
printf("%zd\n",recv(serverSoc,recvBuff,sizeof(recvBuff),0));
return -1;
}else{
printf("The size of recvBuff is:%d\n",(int)sizeof(recvBuff));
printf("%s\n",recvBuff);
}
char sendBuff[50] = "hello";
if(send(serverSoc,&sendBuff,sizeof(sendBuff),0)<0){
printf("We have an error sending message to the client.\n");
return -1;
}else{
printf("%zd\n",send(serverSoc,&sendBuff,sizeof(sendBuff),0));
}
return 0;
}
2.) Client.c:
#include<stdio.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#include<unistd.h>
int main(){
char clientRecv[50];
char clientSend[50] = "Hello Server\n";
//Here, we create a socket for the client with the in-built function socket that we get from sys/socket.h, socket(int domain, int type , int protocol)
int clientSoc = socket(PF_INET,SOCK_STREAM,0);
printf("The client socket has been assigned the value of: %d\n",clientSoc);
if(clientSoc < 0){
printf("We have an error");
return (-1);
}
//all the struct(s) that are needed for the program
struct sockaddr_in serverInfo;
serverInfo.sin_family = PF_INET;
serverInfo.sin_port = htons(8000);
serverInfo.sin_addr.s_addr = INADDR_ANY;
//Connecting the client to the server
if((connect(clientSoc,(struct sockaddr*)&serverInfo,(socklen_t)sizeof(serverInfo)))< 0){
printf("Not connecting");
return(-1);
}
if(send(clientSoc,&clientSend,sizeof(clientSend),0)<0){
printf("We are having an error in sending the message.\n");
return -1;
}
int clientRecvmsg = (int)recv(clientSoc, &clientRecv,sizeof(clientRecv),0);
if(clientRecvmsg<0){
printf("We have error receiving messages from the server.\n");
}else{
printf("clientRecvmsg: %s\n", clientRecv);
return -1;
}
return 0;
}
acceptreturns the new socket you should use to communicate with the client, or -1 ifacceptwas unsuccessful.getaddrinforather than that outdated IPv4 stuff.