473,394 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to use printf more efficiently?

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[i]);
}

"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.
Nov 13 '05 #1
2 2498
Abby wrote:
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[i]);
}

"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>

--
Bertrand Mollinier Toublet
"Reality exists" - Richard Heathfield, 1 July 2003

Nov 13 '05 #2
>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[i]);
}

"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
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: elmlish | last post by:
Hello all, I'm currently befuddled as to how to efficiently test for a positive re. match then use the results of that match in a function. Mostly what I've seen people do is to first test for...
14
by: Klaus Neuner | last post by:
Hello, I need to gather information that is contained in various files. Like so: file1: ===================== foo : 1 2 bar : 2 4
3
by: Mark van Heeswijk | last post by:
Probably a very basic question but i'm new to c++ What's the most efficient (mem) way to copy string 'a' to string 'b'? Now i do: char a; char* b; // length not known...could be 1000...
7
by: Adam Hartshorne | last post by:
As a result of a graphics based algorihtms, I have a list of indices to a set of nodes. I want to efficiently identify any node indices that are stored multiple times in the array and the...
0
by: adavidso | last post by:
I am a bit of a newbie to python, and would like a little advice on how to more efficiently process arrays. I work a lot with satellite data, and have opened and read two binary images into...
17
by: G Patel | last post by:
E. Robert Tisdale wrote: > > int main(int argc, char* argv) { > quad_t m = {0, 1, 2, 3}; > int r; > fprintf(stdout, "m = ("); > for (size_t...
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
7
by: eyh5 | last post by:
Hi, I'm writing some C codes to run simulations. I'm wondering if there is a website that may contain useful information on how to make one's code run more efficiently and in a...
31
by: krypto.wizard | last post by:
How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I don't know the correct answer.
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.