-
-
Save reconbot/35f64ab10d547a4e7c2e to your computer and use it in GitHub Desktop.
This is the code for chatting. Run `broadcaster2.rb` (if you want a nickname) and `receiver.rb` in different terminal windows to join the fun!
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' | |
chat_socket = UDPSocket.new | |
# tell the OS we mean to broadcast | |
chat_socket.setsockopt(:SOCKET, :SO_BROADCAST, 1) | |
print "# Sending messages to everyone port 31337\n< " | |
while message = gets | |
chat_socket.send message, 0, '255.255.255.255', 31337 | |
print "< " | |
end |
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' | |
puts "What's your nick name?" | |
nick_name = gets.chomp | |
chat_socket = UDPSocket.new | |
# tell the OS we mean to broadcast | |
chat_socket.setsockopt(:SOCKET, :SO_BROADCAST, 1) | |
print "# Sending messages to everyone port 31337\n< " | |
while message = gets | |
chat_socket.send "<#{nick_name}> #{message}", 0, '255.255.255.255', 31337 | |
print "< " | |
end |
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' | |
chat_socket = UDPSocket.new | |
# Listen on all IP addresses on port 31337 | |
chat_socket.bind "", 31337 | |
puts "# Now listening on port 31337" | |
while message = chat_socket.gets.chomp | |
puts "> " + message | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment