Solved

how to receive a complete TCP message

Posted on 2012-03-27
2
570 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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.

746 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

10 Experts available now in Live!

Get 1:1 Help Now