Abstract
- Bidirectional communication channels that can be used by a lot of Process (进程) simultaneously - A process can accept many connections on the same socket and attend several Client simultaneously
- There are 2 main types of socket - Unix Domain Socket & Network Domain Socket
Network Domain Socket | Unix Domain Socket | |
---|---|---|
Scope | Network-wide communication | Communication within the same machine |
Addressing | IP Address + Network Port | Filesystem path to socket file |
Protocols | TCP/IP (TCP, UDP, etc) | Unix-specific IPC protocols |
Overhead | Higher due to network layers | Lower, everything happens on the same machine, no network overhead |
Security | More exposed, may accidentally expose the connection to public network | More restricted with file permissions & contained within the machine |
Performance | Slow | Fast |
Unix Domain Socket
- Socket in Unix-like system that provides Inter-Process Communication (IPC) using File Descriptor, where the Kernel handles the System Call (系统调用) and put the mechanism behind a Abstraction Barrier
Important
Runs entirely in Main Memory, so it is extremely fast to perform read and write to the unix domain socket file.
Info
Unix Domain Socket Lifecycle
- The above diagram show the System Call (系统调用) used to implement Socket
Server Process
socket()
- creates a new Socket & returns a File Descriptor that can be used to refer to the socket in future system callsbind()
- bind the socket to a Pathname, so that the client can connect to itlisten()
- mark the socket as passive, so the socket that will accept incoming connection requestsaccept()
- accept an incoming connection. This call blocks until a connection request arrives. Note that this function will error out iflisten()
is not called beforehand. Importantly, this call creates a new socket that is connected to the peer socket(peer socket = the socket at the other end of the connection, in this case, socket created by client process) and returns a file descriptor associated with it. So, if you want to communicate with the peer socket, you should use the file descriptor returned fromaccept()
, not the file descriptor returned from the call tosocket()
. The latter socket remains open and is used to accept further connection requests
Client Process
socket()
- creates a new Socket & returns a File Descriptor that can be used to refer to the socket in future system calls. Note that by default, a socket created usingsocket()
is marked as active, and can be used in aconnect()
call to connect to a passive socketconnect()
- connects to a passive socket on the pathname specified when the server performs thebind()
. Note thatconnect()
should be called afterlisten()
is called on the server socket, otherwise it will error. However, it can be called beforeaccept()
Communication takes place
read()
write()
End
close()
Note
Server socket can accept many client connections at the same time.
C Code Example
You can refer to this Medium Article to implement UNIX Domain Socket on both the client and server using C.
Code
You can use
find / -type s
to list down all the unix domain socket files on the system.
Unix Domain Socket with Nodejs
Abstract
We have 2 Nodejs files. One file is the client process and another one is server process. Client process passes dummy text to the server process by writing to
./server.sock
, and the server process simply prints out what it reads from./server.sock
to the screen.
- Socket server process
- Unix domain socket client process
Network Domain Socket
- The Socket Client & Server use to communicate over the Computer Network