Skip to content

Instantly share code, notes, and snippets.

@rickhull
Forked from tenderlove/gist:606809
Created October 2, 2010 07:05
Show Gist options
  • Save rickhull/607395 to your computer and use it in GitHub Desktop.
Save rickhull/607395 to your computer and use it in GitHub Desktop.
require 'socket'
require 'rubygems'
require 'nokogiri'
PORT = rand(65535 - 1024) + 1024
sthread = Thread.new {
s = TCPServer.new(PORT).accept
puts "[SERVER] Client connected."
s.write('<foo>')
5.times {
sleep rand(0.0)
s.write('<test>')
s.write('DATA' * 1000)
s.write('</test>')
puts "[SERVER] Sent a chunk."
}
s.write('</foo>')
puts "[SERVER] Document transmitted."
puts "[SERVER] Holding the socket open..."
5.downto(1) { |count|
puts "[SERVER] #{count}"
sleep 1
}
s.close
puts "[SERVER] Closed the socket."
}
c = TCPSocket.new('', PORT)
Nokogiri::XML::Reader.from_io(c).each { |node|
puts node.name #"[CLIENT] Received <#{node.name}>"
}
puts "Document received."
sthread.join
[SERVER] Client connected.
[SERVER] Sent a chunk.
foo
test
#text
test
test
[SERVER] Sent a chunk.
#text
test
test
[SERVER] Sent a chunk.
#text
test
test
[SERVER] Sent a chunk.
#text
test
test
[SERVER] Sent a chunk.
[SERVER] Document transmitted.
[SERVER] Holding the socket open...
[SERVER] 5
[SERVER] 4
[SERVER] 3
[SERVER] 2
[SERVER] 1
[SERVER] Closed the socket.
#text
test
foo
Document received.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment