Skip to content

Instantly share code, notes, and snippets.

@jmlacroix
Created October 30, 2012 19:08
Show Gist options
  • Save jmlacroix/3982311 to your computer and use it in GitHub Desktop.
Save jmlacroix/3982311 to your computer and use it in GitHub Desktop.
Crash Course sur les Sockets - Présentation OpenCode 30 Octobre 2012
require 'socket'
# request through ruby TCPSocket
s = TCPSocket.open 'localhost', 4567
s.write "GET / HTTP/1.0\r\n\r\n"
s.read
s.close
# request through 'normal' Socket
include Socket::Constants
s = Socket.new AF_INET, SOCK_STREAM
addr = Socket.pack_sockaddr_in(4567, '127.0.0.1')
s.connect addr
s.send "GET / HTTP/1.0\r\n\r\n", 0
s.recv 1024
s.close
# port scanner
#(1..65535).each do |p|
[ 1, 79, 80, 81, 630, 631, 632, 3689, 4567, 9393, 27017, 65535 ].each do |p|
begin
TCPSocket.new 'localhost', p
puts "port #{p} open"
rescue
puts "port #{p} closed"
end
end
# scan subnet for http ports
#ip = '192.168.0.'
ips = Socket.ip_address_list.select do |a|
a.ip_address if a.ipv4? && !a.ipv4_loopback?
end
ip = ips.first.ip_address.to_s.split('.')[0..2].join('.') + '.'
ts = []
(1..255).each do |i|
ts << Thread.new {
begin
TCPSocket.new ip + i.to_s, 80
puts ip + i.to_s
rescue
end
}
end
ts.each { |t| t.join }
# web server
server = TCPServer.new 2000
loop do
client = server.accept
client.puts "HTTP/1.1 200 OK \r\nContent-Type: text/html;charset=utf-8\r\nContent-Length: 4\r\nConnection: close\r\n\r\nhey!"
client.close
end
server.close
# http proxy server
server = TCPServer.new 2000
loop do
puts "waiting"
client = server.accept
req = client.recvfrom(1024)[0]
puts "new connection, forwarding request"
real = TCPSocket.new 'localhost', 4567
real.write req
client.write real.read
real.close
client.close
puts "done"
end
server.close
# http proxy client
s = TCPSocket.new 'localhost', 2000
s.write "GET / HTTP/1.0\r\n\r\n"
s.read
# ---
addr = Socket.pack_sockaddr_in 2203, '127.0.0.1'
# blocking tcp server
s = Socket.new AF_INET, SOCK_STREAM
s.bind addr
s.listen 5
c, c_addr = s.accept
puts "waiting for data"
puts c.recv(7)
puts c.recv(4)
c.close
s.close
# tcp client
s = Socket.new AF_INET, SOCK_STREAM
s.connect addr
s.puts "Hello World!"
s.close
# non blocking tcp server
s = Socket.new AF_INET, SOCK_STREAM
s.bind addr
s.listen 5
begin
c, c_addr = s.accept_nonblock
rescue
puts 'nothing, wait'
sleep 1
retry
end
puts c.recv(7)
puts c.recv(4)
c.close
s.close
# udp server
s = UDPSocket.new
s.bind '127.0.0.1', 2000
s.send 'Hello Self!', 0, '127.0.0.1', 2000
p s.recvfrom(11)[0]
s.close
# udp packet test
s = UDPSocket.new
s.bind '127.0.0.1', 2001
heys = 0
t = Thread.new {
loop do
s.recv 5
heys += 1
end
}
10000.times { s.send "hey", 0, '127.0.0.1', 2001 }
sleep 1
puts heys
t.exit
s.close
# unix sockets
s1, s2 = UNIXSocket.pair
def sayhi s, count
count.times do
s.send "hi", 0
puts s.recv 10
end
end
def sayho s, count
count.times do
puts s.recv 10
s.send "ho", 0
end
end
t = Thread.new { sayhi s2, 10 }
sayho s1, 10
t.join
s1.close
s2.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment