2

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));
4
  • 2
    Can you show more of your code where you connect and send/recv? Commented Dec 3, 2013 at 11:37
  • It just two lines inside while loop. Commented Dec 3, 2013 at 11:42
  • So assuming you want to program some sort of chat system, you probably need a receive-thread that checks the socket for data while you are waiting for user input Commented Dec 3, 2013 at 11:46
  • What do you expect this atoi("127.0.0.1") to do. Test it in a seperate program. Commented Dec 3, 2013 at 18:08

2 Answers 2

2

Do I need to create new one for listening?

Yes, since the socket will be bound to a different address.

You can receive data from server through that socket, but to accept new connections you need a listener socket and 1 socket per other client. Remember that a socket is an endpoint for communication.

Sign up to request clarification or add additional context in comments.

4 Comments

To send messages from server, do I need to create a socket for sending as well. Right now, my server code has only one socket.
@Andrew, No. Once you accepted a connection you use that socket to send data. Sockets are bi-directional, but have (most of the time, for SOCK_STREAM), fixed endpoints.
I created a new socket but it's not working for some reason.I added the code that initializes socket structure in my post.
If you're connecting from outside your machine replace "127.0.0.1" with "0.0.0.0". If not, show the code where you call bind and listen since that seems ok.
0

The problem with your program is that you're using blocking I/O calls, which are rather hard to manage when you want something different from a strict request - reply mechanism. For example, while fgets is waiting for user input, you will not peek at the network, and you will not receive messages sent to you, until fgets finishes, and that's only when the user hits Enter. If the user does not hit any key for an hour, your program will be waiting in fgets for an hour.

For this to work, you have to decouple the networking code from the user input code (you cannot wait for either of them infinitely, holding up traffic from the other). Normally, this can be done with either using threads (so the blocking I/O call is holding up a single thread), or using non-blocking I/O (so that no I/O call is blocking anything). When using C, I'd go for the latter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.