stupid TCP

J

Joe Van Dyk

require 'socket'

tcp_server =3D TCPServer.new 'localhost', 4321

puts "connecting..."
tcp_client =3D TCPSocket.new 'localhost', 4321
tcp_client.write "Hello World"
puts "finshed writing"

session =3D tcp_server.accept
puts "we recieved: <#{session.gets}>"



Why doesn't that work?
 
J

Joe Van Dyk

require 'socket'
=20
tcp_server =3D TCPServer.new 'localhost', 4321
=20
puts "connecting..."
tcp_client =3D TCPSocket.new 'localhost', 4321
tcp_client.write "Hello World"
puts "finshed writing"
=20
session =3D tcp_server.accept
puts "we recieved: <#{session.gets}>"
=20
=20
=20
Why doesn't that work?

Or, a threaded version:

require 'socket'

t =3D Thread.new do=20
tcp_server =3D TCPServer.new 'localhost', 4321
while (session =3D tcp_server.accept)
puts "we recieved: <#{session.gets}>"
end
end

tcp_client =3D TCPSocket.new 'localhost', 4321
tcp_client.write "Hello World"

t.join

Still doesn't work though. :( What am I missing?
 
J

Joe Van Dyk

=20
Or, a threaded version:
=20
require 'socket'
=20
t =3D Thread.new do
tcp_server =3D TCPServer.new 'localhost', 4321
while (session =3D tcp_server.accept)
puts "we recieved: <#{session.gets}>"
end
end
=20
tcp_client =3D TCPSocket.new 'localhost', 4321
tcp_client.write "Hello World"
=20
t.join
=20
Still doesn't work though. :( What am I missing?

Aha. I'm guessing it has something to do with not sending the data
immediately. If I do

tcp_client.close

Then the data gets sent. But I don't want to close the socket. And
I'm not seeing anything (yet) that would let me immediately send (I
tried IO#flush) the data over the socket.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top