0

I am very new to socket programming and I'm currently working on a TCP program which accepts various commands from the user (one being ls-remote). The client sends the command to the server, and then depending on what the command is, the server acts accordingly. For ls-remote, the server sends back to the client its current directories from the folder where it is running and the client prints them to the user. This is working perfectly. However, this is supposed to be done in a loop until the user enters "exit". This is where my problem is, when the program prompts the user once again to enter another command after it has already done work with the server, then it does not work (ls-remote). I believe it's because close(newsockfd) is done at the end in my server code but I don't understand why I must do close(newsockfd) (or if I should even do that if I want to continue sending commands to the server?). Also, if close(newsockfd) is required, how can I fix my problem so the user can enter commands until they wish to exit? Here is my code: please ignore the other commands as I have not implemented them yet.

3
  • You try to accept a new client after every recv call. Commented Sep 18, 2015 at 18:59
  • After running once through the for-loop it starts all over again. First the server accept a client, then the client sends a message to the server, the server receives it, etc. After this whole stuff's done, the for-loop starts again waiting for a client to connect, hence preventing to receive any messages. Commented Sep 18, 2015 at 19:02
  • 1
    Yes... that's supposed to be the part where your mind comes into play. Commented Sep 18, 2015 at 19:10

1 Answer 1

1

TCP is a byte stream protocol, not a message protocol. You need a message protocol, but have neither implemented nor used one. At best, this may appear to work some of the time.

n = recv(newsockfd, buffer, 255, 0); 
if(n < 0) syserr("can't receive from client"); 
else buffer[n] = '\0';
printf("SERVER GOT MESSAGE: %s\n", buffer);

You never implemented a message protocol, but your code expects to receive messages. So you're hoping that this will work somehow by magic. If you want to send and receive messages, you have to write code that sends and receives messages. TCP is not a message protocol.

Start by defining a message in terms of bytes. For example, "a message is a sequence of characters not containing a newline and ending with a newline". Or, "a message is a sequence of characters preceded by the number of characters as a 16-bit integer in network byte order". Then write code to send and receive "messages" as you've defined them.

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

2 Comments

This has to be by far the #1 mistake in TCP programming. How do we prevent newbies from making this same mistake over and over again?
@JonathonReinhart We pass a law requiring people to get a license before they can write software that uses TCP. The sole requirement for getting the license is the ability to recite this phrase from memory and define every term in it, "TCP is a byte-stream protocol that provides reliable, ordered delivery but does not preserve application message boundaries."

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.