Source From Here
Communication within a host
UNIX domain sockets are a method by which processes on the same host can communicate. Communication is bidirectional with stream sockets and unidirectional with datagram sockets. Below is the sample code in C:
Identity
Instead of identifying a server by an IP address and port, a UNIX domain socket is known by a pathname. Obviously the client and server have to agree on the pathname for them to find each other. The server binds the pathname to the socket:
The resulting pathname is visible in the filesystem, like:
Unlink before bind
Note that, once created, this socket file will continue to exist, even after the server exits. If the server subsequently restarts, the file prevents re-binding:
So, servers should unlink the socket pathname prior to binding it.
File permissions control who can connect
For UNIX domain sockets, file and directory permissions restrict which processes on the host can open the file, and thus communicate with the server. Therefore, UNIX domain sockets provide an advantage over Internet sockets (to which anyone can connect, unless extra authentication logic is implemented).
Comparison with named pipes for IPC
IPC within a Unix host by may be accomplished by several other means including named pipes. What circumstances favor UNIX domain sockets versus pipes? The choice is influenced by these factors:
- Duplex
- Distinct clients
- Method of creating and opening
Linux Abstract Socket Namespace
Linux has a special feature: if the pathname for a UNIX domain socket begins with a null byte \0, its name is not mapped into the filesystem. Thus it won’t collide with other names in the filesystem. Also, when a server closes its UNIX domain listening socket in the abstract namespace, its file is deleted; with regular UNIX domain sockets, the file persists after the server closes it.
Resources
Here are some C programs that implement a UNIX domain socket client and server. These are placed in the public domain:
Supplement
* Programming->C/C++->Sockets Tutorial
Communication within a host
UNIX domain sockets are a method by which processes on the same host can communicate. Communication is bidirectional with stream sockets and unidirectional with datagram sockets. Below is the sample code in C:
- fd = socket(AF_UNIX, SOCK_STREAM, 0);
Instead of identifying a server by an IP address and port, a UNIX domain socket is known by a pathname. Obviously the client and server have to agree on the pathname for them to find each other. The server binds the pathname to the socket:
- struct sockaddr_un addr;
- memset(&addr, 0, sizeof(addr));
- addr.sun_family = AF_UNIX;
- strncpy(addr.sun_path, "socket", sizeof(addr.sun_path)-1);
- bind(fd, (struct sockaddr*)&addr, sizeof(addr));
Unlink before bind
Note that, once created, this socket file will continue to exist, even after the server exits. If the server subsequently restarts, the file prevents re-binding:
So, servers should unlink the socket pathname prior to binding it.
File permissions control who can connect
For UNIX domain sockets, file and directory permissions restrict which processes on the host can open the file, and thus communicate with the server. Therefore, UNIX domain sockets provide an advantage over Internet sockets (to which anyone can connect, unless extra authentication logic is implemented).
Comparison with named pipes for IPC
IPC within a Unix host by may be accomplished by several other means including named pipes. What circumstances favor UNIX domain sockets versus pipes? The choice is influenced by these factors:
- Duplex
- Distinct clients
- Method of creating and opening
Linux Abstract Socket Namespace
Linux has a special feature: if the pathname for a UNIX domain socket begins with a null byte \0, its name is not mapped into the filesystem. Thus it won’t collide with other names in the filesystem. Also, when a server closes its UNIX domain listening socket in the abstract namespace, its file is deleted; with regular UNIX domain sockets, the file persists after the server closes it.
Resources
Here are some C programs that implement a UNIX domain socket client and server. These are placed in the public domain:
Supplement
* Programming->C/C++->Sockets Tutorial
沒有留言:
張貼留言