windows socket recv problem

S

sonu

Hi,

I m trying to receive a message on windows sockets using recv()
function.
The message is such that the first two bytes of the message represent
the length of the
message. Hence when trying to recv a message, i m doing 2 recv() calls.
The first recv() call receives the length of the message.
and the second recv() call receives the actual message.

The problem is that the first recv() instead of RECEIVING THE FIRST TWO
BYTES, receives the last two bytes of the message.

e.g unsigned char szTbuf[3];
recv(..., (char * ) szTbuf, 2, 0);

the above receives the last two bytes. and after receving it doesnt
come out.

Can any body help me please.

Sincere Thanx.

rgds,
Sandeep.
 
T

Tom St Denis

sonu said:
e.g unsigned char szTbuf[3];
recv(..., (char * ) szTbuf, 2, 0);

the above receives the last two bytes. and after receving it doesnt
come out.

Can any body help me please.

Off topic and you probably send them in the wrong order, or your send
fails or gah.

Check return codes and/or post more complete code showing the error
[ideally though, move this to comp.programmer or something].

Tom
 
S

sandeep

Tom said:
sonu said:
e.g unsigned char szTbuf[3];
recv(..., (char * ) szTbuf, 2, 0);

the above receives the last two bytes. and after receving it doesnt
come out.

Can any body help me please.

Off topic and you probably send them in the wrong order, or your send
fails or gah.

Check return codes and/or post more complete code showing the error
[ideally though, move this to comp.programmer or something].

Tom
 
C

Clever Monkey

sonu said:
I m trying to receive a message on windows sockets using recv()
function.
The message is such that the first two bytes of the message represent
the length of the
message. Hence when trying to recv a message, i m doing 2 recv() calls.
The first recv() call receives the length of the message.
and the second recv() call receives the actual message.
[Follow-ups set.]

This is probably not the best way to do this. Instead, loop on recv
until you have everything. Are you checking return values and error
codes? For example, you may need to care about WSAEMSGSIZE.

See the example use of recv() on MSDN
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/recv_2.asp>
 
S

Skarmander

sonu said:
Hi,

I m trying to receive a message on windows sockets using recv()
function.
The message is such that the first two bytes of the message represent
the length of the
message. Hence when trying to recv a message, i m doing 2 recv() calls.
The first recv() call receives the length of the message.
and the second recv() call receives the actual message.

The problem is that the first recv() instead of RECEIVING THE FIRST TWO
BYTES, receives the last two bytes of the message.

e.g unsigned char szTbuf[3];
recv(..., (char * ) szTbuf, 2, 0);

the above receives the last two bytes. and after receving it doesnt
come out.

Can any body help me please.
Read the Winsock FAQ (http://tangentsoft.net/wskfaq/) from top to bottom.
Some of it is probably not relevant to you (mostly that which deals with
16-bit Windows) but a great many of the rest is, and almost everything in
there is worth keeping in the back of your head.

If that doesn't help you, try a Windows-specific newsgroup like
comp.os.ms-windows.programmer.tools.winsock.

S.
 
A

Arkady Frenkel

But any case put the length at start , so if you not read all at once ,
you'll know how many bytes you have to read more
Arkady

Clever Monkey said:
sonu said:
I m trying to receive a message on windows sockets using recv()
function.
The message is such that the first two bytes of the message represent
the length of the
message. Hence when trying to recv a message, i m doing 2 recv() calls.
The first recv() call receives the length of the message.
and the second recv() call receives the actual message.
[Follow-ups set.]

This is probably not the best way to do this. Instead, loop on recv until
you have everything. Are you checking return values and error codes? For
example, you may need to care about WSAEMSGSIZE.

See the example use of recv() on MSDN
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/recv_2.asp>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top