Stupid TCP

require 'socket'

tcp_server = TCPServer.new 'localhost', 4321

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

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

Why doesn't that work?

Or, a threaded version:

require 'socket'

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

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

t.join

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

···

On 6/30/05, Joe Van Dyk <joevandyk@gmail.com> wrote:

require 'socket'

tcp_server = TCPServer.new 'localhost', 4321

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

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

Why doesn't that work?

Joe Van Dyk said:

Or, a threaded version:

require 'socket'

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

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

t.join

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

You are missing a newline:

tcp_client.write "Hello World\n"

gets looks for a newline.

Ryan

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.

···

On 6/30/05, Joe Van Dyk <joevandyk@gmail.com> wrote:

On 6/30/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> require 'socket'
>
> tcp_server = TCPServer.new 'localhost', 4321
>
> puts "connecting..."
> tcp_client = TCPSocket.new 'localhost', 4321
> tcp_client.write "Hello World"
> puts "finshed writing"
>
> session = tcp_server.accept
> puts "we recieved: <#{session.gets}>"
>
>
>
> Why doesn't that work?

Or, a threaded version:

require 'socket'

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

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

t.join

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

Joe Van Dyk said:

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.

From what I've seen the problem is on the reading end, not the writing
end. Try just doing

puts "we recieved: <#{session.read(5)}>"

instead of using gets. You will see "Hello", even without using a newline
(or calling puts), or closing the client socket.

In my experience most TCP problems I've had have been on the reading side.

Ryan

Ahhh... thanks. All working fine now.

···

On 6/30/05, Ryan Leavengood <mrcode@netrox.net> wrote:

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

You are missing a newline:

tcp_client.write "Hello World\n"

gets looks for a newline.

Ryan

"Ryan Leavengood" <mrcode@netrox.net> writes:

Joe Van Dyk said:

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.

From what I've seen the problem is on the reading end, not the writing
end. Try just doing

Joe,

Just another thing to note: in TCP, the TCP stack is free to send the
data on the wire whenever it wants to. You can't force it, although
in most TCP stack implementations, a flush() is persuasive enough.

If you want a more guaranteed way of putting data on the wire whenever
you want it, you'd have to use other protocols, like UDP.

YS.