i have two files a client and a server , i want to send the value of the variable (choice) from the client to the server , but the value is not being sent and i have no idea what the problem is
this is my code for client
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<fcntl.h>
int main(int argc , char *argv[])
{
char buf;
int clientSocket, cnnt,fileDiscriptor,sizeInByte=0 , size=0 , serverSocket,choice=2 ;
struct sockaddr_in serverInfo;
if(argc != 3)
{
printf("Few arguments\nUsage : ./client <ip> <port>\n");
exit(1);
}
memset(&serverInfo,0,sizeof(serverInfo));
serverInfo.sin_family = AF_INET;
inet_aton(argv[1],&serverInfo.sin_addr.s_addr);
serverInfo.sin_port = htons(atoi(argv[2]));
clientSocket = socket(AF_INET,SOCK_STREAM,0);
if(clientSocket < 0)
{
printf("Could not create client socket\n");
exit(1);
}
cnnt = connect(clientSocket,(struct sockaddr *)&serverInfo,sizeof(serverInfo));
if(cnnt < 0)
{
printf("Could not create connection to the server\n");
exit(1);
}
recv(clientSocket , &serverSocket , sizeof(int) , 0);
send(serverSocket , &choice , sizeof(int),0);
close(clientSocket);
return 0;
}
and this is my code for the server
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <sys/wait.h>
int main(int argc , char *argv[])
{
int choice;
int serverSocket, bnd, lstn , clientSize, connectionFd;
struct sockaddr_in serverInfo, clientInfo ;
if(argc != 2)
{
printf("Few arguments\nUsage : ./server <port>\n");
exit(1);
}
memset(&serverInfo , 0 , sizeof(serverInfo));
memset(&clientInfo , 0 , sizeof(clientInfo));
serverInfo.sin_family = AF_INET;
serverInfo.sin_port = htons(atoi(argv[1]));
serverInfo.sin_addr.s_addr = INADDR_ANY;
serverSocket = socket(AF_INET,SOCK_STREAM,0);
if(serverSocket < 0)
{
printf("Could not create server socket\n");
exit(1);
}
bnd = bind(serverSocket , (struct sockaddr *)&serverInfo , sizeof(serverInfo));
if(bnd < 0)
{
printf("Could not bind\n");
exit(1);
}
lstn = listen(serverSocket , 1);
if(lstn < 0)
{
printf("Could not listen on server socket\n");
exit(1);
}
clientSize = sizeof(clientInfo);
while(1)
{
connectionFd = accept(serverSocket , (struct sockaddr *)&clientInfo , &clientSize);
if(connectionFd < 0)
continue;
send(connectionFd , &serverSocket , sizeof(int) , 0);
recv(serverSocket , &choice , sizeof(int),0);
close(connectionFd);
break;
}
printf("CHOICE IS :%d\n",choice);
return 0;
}
are you sure it is not
send-ing? IMO,recv(clientSocket , &serverSocket , sizeof(int) , 0);in client is blocking, thus not reaching the
send()call. Can you put therecv()call [which is not intended and required, IMO] aftersend()in client side?Hint: Server is listening for incoming connection , and as long as there is no imcoming connection, it doesn't has an address to
sendthe communicate back.EDIT:
As you're sending
inttype, [more than 1 byte size], I would recommend usinghtonl()in client side andntohl()is server side to take care of the endianness issue [if any].