Solved

how to receive a complete TCP message

Posted on 2012-03-27
2
579 Views
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:

AContext->Connection->Socket->CheckForDataOnSource(10);
if(!AContext->Connection->Socket->InputBufferIsEmpty())
{
   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
    IdTCPClient1->Socket->Send(buff,n);
    delete [] buff;
0
Comment
Question by:alcindor
2 Comments
 
LVL 35

Accepted Solution

by:
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!
0
 
LVL 2

Author Closing Comment

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

Roger
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

829 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