stupid TCP

Discussion in 'Ruby' started by Joe Van Dyk, Jun 30, 2005.

  1. Joe Van Dyk

    Joe Van Dyk Guest

    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?
     
    Joe Van Dyk, Jun 30, 2005
    #1
    1. Advertising

  2. Joe Van Dyk

    Joe Van Dyk Guest

    On 6/30/05, Joe Van Dyk <> wrote:
    > 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?
     
    Joe Van Dyk, Jun 30, 2005
    #2
    1. Advertising

  3. Joe Van Dyk

    Joe Van Dyk Guest

    On 6/30/05, Joe Van Dyk <> wrote:
    > On 6/30/05, Joe Van Dyk <> wrote:
    > > 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?

    >=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.
     
    Joe Van Dyk, Jun 30, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael W Cocke

    Stupid RE question

    Michael W Cocke, Dec 27, 2004, in forum: Perl
    Replies:
    2
    Views:
    450
    Michael W Cocke
    Dec 28, 2004
  2. Steve

    Is this a stupid question?

    Steve, Jul 17, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    364
    Mario Vargas
    Jul 17, 2003
  3. Brandon McCombs
    Replies:
    4
    Views:
    535
    Richard Wheeldon
    Aug 28, 2006
  4. Tiger
    Replies:
    5
    Views:
    1,012
    Dave Thompson
    May 1, 2006
  5. rincewind

    stupid, STUPID question!

    rincewind, Apr 19, 2009, in forum: HTML
    Replies:
    25
    Views:
    1,061
Loading...

Share This Page