How to use printf more efficiently?

A

Abby

I'm working on sending and receiving data packet using UDP. I don't
know the length of the data packet I'm receiving but I would like to
print out all bytes in that packet to the screen. How can I do that?
Right now, I have to do something like this ...

printf("Data Received: \n");
for (i=0; i<=50; i++){
printf("Data %d: %d\n", i, resp->data);
}

"50" is the no. that I thought the data bytes in the packet will not
exceed this range. Please let me know if there're other ways to print
out the exact number of data bytes in the packet. Thank you.

Abby.
 
B

Bertrand Mollinier Toublet

Abby said:
I'm working on sending and receiving data packet using UDP. I don't
know the length of the data packet I'm receiving but I would like to
print out all bytes in that packet to the screen. How can I do that?
Right now, I have to do something like this ...

printf("Data Received: \n");
for (i=0; i<=50; i++){
printf("Data %d: %d\n", i, resp->data);
}

"50" is the no. that I thought the data bytes in the packet will not
exceed this range. Please let me know if there're other ways to print
out the exact number of data bytes in the packet. Thank you.

Note that networking is not topical on c.l.c.

That said, in general, when you have a buffer partially filled with data
that you want to access, you need to have one of the two following
bits of information:

- either you should know the size of the data you are accessing.
- or you should have a marker of the end of the data (like the nul
terminator for C strings).

If you don't have any of these, you can't do what you want.
<OT>
In the terms of your own problem, you can only have a marker of the end
of the data if you know what the content of the UDP packets is, and if
(if any) there is a data terminator.

However, you are most likely using some implementation of the Berkeley
sockets to receive your packets, and the recv function lets you know how
much data was read (with its return value).
</OT>
 
G

Gordon Burditt

I'm working on sending and receiving data packet using UDP. I don't
know the length of the data packet I'm receiving but I would like to
print out all bytes in that packet to the screen. How can I do that?
Right now, I have to do something like this ...

printf("Data Received: \n");
for (i=0; i<=50; i++){
printf("Data %d: %d\n", i, resp->data);
}

"50" is the no. that I thought the data bytes in the packet will not
exceed this range. Please let me know if there're other ways to print
out the exact number of data bytes in the packet. Thank you.


ANSI C doesn't do networking.

How did you read the UDP packet? fread() will tell you the number
of elements (which should probably be characters, not the expected
size of the whole packet) you actually read. Other non-ANSI-C POSIX
functions like read() or recv() may give you similar information.

A UDP packet is probably NOT going to be a nul-terminated string, so
don't count on that for finding the end. If the header is included,
you'll need to skip over that part and expect that it's likely to
contain some '\0' characters in it.

Gordon L. Burditt
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top