Created
May 30, 2018 20:13
-
-
Save nixpulvis/66d4d0103487236d2a89f0578c2c058d to your computer and use it in GitHub Desktop.
SO_REUSEPORT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "socket" | |
PORT = 1234 | |
server = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM) | |
# NOTE: Commentting out this line will prevent multiple processes to run at | |
# once on the same port. | |
server.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEPORT, true) | |
server.bind(Addrinfo.tcp("", PORT)) | |
server.listen(0) | |
loop do | |
client, addr = server.accept | |
data = client.recv(1024) | |
client.send(data, 0) | |
client.close | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment