how to receive a complete TCP message

Posted on 2012-03-27
Last Modified: 2012-03-27
The following code snippet when run, gives a value for numbytes of 3472 decimal, even though I am sending 20000 bytes (the code I use for sending is also given).
how do I check that all the TCP data has been received and is ready to read? I don't normally know in advance, how large the data is, only that it is sent by one call to the Client Send function.

Server code: // using Indy 10

IdTCPServer1Execute(TIdContext *AContext)
int numbytes:

   numbytes = AContext->Connection->Socket->InputBuffer->Size;

client code: // using Indy 9

    char *buff;
    int n = 20000;

    buff = new char[n]; // just some random test data with a known length
    delete [] buff;
Question by:alcindor
LVL 35

Accepted Solution

mccarl earned 500 total points
ID: 37774369
No, there is no way to know!

TCP is a stream protocol, and so everything that is sent is just a stream of bytes, as opposed to being a packet of bytes that may have some length.

Protocols that are built on TCP have some way of knowing how much data to receive. Some know the exact length up front (such as HTTP where the Content-Length header tells how much data to expect). Other protocols know when the data is finished due to some delimiter in the stream of data (such as SMTP/email where lines terminated by CR/LF are processed as one unit). Still others might only use a connection once, and so they can send the data and then close the connection to mark the end of the data.

Since you do know the length of the data, in your client code, you could just send the number of bytes as an integer (or delimited string, etc) first, and then follow that with the data. That way the server can read in that integer first, and then know how many bytes to read in next. The only thing with that is that you may need a bit of extra code to resyncronize with that 'length' information if the stream every gets out of sync.

Otherwise, if your data is constrained in some, say that you data is all printable ASCII characters, then you can just send a marker byte at the end of that data, say an 0x03 byte (which is ASCII for ETX, End of Transmission) and then the server can just read data until it sees the 0x03. And as I said, as long as that byte CAN'T occur in your real data, then there is no problem!

Author Closing Comment

ID: 37775248
OK, I wasn't aware of that. Thanks for your help,


Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

832 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