• Loading
    • IPC in UNIX Using Sockets

      Sockets
      Sockets are one of the versatile IPC mechanisms in Unix. Sockets allow point-to-point, two-way communication between processes. Unix sockets are similar to two-way FIFOs. When processes communicate using sockets, data communication occurs using the Unix socket interface. The socket interface is the de-facto standard for communication protocols using sockets. Sockets are designed to be used with different protocols, such as Transmission Control Protocol/Internet Protocol (TCP/IP).

      A socket is the endpoint of communication. Each socket has a unique name, a type, and one or more associated processes. Sockets exist in communication domains. A socket domain is an abstraction containing an addressing structure and a set of protocols.

      Sockets can be connected with other sockets that belong to the same domain. All the prototypes and definitions related to sockets are provided in the <sys/socket.h> system header file. Of the twenty-three socket domains, only the Unix and Internet domains are used normally. The Unix socket domain can be used to communicate with processes on the same Unix system. The Unix domain provides a socket address space on a single system. Unix domain sockets are named with Unix paths. The syntax of a Unix socket struct is:

      Code:
      struct sockaddr_un
      {
          unsigned short sun_family;  /* AF_UNIX */
          char sun_path[108];
      }
      The socket structure is passed to the bind() system function. This function associates a socket descriptor with a certain file that is specified in the sun_path field.

      Creating and Naming a Socket

      You can create a socket by calling the socket() system function. The syntax of the function is:

      Code:
      int socket(int domain, int type, int protocol)
      This function is used to create a socket in the specified domain and of the specified type. A protocol is also specified in the function. If the protocols are not specified, the system uses the default protocol that supports the specified socket type. The function returns a socket handle. You use the bind() method to bind the socket to an address so that the socket can be identified by a remote process. The syntax of the bind() system function is:

      Code:
      int bind(int s, const struct sockaddr *name, int namelen)
      The bind() function can be called in three different ways, depending on the socket domain. For Unix domain sockets with a path containing fourteen or fewer characters, you can use:

      Code:
      #include <sys/socket.h>
      bind (sd, (struct sockaddr *) &addr, length);
      If the path of a Unix domain socket requires more than fourteen characters, you can use:

      Code:
      #include <sys/un.h>
      bind (sd, (struct sockaddr_un *) &addr, length);
      For Internet domain sockets, you can use:

      Code:
      #include <netinet/in.h>
      bind (sd, (struct sockaddr_in *) &addr, length);
      In the Unix domain, binding a name creates a named socket in the file system. You can use the unlink() or the rm() system function to remove the socket.

      Socket Types
      The socket type determines the socket communication properties, such as reliability, sequencing, and preventing duplication of messages. The basic socket types defined in the <sys/socket.h> system header file are:

      • Stream socket: Provides two-way, sequential, reliable, and unduplicated flow of data without any record boundaries. The socket type is SOCK_STREAM.
      • Datagram socket: Allows a two-way flow of messages. The sequence of the data on the datagram socket is not guaranteed. The record boundaries in the data are preserved. The socket type is SOCK_DGRAM.
      • Sequential packet socket: Provides a two-way, sequential, and reliable connection for datagrams of a fixed maximum length. The socket type is SOCK_SEQPACKET.
      • Raw socket: It provides access to the new communication protocols.


      Application of Sockets
      Sockets communicate with each other using one of these communication models:

      • Client-server model: Where a client process sends a request and the server process listens and services this request
      • Peer-to-peer networking model: Where the same process can act as a server process or a client process
      • Remote procedure calls (RPC): Where a process is called by the a remote application as a procedure


      Processes in a client-server model communicate using sockets. To begin the communication, a socket for the client is connected to a socket for the server. Before the communication can occur, both the client and the server have to be designed. Designing a server includes:

      • Creating the socket.
      • Naming the socket.
      • Attaching a connection to the socket.
      • Transferring data using the socket.
      • Deallocating the socket after use.



      Designing a client includes:
      • Creating the socket.
      • Attaching a connection.
      • Transferring data using the socket.
      • Deallocating the socket after use.



      Note: It is not always necessary to assign a name to a socket for the client.

      Comparison of Sockets for Different Operating Systems

      The key differences between the sockets implemented in Unix and those implemented in Windows are:

      The header files required by Unix sockets include:

      Code:
          #include <stdio.h>
          #include <sys/socket.h>
          #include<arpa/inet.h>
          #include<stdlib.h>
          #include<string.h>
          #include<unistd.h>
      The header files required by Windows OS are:

      Code:
          #include <stdio.h>
          #include <winsock.h>
          #include<stdlib.h>
      The application setup of sockets is similar in Windows and Unix OS except that the initialization code required in WinSock of Windows OS is different from that required in the Unix socket.

      The communication part of the sockets is identical in both Unix and Windows OSs.

      The shutdown of an application in Unix and Windows is different. Unix uses the close() system function to close a socket. Windows uses the closesocket() system function to close a socket. In addition, Windows use a different error-reporting facility than the one used in Unix.
    • Currently Active UsersCurrently Active Users

      There are currently 80 users online. 3 members and 77 guests

      Most users ever online was 323, 11-23-2011 at 07:47 AM.

      1. airncnimxkl,
      2. airnqpzarcl,
      3. vbairmaclsho