Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am doing socket programming in QT and I have to design a protocol to transfer DATA over TCP/IP.

Now my protocol design is SIMPLE and it sends commands in a fashion that the first BYTE of the data written to the socket for every write will be the COMMAND. So whenever I write into the socket using socket->write("CDATA") the first byte, "C" in this case will mean a command for the server to do something.

I just want to know one thing, that whether the write will be broken down into multiple reads on the server ? I know there will be a buffer size on the server for the read. But can the socket->write() on the client be recieved in multiple READs on the server when the write is WITHIN the buffer limits of the server ?

To clear this question I will given an example Lets say the buffer read size of the socket on the server is 4096 BYTES. The client writes socket->write("CDATA") to the server. Now is there any possibility that server will receive this in MORE than ONE reads ? Because I have a while loop on the server :

while{
 char str[] = socket->read();
 // What is the coomand in the first byte 
 if(str[0] == "C"){
  // Do something
 }
}

If the data sent by the client is received in more than ONE reads (even though the client sent it in one write) my protocol design will fail

Your inputs are greatly appreciated.

share|improve this question

closed as off topic by Michael Hampton, Iain Nov 8 '12 at 14:37

Questions on Server Fault are expected to relate to professional server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Your question is off topic for Serverfault because it appears to relate programming. It may be on topic for StackOverflow but please search their site for similar questions that may already have the answer you're looking for. –  Iain Nov 8 '12 at 14:37
    
Your "server" should have a read loop that's completely separate from other functionality. After it reads some data into a buffer it can call a function to check if the buffer contains a command, if it does then pull the command out of the buffer, parse command and call "do something" functions. This is all most complicated than it sounds here. Programming network servers is really quite difficult to do right. =] –  Chris S Nov 8 '12 at 14:44
add comment

1 Answer

Yes, you should always allow for the possibility that you won't read a complete 'command'.

Consider for instance, the case of sending multiple commands at once. You have no idea where the packet boundary will happen.

You need code to recombine the traffic at the other end and break it up into processable chunks.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.