CS558 Sylabus

### Sending and receiving binary numbers

• Storing integer (or any other type of) binary numbers

• Computers store binary numbers in one of 2 possible order:

 Big endian ordering Little endian ordering

Graphically:

• Host byte ordering:

 Host byte ordering =   the storage ordering of binary numbers used in a computer (= host)

• Transmiting integer (or any other type of) binary numbers

• Fact:

• The function call:

will transmit the len bytes (stored) in the variable data in the order stored in (host) memory

• What happens if we transmit binary data between a big endian computer and a little endian computer

• Consider the following data transmission of an integer binary number from little endian computer to a big endian computer:

• A little endian computer send the integer value 2 to a big endian computer

• The big endian computer will interpret the integer binary number as follows:

• Example Program: (Demo above code)

• Prog file:

 /home/cs455001/demo/netw-prog-ipv4/udp-send-int.c /home/cs455001/demo/netw-prog-ipv4/udp-recv-int.c

How to run the program:

• To compile:

 gcc -o udp-send-int udp-send-int.c on W301 (or a lab machine) gcc -o udp-recv-int udp-recv-int.c -lnsl -lsocket on compute

• To run:

 On compute:   udp-recv-int 8000 On W301:       udp-send-int compute 8000

• How to transmit binary data

• Technique to transmit binary data:

• Sender:

 First, convert the binary data to network byte ordering Then, send the converted data

Example:

 int k = 1; // k is stored in host byte order printf("Sending: int value = %d\n", k); int x = htonl(k); // Convert to network byte ordering sendto(s, &x, sizeof(int), 0 /* flags */, (struct sockaddr *)&dest_addr, sizeof(dest_addr));

• Receiver the binary data (we know it is in network byte order

• Convert the data to host byte order using:

 int ntohl( int ): convert 4 byte data from network byte ordering to host byte ordering short ntohs( short): convert 2 byte data from network byte ordering to host byte ordering

Example:

 int x; src_addr_len = sizeof(src_addr); len = recvfrom(s, &x, sizeof(int), 0 /* flags */, (struct sockaddr *) &src_addr, &src_addr_len); k = ntohl( x ); // Convert int from network byte ordering // to host byte ordering printf("k = %d", k);

• Example Program: (Demo above code)

• Prog file:

• Edit the same demo programs:

 /home/cs455001/demo/netw-prog-ipv4/udp-send-int.c /home/cs455001/demo/netw-prog-ipv4/udp-recv-int.c

Uncomment the htonl( ) and ntohl( ) calls in the code

How to run the program:

• To compile:

 gcc -o udp-send-int udp-send-int.c on W301 (or a lab machine) gcc -o udp-recv-int udp-recv-int.c -lnsl -lsocket on compute

• To run:

 On compute:   udp-recv-int 8000 On W301:       udp-send-int compute 8000

• Transmitting structured data

• Fact:

• Structured data can contain:

 String/text data   (stored in the same order in both big endian computer and little endian computer) integer/float binary data   (stored in different order in big endian computer and little endian computer)

• How to transmit structured data:

• Transmitting the structured data:

 Use htonl( ) (or htons( )) to convert every integer/float variable to network byte order Send the converted struct using sendto( )

• How to receive structured data:

• Transmitting the structured data:

 Use recvfrom( ) to receive the transmitted struct data Use ntohl( ) (or ntohs( )) to convert every integer/float variable to host byte order

• Example Program: (Demo above code)

• Prog file:

 /home/cs455001/demo/netw-prog-ipv4/udp-send-struct.c /home/cs455001/demo/netw-prog-ipv4/udp-recv-struct.c

How to run the program:

• To compile:

 gcc -o udp-send-struct udp-send-struct.c on W301 (or a lab machine) gcc -o udp-recv-struct udp-recv-struct.c -lnsl -lsocket on compute

• To run:

 On compute:   udp-recv-struct 8000 On W301:       udp-send-struct compute 8000