Unix Socket – Network Byte Orders ”; Previous Next Unfortunately, not all computers store the bytes that comprise a multibyte value in the same order. Consider a 16-bit internet that is made up of 2 bytes. There are two ways to store this value. Little Endian − In this scheme, low-order byte is stored on the starting address (A) and high-order byte is stored on the next address (A + 1). Big Endian − In this scheme, high-order byte is stored on the starting address (A) and low-order byte is stored on the next address (A + 1). To allow machines with different byte order conventions communicate with each other, the Internet protocols specify a canonical byte order convention for data transmitted over the network. This is known as Network Byte Order. While establishing an Internet socket connection, you must make sure that the data in the sin_port and sin_addr members of the sockaddr_in structure are represented in Network Byte Order. Byte Ordering Functions Routines for converting data between a host”s internal representation and Network Byte Order are as follows − Function Description htons() Host to Network Short htonl() Host to Network Long ntohl() Network to Host Long ntohs() Network to Host Short Listed below are some more detail about these functions − unsigned short htons(unsigned short hostshort) − This function converts 16-bit (2-byte) quantities from host byte order to network byte order. unsigned long htonl(unsigned long hostlong) − This function converts 32-bit (4-byte) quantities from host byte order to network byte order. unsigned short ntohs(unsigned short netshort) − This function converts 16-bit (2-byte) quantities from network byte order to host byte order. unsigned long ntohl(unsigned long netlong) − This function converts 32-bit quantities from network byte order to host byte order. These functions are macros and result in the insertion of conversion source code into the calling program. On little-endian machines, the code will change the values around to network byte order. On big-endian machines, no code is inserted since none is needed; the functions are defined as null. Program to Determine Host Byte Order Keep the following code in a file byteorder.c and then compile it and run it over your machine. In this example, we store the two-byte value 0x0102 in the short integer and then look at the two consecutive bytes, c[0] (the address A) and c[1] (the address A + 1) to determine the byte order. #include <stdio.h> int main(int argc, char **argv) { union { short s; char c[sizeof(short)]; }un; un.s = 0x0102; if (sizeof(short) == 2) { if (un.c[0] == 1 && un.c[1] == 2) printf(“big-endiann”); else if (un.c[0] == 2 && un.c[1] == 1) printf(“little-endiann”); else printf(“unknownn”); } else { printf(“sizeof(short) = %dn”, sizeof(short)); } exit(0); } An output generated by this program on a Pentium machine is as follows − $> gcc byteorder.c $> ./a.out little-endian $> Print Page Previous Next Advertisements ”;
Category: Unix Sockets
Unix Socket – Ports and Services ”; Previous Next When a client process wants to a connect a server, the client must have a way of identifying the server that it wants to connect. If the client knows the 32-bit Internet address of the host on which the server resides, it can contact that host. But how does the client identify the particular server process running on that host? To resolve the problem of identifying a particular server process running on a host, both TCP and UDP have defined a group of well-known ports. For our purpose, a port will be defined as an integer number between 1024 and 65535. This is because all port numbers smaller than 1024 are considered well-known — for example, telnet uses port 23, http uses 80, ftp uses 21, and so on. The port assignments to network services can be found in the file /etc/services. If you are writing your own server then care must be taken to assign a port to your server. You should make sure that this port should not be assigned to any other server. Normally it is a practice to assign any port number more than 5000. But there are many organizations who have written servers having port numbers more than 5000. For example, Yahoo Messenger runs on 5050, SIP Server runs on 5060, etc. Example Ports and Services Here is a small list of services and associated ports. You can find the most updated list of internet ports and associated service at IANA – TCP/IP Port Assignments. Service Port Number Service Description echo 7 UDP/TCP sends back what it receives. discard 9 UDP/TCP throws away input. daytime 13 UDP/TCP returns ASCII time. chargen 19 UDP/TCP returns characters. ftp 21 TCP file transfer. telnet 23 TCP remote login. smtp 25 TCP email. daytime 37 UDP/TCP returns binary time. tftp 69 UDP trivial file transfer. finger 79 TCP info on users. http 80 TCP World Wide Web. login 513 TCP remote login. who 513 UDP different info on users. Xserver 6000 TCP X windows (N.B. >1023). Port and Service Functions Unix provides the following functions to fetch service name from the /etc/services file. struct servent *getservbyname(char *name, char *proto) − This call takes service name and protocol name, and returns the corresponding port number for that service. struct servent *getservbyport(int port, char *proto) − This call takes port number and protocol name, and returns the corresponding service name. The return value for each function is a pointer to a structure with the following form − struct servent { char *s_name; char **s_aliases; int s_port; char *s_proto; }; Here is the description of the member fields − Attribute Values Description s_name http It is the official name of the service. For example, SMTP, FTP POP3, etc. s_aliases ALIAS It holds the list of service aliases. Most of the time, it will be set to NULL. s_port 80 It will have the associated port number. For example, for HTTP, it will be 80. s_proto TCP UDP It is set to the protocol used. Internet services are provided using either TCP or UDP. Print Page Previous Next Advertisements ”;