package
0.0.0-20240816111131-174649f5e035
Repository: https://github.com/ericwq/examples.git
Documentation: pkg.go.dev
# README
Reference
- The evolution of reuseport in the Linux kernel
- 跨进程复制socket
- Linux网络编程“惊群”问题总结
- accept 与 epoll 惊群
- Advanced Programming in the UNIX Environment. 3rd Edition, Page 642, 17.4 Passing File Descriptors
- UNIX Network Programming - The Sockets Networking API. Volume 1, 3rd Edition, Page 420, 15.7 Passing Descriptors
- UNIX Network Programming - The Sockets Networking API. Volume 1, 3rd Edition, Page 829, 4.4 BSD Implementation
Demo steps
Run the following command to build the binaraies.
go build -o demo main.go
Run the following command to start the first server.
./demo
You should get the following output:
$ ./demo
listen on: [::]:7000
Listening on /tmp/unix_socket_tcp
[49451] 2021-11-06 21:43:55.0125327 +0800 CST m=+4.472036001 recv msg is: hello
[49451] 2021-11-06 21:43:55.1211736 +0800 CST m=+4.580667201 recv msg is: hello
Run the following command to start the client. The client will keep sending hello
message to localhost:7000
while true; do nc localhost 7000 -e echo hello; sleep 0.1; done
Run the following command to start the second server.
./demo 2
You should get the following output:
$ ./demo 2
recv fd 7
listen on: [::]:7000
[49591] 2021-11-06 21:44:02.6632161 +0800 CST m=+1.219149001 recv msg is: hello
[49591] 2021-11-06 21:44:02.8883711 +0800 CST m=+1.444308201 recv msg is: hello
[49591] 2021-11-06 21:44:03.0006343 +0800 CST m=+1.556868001 recv msg is: hello
You can run more servers, if you like it.
$ ./demo 3
recv fd 7
listen on: [::]:7000
[49778] 2021-11-06 21:44:13.1593171 +0800 CST m=+1.555800001 recv msg is: hello
[49778] 2021-11-06 21:44:13.6133832 +0800 CST m=+2.009869001 recv msg is: hello
[49778] 2021-11-06 21:44:13.717499 +0800 CST m=+2.113980301 recv msg is: hello
Now, watch the output from the client and 3 servers. The same socket is shared by 3 processes.
# Functions
No description provided by the author