Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to receive a complete TCP message

Posted on 2012-03-27
2
Medium Priority
?
604 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

610 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