Winsock programming problem

Posted on 2005-04-07
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
    LVL 22

    Expert Comment


    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

    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

    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

    Hi grg99,

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

    LVL 1

    Accepted Solution

    PAQed with points (250) refunded

    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now