You will use SOCK_DGRAM on a local socket when you want to preserve message boundaries. Again, no specific protocol type is permitted for PF_LOCAL domain sockets at this time. Take a look at the following example:
Example
int s;
s = socket(PF_LOCAL,SOCK_DGRAM,0);
if ( s == -1 )
{
perror("socket()");
}
The steps used to create this local datagram socket are
- Integer s is declared to receive the socket number (it is treated the same as a file descriptor).
- The socket(2) function is called. The domain argument is set to PF_LOCAL, and the socket type argument is set to SOCK_DGRAM to request a datagram socket. The protocol argument is set to zero, which is the only valid value for PF_LOCAL sockets.
- The value s is tested to see whether it is the value -1. If it is, then an error has occurred, and errno has the reason for it. Function perror(3) is used in this example to report what the errno code indicates.
4. If s is not -1, then it represents a valid socket.
Datagram sockets are attractive to use for PF_LOCAL sockets because they are mostly reliable and they preserve message boundaries. They don't get lost in network transmission errors as PF_INET datagrams can, because they remain internal to the local host. However, you should assume that kernel buffer shortages might cause PF_LOCAL packets to be lost, even if this rarely occurs.
NOTE
When a socket is created, it is nameless (without an address). A valid socket address must be set up and the function bind(2) called to give the socket an address.
No comments:
Post a Comment