Solved

byte ordering in networks (read, write)

Posted on 2001-09-14
4
265 Views
Last Modified: 2010-04-15
I'm writing a network program that communicates using sockets.

If I am using read / write functions in C programming, do I have to be concerned with byte ordering if I am sending integers, or integer arrays over?

I do know that the due to the byte ordering for different systems that may use little or big endian.  But I also heard that some integer numbers are generally the same whether it's little/big endian.

Any comments?
0
Comment
Question by:YamSeng
4 Comments
 
LVL 3

Expert Comment

by:3rsrichard
ID: 6482505
>>>I'm writing a network program that communicates using sockets.

If you are writing a program to talk to some existing protocol, then you
certainly need to comply with its rfc.
If you are sending data to yourself then you just need to be consistent
from end-to-end.

Most things you send, say through a program like this, may be encoded and
reencoded several times using ASN, BER and PPP.  The transporter, UDP
or TCP, don't care how the individual bytes are arranged or interpreted.
0
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6483234
YamSeng,

If you are using a stream socket, you can be assured that you will be receiving/sending the information in host byte order (the socket itself will do the conversion from host to network byte order and vice-versa).  You will need to pay a little more attention if you are using raw sockets since you would be bypassing the byte conversion algorithms.  In this case, you should look at the htons (host to network short), htonl, ntohs, and ntohl functions.  These are very useful functions for doing byte order conversions (network byte order is always big endian, so these will only do a conversion if you are on a little endian machine, a nice feature to take advantage of).

If you are concerned about portability between big and little endian platforms, you will need to be especially careful of bitwise comparisons using constants, bitwise shifting, and pretty much any other bitwise comparison.  If you find yourself doing these a lot, it would be wise to force a byte ordering (with htonl or htons), such that the comparisons will be done in the same byte order, regardless of platform.
0
 
LVL 1

Author Comment

by:YamSeng
ID: 6484038
BlackDiamond,

I create my socket using
socket(AF_INET, SOCK_STREAM, 0);

Does that means I don't have to care about this byte ordering thing?

But I've got a book here on socket programming, it advises to convert to string before sending over and converting back to the data type at the other end.

The other method is explicitly define the binary formats of the supported datatypes and pass all data between client and server in this format. (similar to RPC)

This book also uses SOCK_STREAM on it's sockets.
0
 
LVL 1

Accepted Solution

by:
jimmy007 earned 20 total points
ID: 6486420
depends what you carry and on which plattform your application support!!!

software for i386 plattform only or PPC only you don't care. Sender and receiver have the same architecture.

plattform independant:

1 - if you send and read bytes it's OK

2 - if you use word or int then you must use byte ordering macros:

for instance, let's say you have a structure:

typedef struct _test{
    int i1;
    byte b1;
} test;

test data1;
data1.i1 = 0x01020304
data1.b1 = 0x5

if you don't care about byte ordering and you send this information from Intel computer to a MAC then on the other hand you will get:

data1.i1 will be 0x04030201 --> corrupted
data1.b1 remain the same

before seding it you must use: htonl or htons; stand for Host TO Network Long / Host TO Network Short

on reception you use: ntohl or ntohs

sender:

htonl(data1.i1) then memcpy to your buffer and write to the created socket.

receiver:

read the socket, memcpy into the same structure then use ntohl(data1.i1) before using it

I hope this will help you



0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now