[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

stupid TCP

Joe Van Dyk

6/30/2005 8:59:00 PM

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?


6 Answers

Joe Van Dyk

6/30/2005 9:08:00 PM

0

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. :( What am I missing?


Ryan Leavengood

6/30/2005 9:14:00 PM

0

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. :( What am I missing?

You are missing a newline:

tcp_client.write "Hello World\n"

gets looks for a newline.

Ryan


Joe Van Dyk

6/30/2005 9:15:00 PM

0

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. :( 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.


Ryan Leavengood

6/30/2005 9:24:00 PM

0

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

Joe Van Dyk

6/30/2005 9:24:00 PM

0

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. :( What am I missing?
>
> You are missing a newline:
>
> tcp_client.write "Hello World\n"
>
> gets looks for a newline.
>
> Ryan

Ahhh... thanks. All working fine now.


Yohanes Santoso

7/1/2005 3:11:00 AM

0

"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.