Skip to content

Instantly share code, notes, and snippets.

@reconbot
Last active Dec 29, 2015
Embed
What would you like to do?
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!
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
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
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