Socket Data Transfer

Most of the work performed by the socket layer is in sending and receiving data. The socket layer itself explicitly refrains from imposing any structure on data transmitted or received through sockets.

Any data interpretation or structuring is logically isolated in the implementation of the communication domain.

Once a connection is established between sockets, an application program can send and receive data. Sending and receiving data can be done with any one of several subroutines. The subroutines vary according to the amount of information to be transmitted and received and the state of the socket being used to perform the operation.

  • The write subroutine can be used with a socket that is in a connected state, as the destination of the data is implicitly specified by the connection.
  • The sendto and sendmsg subroutines allow the process to specify the destination for a message explicitly.
  • The read subroutine allows a process to receive data on a connected socket without receiving the sender's address.
  • The recvfrom and recvmsg subroutines allow the process to retrieve the incoming message and the sender's address.

The applicability of the above subroutines varies from domain to domain and from protocol to protocol.

Although the send and recv subroutines are virtually identical to the read and write subroutines, the extra flags argument in the send and recv subroutines is important. The flags, defined in the sys/socket.h file, can be defined as a nonzero value if the application program requires one or more of the following:
Item Description
MSG_OOB Sends or receives out-of-band data.
MSG_PEEK Looks at data without reading.
MSG_DONTROUTE Sends data without routing packets.
MSG_MPEG2 Sends MPEG2 video data blocks.

Out-of-band data is specific to stream sockets. The option to have data sent without routing applied to the outgoing packets is currently used only by the routing table management process, and is unlikely to be of interest to the casual user. The ability to preview data is, however, of general interest. When the MSG_PEEK flag is specified with a recv subroutine, any data present is returned to the user, but treated as still unread. That is, the next read or recv subroutine applied to the socket returns the data previously previewed.