Skip to content

Instantly share code, notes, and snippets.

@jasonmadigan
Created February 4, 2011 11:30
Show Gist options
  • Save jasonmadigan/811011 to your computer and use it in GitHub Desktop.
Save jasonmadigan/811011 to your computer and use it in GitHub Desktop.
udp_server.rb
#!/usr/bin/ruby
require 'socket.so'
class UDPServer
def initialize(port)
@port = port
end
def start
@socket = UDPSocket.new
@socket.bind('', @port) # Bind to 0.0.0.0
while true
packet = @socket.recvfrom(1024)
puts packet
end
end
end
server = UDPServer.new(54321)
server.start
#!/usr/bin/ruby
require 'socket.so'
class UDPClient
def initialize(host, port)
@host = host
@port = port
end
def start
@socket = UDPSocket.open
@socket.connect(@host, @port)
while true
@socket.send("KNOCK KNOCK", 0, @host, @port)
sleep 2
end
end
end
client = UDPClient.new("1.2.3.4", 54321)
client.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment