I'm writing a program that communicates over network using TCP. If user provide ip address as a command line argument, the program would attempt to connect to that address. If not, it will wait for others to connect. For the sake of clarity, even though it's p2p connection, I would call the one the waits for others to connect as server and the other one as client. The server has no problem receiving whatever text message the client send. However, the client side only receives text messages from server only when it sends its own message. How do I fix that so that client side receives messages right away? This is a snippet of my code
a.sin_family = AF_INET;
a.sin_addr.s_addr = iet_addr(SERVER_IP);
addr.sin_port = htons((unsigned short)SERVER_PORT);
Here I'm using same socket that I use for sending. Do I need to create new one for listening?
fgets(buffer,sizeof(buffer),stdin);
send(socket,buffer,strlen(buffer),0);
b = recv(socket,buffer,sizeof(buffer),0);
buffer[b] = 0;
printf("%s",buffer);
Edited: This is for listening socket
add.sin_family = AF_INET;
add.sin_addr.s_addr = htonl(atoi("127.0.0.1"));
add.sin_port = htons((unsigned short)NEWPORT);
Edit: This is binding code
bind(socket,(struct sockaddr *)&add,sizeof(add));
atoi("127.0.0.1")to do. Test it in a seperate program.