2

I am writing c++ socket code and i need some help ! in my program i don't know what the size of the message would be , it may send a part of file or the file it self ,the file may be a huge file , so shall i specify a maximum size for the packet , shall i divide it to more than one if exceeded the maximum ?

1
  • Have a look at ZeroMQ. It makes message passing a joy to do. Commented Aug 20, 2012 at 17:09

2 Answers 2

9

It's never constructive to think about "packets" and "messages" when using TCP because:

  • The network engine has its own ways of deciding the best segment size
  • Segment size makes no difference to application code: the receiving TPC is free to coalesce segments before passing data to the receiving process

You should view TCP the way it was designed: reliable in order bytes-stream service. So just write large-enough blocks and the engine and its myriad of rules will take care of it.

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

7 Comments

so that when my application want to send a segment over the network ,simply i can send it regardless of its size ,right? i mean that the buffer value would be recieved completely on the server side without any data losing : write(sockfd,buffer,strlen(buffer)); that's right? thanks a lot..
@sana You should also check the return code of the write function / method.
yes i know , about the return value , but my focus here is a bout the buffer size :)
@sana They're related. The return code usually tells you how many bytes were written.
but although the third parameter tells the write the bytes count to send , here is my question. it may be a very large number ,so if i printed the n value may it be less than the beffer length i passed it as a parameter to the write method ?
|
2

The problem is a little vague, but the approach seems universal. The transmitter should send an indication of how many bytes the receiver should expect. The receiver should expect to see this indication, and then prepare to receive that many bytes.

As far as packet size, generally an application does not worry about how the bytes are delivered on the network per se, but the application may care about not calling send and recv system calls too many times. This is particularly important on a concurrent server, when efficiency is key to scalability. So, you want a buffer that is big enough to avoid making too many system calls, but not so big as to cause you to block for a long time waiting for the data to drain into the kernel buffer. Matching the send/recv socket buffer size is usually sufficient for that, but it depends on other factors, like the bandwidth and latency of the network, and how quickly the receiver is draining the data, and the timeslice you want to allow per connection being handled during concurrency.

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.