Saturday, August 6, 2011

SOCK_SEQPACKET Socket Type

Although the SOCK_SEQPACKET type will not be used very frequently in software development and studies, I perhaps you should be least become familiar with it.
This socket type is important for protocols such as X.25 and AX.25 that use it. It is very similar to SOCK_STREAM but has one subtle distinction. The difference is that although the SOCK_STREAM socket does not preserve message boundaries, the SOCK_SEQPACKET does. When X.25 is used, for example, and SOCK_SEQPACKET is chosen, each packet is received in the same unit size in which it was originally written. For example, imagine the sending end performing the following two writes:
  1. Write a message one of 25 bytes.
  2. Write a message two of 30 bytes.
Although the receiving process might indicate that it can accept up to 256 bytes in one read(2) call, the following receive events will occur:
  1. A message will be received with a length of 25 bytes. This corresponds to the length of the first message that was written by the sending process.
  2. A second message will be received with a length of 30 bytes. This corresponds to the length of the second write of the sending process.
Although the receiving buffer was able to receive the total combined message length of 55 bytes, only the first message for 25 bytes is received by the first read(2) call on the socket. This tells the application that this message was precisely 25 bytes in length. The next call to read(2) will fetch the next message of 30 bytes, regardless of whether there is more data that could be returned.
With this behavior, you can see that SOCK_SEQPACKET preserves the original message boundaries. The following provides a summary of characteristics for this socket type:
  • Message boundaries are preserved. This feature distinguishes the SOCK_SEQPACKET type from the SOCK_STREAM type.
  • The data bytes received are guaranteed to be in precisely in the same order in which they were written.
  • All data written is guaranteed delivered to the remote end without error. If it cannot be delivered after reasonable attempts at automatic recovery, an error is reported to the sending and receiving processes.
  • The data is transported over a pair of connected sockets.
NOTE
Not all socket types can be used with all protocols. For example, SOCK_STREAM is supported for the PF_INET protocol family, but SOCK_SEQPACKET is not. Conversely for PF_X25, the socket type SOCK_SEQPACKET is supported, but SOCK_STREAM is not.


Sample Code using SOCK_SEQPACKET

No comments:

Post a Comment