Last active
October 1, 2020 18:23
-
-
Save cmol/ffe25bc002a425ef12f6f8c608621975 to your computer and use it in GitHub Desktop.
This file contains 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" | |
LISTEN_ADDR = "::" | |
LISTEN_PORT = 12345 | |
MSG_LENGTH = 256 | |
FLAGS = 0 | |
# Create socket and bind it to the listen on all addresses and the given port | |
server_socket = UDPSocket.new :INET6 | |
server_socket.bind(LISTEN_ADDR, LISTEN_PORT) | |
loop do | |
# Listen for messages of up to specified length | |
message, client = server_socket.recvfrom(MSG_LENGTH) | |
# Extract client information given as array and log connection | |
addr_info = Addrinfo.new(client) | |
puts "Client connected from #{addr_info.ip_address} using " + | |
"#{addr_info.ipv6_v4mapped? ? "IPv4" : "IPv6"}" | |
# Write back to client with AddressFamily and reversed original message | |
server_socket.send("[#{addr_info.ipv6_v4mapped? ? "IPv4" : "IPv6"}]" + | |
" #{message.chomp.reverse}", | |
FLAGS, addr_info.ip_address, addr_info.ip_port) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment