UDP Reliability?

Started by
4 comments, last by WebsiteWill 20 years, 8 months ago
I''m working on the recieving end of my network library and got to thinking about something. I was going to use multiple threads to handle incoming packets. Each thread would call recieve and get a packet then store that packet onto a queue after it has been checked for type. So call revcfrom() check packet type cast packet into appropriate packet type (passing generic poitners) and store packet onto the proper queue for further processing. Then it hit me. I know that when you call recv with TCP you aren''t guaranteed to recieve an entire packet because it is a data stream. All this time I have been under the assumption that with UDP if i call recv and there is anything there then it will gather it up because I thought UDP guaranteed that if a packet arrived at all then it arrived completely intact. Is this the case? Also, what about multiple packets arriving. Is it possible for a single call to recvfrom to return more data than just a single packet? Or will it only recvfrom up the the next header on the next packet? If that''s the case then everything is fine. If it''s possible that it will recieve more info than a single packet then I''ll have to redesign some of this code. At that point it will become a single thread doing all of the recv calls, converting them to proper packet types and storing them onto proper queues as necessary. Thanks for any help, Webby
Advertisement
I found this in "Unix Network Programming"
Page 366
"In the case of a UDP socket with a datagram on the recieve queue, if we call recvfrom specifying MSG_PEEK, followed by another call without specifying MSG_PEEK, the return values from both calls (the datagram size, its contents, and the senders address) will be the same, even if additional datagrams are added to the socket recieve buffer between the two calls. (We are assuming, of course, that some other process is not sharing the same descriptor and reading from this socket at the same time)."

So, based on this, am I correct in assuming that a call to recieve will read in only the first packet on the socket recieving queue even is multiple packets are available? Seems logical but I can''t find a definitive answer to this other than the passage above.

Thanks,
Webby
Yes, your assumption is correct, with the exception of EMSGSIZE, which you should understand as well.

Keep in mind that the packet-based nature of UDP is a great advantage in this case (i.e. easy to use), but it has downfalls. If you send large packets (greater than 548/1472 wan/ethernet respectively), they might be fragmented along the route between hosts. Suddenly you have two(or more) packets, either one of which could be dropped by a busy router. If either packet is dropped, the whole thing is lost. If both packets arrive at the destination, they will be reassembled at the IP layer, and given to your recv() call as one packet. Basically this means that smaller packets are more likely to reach the destination. Unfortunately, this also results in less efficient use of your available bandwidth because your header to data ratio is reduced).

[edited by - fingh on August 6, 2003 1:07:53 PM]
Thanks fingh.

I''ve just about got it running. Having trouble with a casting situation right now that reappeared. Thought I had solved it but I guess not.

I''ll post it here, I''m sure someone reading this thread can help out instead of starting a new thread.

struct S
{
int iNumber;
int iStuff;
string sText;
}sInfo;

I have that as a member of a class. The class contains other functions that help deal with the packets and as was discussed on an older thread, each packet class has a virtual sendon() function that I want to use to convert the structure above into the character array and send over the net.

I had thought it was
char* cpData = (char*)S; or char* cpData = (char*)sInfo;
but neither are seeming to work.
Any ideas here? I''ve done some test cases with this in the past and I can''t see what the problem is now.

This is the actual error.
" error C2275: ''PacGeneric::S'' : illegal use of this type as an expression"
The offending code is
char* cpsInfo = (char*)S;

Any ideas?
Thanks,
Webby
Wouldn''t it just be
char* cpsInfo = (char*)&sInfo;
?
Since sInfo isn''t a pointer, you just get the pointer to it then cast that.
Gah. I figured it out

S* tempPacket = new S;
char* cpsInfo = (char*)tempPacket;

works. Though now looking at it so does
char* cpsInfo = (char*)&sInfo
and is neater.

Thanks AP
Looking too deep for the problems.
Must be Beer:30.
Time for a break

Webby

This topic is closed to new replies.

Advertisement