Solved

how to receive a complete TCP message

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unable to build a Win32 solution with EXE and DLL projects inside: Visual Studio 2010 3 95
Getting IP address 8 71
c++ syntax question 9 44
Problem with SqlConnection 4 160
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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 learn how to clear a vector as well as how to detect empty vectors in C++.

864 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

23 Experts available now in Live!

Get 1:1 Help Now