Traditional Sockets

Two types of sockets are in general use: TCP sockets and UDP sockets (SOCK_STREAM and SOCK_DGRAM respectively). A “raw” socket is also available that is used for specialized applications such as “ping” and “traceroute” (tracert).

In the beginning, the network programmer had to deal with the structures in Listing 12.1 just to establish a connection.

Listing 12.1. socket Address Structures
struct sockaddr {
      unsigned short sa_family; // address family, AF_xxx
      char sa_data[14];       // 14 bytes of protocol address
};
struct sockaddr_in {
      short int sin_family;        // Address family
      unsigned short int sin_port; // Port number
      struct in_addr sin_addr;     // Internet address
      unsigned char sin_zero[8];   // Same size as struct sockaddr
} ;

struct in_addr {
      unsigned long s_addr;
} ;

In addition, because this was a brave new world in which different machines using different processors were not communicating, you needed to remember Network Byte Order. Remember the following?

  • htons()— “Host to Network Short”

  • htonl()— “Host to Network Long”

  • ntohs()— “Network to Host Short”

  • ntohl()— “Network to Host Long”

If that was not enough, the pattern that was established for client/server applications was to use fork when a client connection was accepted on the server. In addition, when asynchronous operation was desired, the common approach was to use select. The problem that Windows programmers faced was that Windows did not have the Unix APIs of fork or select for client/server applications, so it was hard for a Windows programmer to enter the world of network programming. As an answer to this problem, Microsoft developed WinSock.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.1.136