UDP sockets - How to read data? 
Author Message
 UDP sockets - How to read data?

Hi!  I have an application that sends messages "of varied
size" between two computers. The messages have a length
field at the beginning of the message. I"m sending these
message via UDP.

When I do a recvfrom to read the first four bytes (the
length field), I keep getting the WSAMSGSIZE error,
indicating that there's more data on the socket (which I
know, because one or more messages is following). But how
do I know that it read 4 bytes when it returns
SOCKET_ERROR w/ the error being WSAMSGSIZE? I want to read
the first 4 bytes, which tells me how big the message is,
then read the rest of the message. And subsequently read
any other messages sitting on the socket, using the
message length via telling me how big my msgs are.  

I"m not talking about the length field of IP header, I'm
talking about a length field in my message definition.

Can someone tell me how to read from message to message on
a UDP socket using my length field?? I'm not sure how to
get around the WSAMSGSIZE error.

Thanks
-C



Tue, 06 Jul 2004 03:59:24 GMT  
 UDP sockets - How to read data?

Quote:
> Hi!  I have an application that sends messages "of varied
> size" between two computers. The messages have a length
> field at the beginning of the message. I"m sending these
> message via UDP.

> When I do a recvfrom to read the first four bytes (the
> length field), I keep getting the WSAMSGSIZE error,
> indicating that there's more data on the socket (which I
> know, because one or more messages is following). But how
> do I know that it read 4 bytes when it returns
> SOCKET_ERROR w/ the error being WSAMSGSIZE? I want to read
> the first 4 bytes, which tells me how big the message is,
> then read the rest of the message. And subsequently read
> any other messages sitting on the socket, using the
> message length via telling me how big my msgs are.
> [...]

recvfrom() and sendto() cannot handle multiple calls per packet. You must
build, send and receive all data as a block.

WSAStarup() will return the maximum datagram size (which is slightly smaller
than 64k) in iMaxUdpDg. Allocate a receiving buffer that big and parse the
data in it. Don't forget to check the received data length!

In fact you don't even need a counter - recvfrom() will return the length of
the datagram.

--
 Paul Chitescu

 Any spammers will be painfully squeezed into /dev/null



Tue, 06 Jul 2004 04:57:40 GMT  
 UDP sockets - How to read data?

Quote:

> Hi!  I have an application that sends messages "of varied
> size" between two computers. The messages have a length
> field at the beginning of the message. I"m sending these
> message via UDP.

> When I do a recvfrom to read the first four bytes (the
> length field), I keep getting the WSAMSGSIZE error,
> indicating that there's more data on the socket (which I
> know, because one or more messages is following).

You cannot just ask for the first 4 bytes, and then ask for the rest of the
bytes.

Quote:
> But how
> do I know that it read 4 bytes when it returns
> SOCKET_ERROR w/ the error being WSAMSGSIZE?

In this case, recvfrom does return those first 4 bytes, and the rest are
discarded because you did not supply a large enough buffer.

Quote:
> I want to read
> the first 4 bytes, which tells me how big the message is,
> then read the rest of the message. And subsequently read
> any other messages sitting on the socket, using the
> message length via telling me how big my msgs are.

Since you are sending the UDP packets, you know what the largest size is, right?

For example, if you never send packets larger than 2K, then create a 2K receive
buffer and read the whole packet. It is OK to provide recvfrom with a buffer
that is larger than the packet. In fact, the return value of recvfrom is the
actual length of the packet received.

Quote:
> I"m not talking about the length field of IP header, I'm
> talking about a length field in my message definition.

> Can someone tell me how to read from message to message on
> a UDP socket using my length field?? I'm not sure how to
> get around the WSAMSGSIZE error.

BTW, it would also not hurt to read a book on TCP/IP, or some online tutorials.

Quote:
> Thanks
> -C

Phil Frisbie, Jr.
Lead Developer, Hawk Software
http://www.hawksoft.com


Tue, 06 Jul 2004 04:19:45 GMT  
 UDP sockets - How to read data?
You have to read the whole datagram at once, in one read. Unlike TCP
sockets, you cannot read data in pieces. Use ioctlsocket(FIONREAD) to
learn the amound of data pending on the socket. Alternatively, you can
use getsockopt(SO_MAX_MSG_SIZE) to learn the maximum possible size of
the datagram, and always request that much.

With UDP, size of the data packet is maintained by the protocol stack.
Using your own length indicator is unnecessary. recvfrom will tell you
the size of data actually copied into the buffer
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Quote:
> Hi!  I have an application that sends messages "of varied
> size" between two computers. The messages have a length
> field at the beginning of the message. I"m sending these
> message via UDP.

> When I do a recvfrom to read the first four bytes (the
> length field), I keep getting the WSAMSGSIZE error,
> indicating that there's more data on the socket (which I
> know, because one or more messages is following). But how
> do I know that it read 4 bytes when it returns
> SOCKET_ERROR w/ the error being WSAMSGSIZE? I want to read
> the first 4 bytes, which tells me how big the message is,
> then read the rest of the message. And subsequently read
> any other messages sitting on the socket, using the
> message length via telling me how big my msgs are.

> I"m not talking about the length field of IP header, I'm
> talking about a length field in my message definition.

> Can someone tell me how to read from message to message on
> a UDP socket using my length field?? I'm not sure how to
> get around the WSAMSGSIZE error.

> Thanks
> -C



Tue, 06 Jul 2004 04:23:30 GMT  
 UDP sockets - How to read data?

Quote:

> You have to read the whole datagram at once, in one read. Unlike TCP
> sockets, you cannot read data in pieces. Use ioctlsocket(FIONREAD) to
> learn the amound of data pending on the socket.

PLEASE don't tell people to do that! Not only is it a bad programming practice,
it really hurts Winsock performance.

Quote:
> Alternatively, you can
> use getsockopt(SO_MAX_MSG_SIZE) to learn the maximum possible size of
> the datagram, and always request that much.

But since he is sending the packets, he already knows the maximum size...

Quote:
> With UDP, size of the data packet is maintained by the protocol stack.
> Using your own length indicator is unnecessary. recvfrom will tell you
> the size of data actually copied into the buffer
> --
> With best wishes,
>     Igor Tandetnik

Phil Frisbie, Jr.
Lead Developer, Hawk Software
http://www.hawksoft.com


Wed, 07 Jul 2004 00:49:39 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Problem reading data from TCP server using sockets!

2. Socket: Client read() same data from the server.

3. reading data from a socket, help...

4. How check timeout for reading the data from socket

5. Close a UDP socket

6. Q: Writing UDP Socket in C...

7. udp socket

8. UDP Datagram sockets

9. UDP Socket question

10. TCP/UDP sockets in Visual C++.

11. Broadcast over UDP socket

12. UDP socket and InstallShield???

 

 
Powered by phpBB® Forum Software