Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Winsock programming problem

Posted on 2005-04-07
Medium Priority
Last Modified: 2010-04-01
Hi All,

I am writing a simple clinet/server program by using winsock.

If TCP is being used, is this truth that the recv function may be called twice sometimes in order to receive the whole packets sent by another side.

e.g A side is sending 2048 bytes, I may receive only 1000 bytes in my first call of the recv function. Then I receive the remaining 1048 bytes in another call of the recv function.

Is this truth?

Please advise. Thanks.
Question by:ee_lcpaa
  • 2
  • 2
LVL 22

Expert Comment

ID: 13728071

So you can't depend on TCP to define "chunks" for you.  

One simple way is to precede each of your chunks with their length.  The receiver just reads the length, then reads "length" more bytes.


Author Comment

ID: 13737581
Hi grg99,

what is the reason?

Is the packet too large, it is divided into two pakcets & reassemble at the receive side later.
Please advise. Thanks
LVL 22

Expert Comment

ID: 13741154
Because TCP is defined as being free to merge and split data as needed.   If you do a bunch of 1-byte writes, TCP typically buffers them up until it gets a good sized packet, then sends it all in one chunk.    Similarly if you write a huge buffer, TCP has to split the data into packets that the network can handle.  For example, Ethernet is limited to 1580 byte packets, the internet routers in general only guaranteed 520 bytes. (I think now 99% of them handle full-size ethernet packets, but the guarantee is only 520).

So you may get bigger or smaller chunks than you send.   Any chunking you want done you have to do yourself.  Luckily it's almost trivial, just send a length word in fornt of each chunk, then collect the right size chunk on the receiving end.


Author Comment

ID: 13746032
Hi grg99,

Do you mean the max. packet size is limited by the network equipments / physical wire usually?


Accepted Solution

DarthMod earned 0 total points
ID: 14526837
PAQed with points (250) refunded

Community Support Moderator

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question