Why is recvfrom() buffer null when bytes are received ?!!
Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
First, are you sure the buffer is full of ASCII NUL? The way you’re testing, it might come out as non-printable characters. Also, the values are most likely non-NUL prior to the call (you can even check what the values are prior to and after the call). Also, check numerical values rather than trying to print the textual values (unless of course you want to pipe to od or something):
Thanks for your help. I eventually figured it out.
I ran a for loop to spit out the contents of the buf array.
[where 'int i < recvd' from the recvfrom()]
I printf'd each buf[i] as both a '%u' and a '%c'.
The '%u' showed every byte of the packet. While '%c' was only populated on certain bytes like the transfer mode and filename.
So I guess I might have confused myself by typecasting 'buf' to a String?
I'm saying this because 'buf' doesnt printf as '%s', but each element in the 'buf' array will printf as a single '%u' or '%c'.
I wasn't quite following what you meant by ASCII NUL.
Perhaps these are those elements in the 'buf' array that did not print as '%c' but would print as '%u'?
As far as values being not-NUL before the call...
Is this why you would see a memset() being called to 'clear' a variable because when C allocates memory for that variable, that memory location may already have data there?
Last edited by stees; 03-16-2008 at 02:11 AM.
Reason: grammar
I printf'd each buf[i] as both a '%u' and a '%c'.
The '%u' showed every byte of the packet. While '%c' was only populated on certain bytes like the transfer mode and filename.
There is a difference between printable and non-printable characters. In C, char is an integer type, but it is wide enough to hold a character from a basic charset (usually 8 bits and usually representing some superset of ASCII). Some of these characters have a glyph when printed, others have some other meaning when printed, and others don’t have any effect on modern-day hardware.
For example, in the following code (under an ASCII-compatible locale):
The first statements prints the character ‘A’. The second statement does not print a character, but moves the cursor to the beginning of the next line (because in ASCII 10=='\n'). The third statement has no visible effect. You can use the standard C function isprint() to determine whether or not a particular character prints in your locale.
Quote:
Originally Posted by stees
I wasn't quite following what you meant by ASCII NUL.
Perhaps these are those elements in the 'buf' array that did not print as '%c' but would print as '%u'?
In C “strings” are terminated by the NUL character ('\0') which is semantically different from the NULL pointer (even though they will evaluate to the same value—0—in many situations). For example, suppose I have the following code:
The two commented lines are equivalent to the first line. The output of the above code is just “Hello” (since the string-expecting printf will stop output when it encounters the first '\0').
Quote:
Originally Posted by stees
As far as values being not-NUL before the call...
Is this why you would see a memset() being called to 'clear' a variable because when C allocates memory for that variable, that memory location may already have data there?
With automatic storage, you always already have data there. It is not specified what this data is (but it’s usually stack space, so it may have remnants of previously-called functions). So if you want to be sure there is a specific value, you initialize it to something (like the CANARY in my other post). If you specifically allocate memory, or if your buffer is large, you might want to use memset(). Only if you use static storage is your data guaranteed to be initialized to zero upon first use.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.