473,320 Members | 1,887 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,320 software developers and data experts.

sockets: htonl question


Hello,

I am aware of the need for ntohl and htonl.
But floats?

I am aware of Beej's (Brian "Beej Jorgensen" Hall) implementation:
http://beej.us/guide/bgnet/output/ht....html#htonsman

Yet, I devised my own kludge:
------------------------
uint32_t data, data1;
float num1;

i =read(descriptor, (char *) &data, sizeof(uint32_t));
data1 = ntohl(data);
num1 = *(float*) ( &data1);
--------------------------

This works for me; always has!
Now, mind you, I have done scientific coding.
And in an enviroment with extremley well-behaved floats,
where round-off has never been an issue.
And very little exceptions....

So...

Why does my simpler implementation work?
What problems could it cause?
Friends have advised me to use Jorgensen, but I do not see the need.
(I also have not gotten it to work)

Thanks,
tom
Jul 23 '08 #1
1 3077
On Jul 23, 12:16*pm, Tom Impelluso <impel...@attila.sdsu.eduwrote:
Hello,

I am aware of the need for ntohl and htonl.
But floats?

I am aware of Beej's (Brian "Beej Jorgensen" Hall) implementation:http://beej.us/guide/bgnet/output/ht....html#htonsman

Yet, I devised my own kludge:
------------------------
uint32_t data, data1;
float num1;

i =read(descriptor, (char *) &data, sizeof(uint32_t));
data1 = ntohl(data);
num1 = *(float*) ( &data1);
--------------------------

This works for me; always has!
Now, mind you, I have done scientific coding.
And in an enviroment with extremley well-behaved floats,
where round-off has never been an issue.
And very little exceptions....

So...

Why does my simpler implementation work?
What problems could it cause?
Friends have advised me to use Jorgensen, but I do not see the need.
(I also have not gotten it to work)
Sockets are technically off-topic here (cf. <http://www.parashift.com/c
++-faq-lite/how-to-post.html#faq-5.9>).

<OT>I think "Beej" addresses your question as to why in section 6.4
<http://beej.us/guide/bgnet/output/html/singlepage/
bgnet.html#serializationwhen he mentions different binary formats
for floating point numbers.</OT>

Cheers! --M
Jul 23 '08 #2

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

Similar topics

11
by: Nick Keighley | last post by:
I'm probably missing something rather obvious, but is there a known problem with getting a Python based socket program to communicate with a C based socket program? A simple echo server written in...
4
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I...
15
by: kernel.lover | last post by:
Hello, i want to know to have multiple clients connects to same server program does following is correct code #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include...
2
by: linuxer | last post by:
Hello all, I have the following program, which will take ipv4 address as input in command line and convert it from host byte order to network byte order. I need similar thing to be done for IPv6....
3
by: derSchweiz | last post by:
Hi, Experimenting with sockets, and I think I got it half working. I have the following code: Private Sub socket_receive(ByVal sender As Object, ByVal e As System.EventArgs) Handles...
4
by: Anonymous | last post by:
Hi Folks, Perhaps someone knows the answer to this. I wrote a small socket app on fedora linux that simply loops and sends data until the connection is lost (by the remote peer). I look for a...
0
by: richard.charts | last post by:
Well, unfortunately, I couldn't get any answer on the msdn forums, so I'll give it a shot here. Real question at the bottom....
1
by: kyosti | last post by:
Hello, I'm writing in c++ on linux. I'm trying to send several JPG-files through sockets one after another and I fail( I'm doing it in this way: first client sends the server "FBEGIN" message,...
2
by: absolute0 | last post by:
Suppose I had the following code: unsigned int a = 0xb07b6fde; unsigned long b = htonl(a); b = b >> 4; printf("int: %d\n", b); printf("hex: %x\n", b); printf("\n");
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.