Re: socket programming-receive() function , VC++ 6.0

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Vani,
I think this thread has given you all the information you need. You do not have a MS specific issue or any real general networking programming issue other than lack of very basic knowledge. The best thing for you to do would be to consult a basic book on computer networking. The one issue that the thread did not cover in detail is that on the receive side you need to accumulate the bytes Receive delivers to you and then break them into separate appplication level messages. Thus your OnReceive code may end up proccessing 0, 1, or more application level messages on each call.

Luke

vani wrote:
Any sample code please?

"Dick Dawson" wrote:


To expand on Arkady's explanation you should forget about whether the format of a message is a string or anything else. The most common thing to do is to prepend each message with its length (length here means the acutal number of bytes sent, not string length which doesn't count terminators or may have some other meaning). If messages are less than 255 bytes you can do this with one byte but if you need to use a short or int as the message length you'd be wise to use hton[sl] to put the length in network order before sending and ntoh[sl] on the receive side. You then need to treat the messages received as a stream recognizing that any received message may be only a fraction of a transmitted message, a whole message, parts of multiple messages, or any other fractured contiguous combination. The only thing TCP guarantees (unlike UDP) is that the bytes are received in the same order as they are sent and they are correct. Once you break the stream into individual messages with the aid of your length indicator, then you can deal with the internal structure of the message, i.e. whether it is a string or not.

Luke

"Arkady Frenkel" <arkadyf@xxxxxxxxxxxxxxxx> wrote in message news:uTiV3tpaGHA.4580@xxxxxxxxxxxxxxxxxxxxxxx

So you need explicitly set length of the packet ( or with VB style - lenght before packet , or
ASCII - null as list byte )
Arkady

"vani" <vani@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:CD9F899C-BC9B-42E5-BC4E-1BDA8A24E365@xxxxxxxxxxxxxxxx

Thank you for the reply,
Here i am using TCP socket stream based for communication. This
application is about to communicate with the external device connected to the
port of terminal server. When a command is send from the application to the
external device, the external device response for that command. For one
command , the reply is 18 bytes. But i am able to receive only 14 bytes. The
message is being cut at when ascii value of 0 is occuring. But i am able to
get the complete message when communicated thru Visual Basic Application. If
any information abt the appl is required , i am ready to convey.

"Michael K. O'Neill" wrote:


"vani" <vani@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:C2AABA39-4512-4C29-A9DF-BEFE1A8B5F69@xxxxxxxxxxxxxxxx

Hi,

When executed the following,The receive() is returning 18 bytes of
data(p=18). But the buf variable is having only 14 characters. SO, i am
getting assertion error at i=14 .Can anybody help? Here m_connectsocket is

an

object for class derived from CSocket and 'k' value is 14.

void CSockMFCDlg::onreceive()
{
char buf[256];
Sleep(5000);
int p=m_connectsocket.Receive(buf,256);
...... << snip >>

Why do you think that the sender sent you a NULL-terminated string? Maybe
it didn't.

Or, maybe it sent you two NULL-terminated strings (maybe even in two
separate Send's), but since TCP is a stream-based protocol, a portion of the
second string has been Receive'd after the first. Your program needs to
accommodate this possibility, which happens all the time in TCP. This is
what Luke, above, is telling you in his point #1.

Mike
.



Relevant Pages

  • Re: socket programming-receive() function , VC++ 6.0
    ... of a message is a string or anything else. ... Once you break the stream into ... Here i am using TCP socket stream based for communication. ... external device, the external device response for that command. ...
    (microsoft.public.win32.programmer.networks)
  • Re: socket programming-receive() function , VC++ 6.0
    ... of a message is a string or anything else. ... Once you break the stream into ... Here i am using TCP socket stream based for communication. ... external device, the external device response for that command. ...
    (microsoft.public.win32.programmer.networks)
  • Re: Question about design, defmacro, macrolet, and &environment
    ... "Expects to find the literal string on the stream." ... (defun send (string &optional stream) ... (declaim (inline make-adjustable-string)) ...
    (comp.lang.lisp)
  • Re: Strange problem when not in debugger
    ... private string huidigWeb; ... int buffLength = 2048; ... // Opens a file stream to read the file to be uploaded ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ReplacerStream
    ... string, do a replace on that string and create a stream again to be ... If those are problems, and you are looking just for a single string, it seems to me that you could just read the stream one character at a time, checking to see if it matches the current character in your search string. ...
    (microsoft.public.dotnet.framework)