UDP problems in server/client app

Started by
13 comments, last by hplus0603 15 years, 10 months ago


Isnt p->data a pointer (see below)?? (you treat it that way in the memset)

size of a pointer is usually 32 bits (4 bytes)

You should be sending a array of chars (zero terminated) insid ethe packet data

You dont show how you ar putting data in on the sender end.



you are doing a memset of 4 zeros and no change after the 4th byte
(if the sender isnt zero terminating the data it can overrun after the 4th byte)




typedef struct {
int channel; /* The src/dst channel of the packet */
Uint8 *data; /* The packet data */
int len; /* The length of the packet data */
int maxlen; /* The size of the data buffer */
int status; /* packet status after sending */
IPaddress address; /* The source/dest address of an
incoming/outgoing packet */
} UDPpacket;
--------------------------------------------[size="1"]Ratings are Opinion, not Fact
Advertisement
Quote:Original post by password
Quote:Original post by Kylotan
As hplus0603 said above, you're using sizeof to measure the length of your data, which is wrong. sizeof is a compile-time measure to tell you the size of a given type, and can't know how much data has been placed there. You need to query the packet's 'len' property to see how much to read. Docs here.


I only use sizeof in the memset and memset is something that wasn't originally there either. I don't use sizeof to measure the length of my data at all.


Sorry, my mistake. (sizeof is still the wrong operation to use there though.)
Quote:You should be sending a array of chars (zero terminated) insid ethe packet data


That's wrong -- the data does not need to be zero terminated, unless you intend for it to be a zero terminated string (in which case the zero is part of the data). send() takes a length parameter, so you can send whatever data you want.
enum Bool { True, False, FileNotFound };
We managed to solve it. It works if we send an array of chars which is NULL-terminated with \0.
Quote:It works if we send an array of chars which is NULL-terminated with \0.


That just means that you don't understand what's going on in one of the sending or receiving code paths. You happened to find some work-around that makes it appear to work -- but are you sure that it will keep working? For all possible messages? Is this what it's designed to do?

Trusting in code that "appears" to work, without knowing that it works and why, is one of many ways that software becomes buggy, unstable, and hard to maintain. If you KNOW (through specification or documentation) that the recipient expects a zero-terminated string, then you can send that data -- but then the recipient is fragile and possibly vulnerable to remote attack, because someone might send a packet that does not contain a terminating zero.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement