Difficulty opening a socket

Discussion in 'Ruby' started by ra88it ra88it, Dec 20, 2005.

  1. Hello,

    I write this with some trepidation because it is probably a simple
    problem (I know next to nothing about sockets), but I've tried to find
    the answer on Google and no luck.

    When I do this:

    sock =3D TCPSocket.new('localhost', 8080)

    I get the the "connection refused" error: Errno::ECONNREFUSED

    I have tried other ports, but anyway I don't think that's the problem.
    (No luck with the others, of course.)

    When I do this:

    sock =3D TCPSocket.new('localhost', ftp)

    it works fine.

    Any idea why this doesn't work (or suggestions how I could troubleshoot)?

    Thanks,
    r
    ra88it ra88it, Dec 20, 2005
    #1
    1. Advertising

  2. ra88it ra88it

    Caleb Tennis Guest

    On Tuesday 20 December 2005 12:35, ra88it ra88it wrote:

    > When I do this:
    >
    > sock = TCPSocket.new('localhost', 8080)
    >
    > I get the the "connection refused" error: Errno::ECONNREFUSED


    Is there something listening on port 8080 on your local machine. Sockets are
    a two way beast - there has to be something at the other end willing to
    accept your connection.

    Caleb
    Caleb Tennis, Dec 20, 2005
    #2
    1. Advertising

  3. ra88it ra88it

    Lou Vanek Guest

    telnet localhost 8080

    this should indicate what's listening, if anything.


    Caleb Tennis wrote:

    > On Tuesday 20 December 2005 12:35, ra88it ra88it wrote:
    >
    >
    >>When I do this:
    >>
    >>sock = TCPSocket.new('localhost', 8080)
    >>
    >>I get the the "connection refused" error: Errno::ECONNREFUSED

    >
    >
    > Is there something listening on port 8080 on your local machine. Sockets are
    > a two way beast - there has to be something at the other end willing to
    > accept your connection.
    >
    > Caleb
    Lou Vanek, Dec 20, 2005
    #3
  4. ra88it ra88it wrote:
    > Hello,
    >
    > I write this with some trepidation because it is probably a simple
    > problem (I know next to nothing about sockets), but I've tried to find
    > the answer on Google and no luck.
    >
    > When I do this:
    >
    > sock = TCPSocket.new('localhost', 8080)
    >
    > I get the the "connection refused" error: Errno::ECONNREFUSED


    OK, that means no process is accepting connections on port 8080. What
    are you trying to do?

    >
    > I have tried other ports, but anyway I don't think that's the problem.
    > (No luck with the others, of course.)


    Why are you randomly tring ports? What are you trying to do?

    >
    > When I do this:
    >
    > sock = TCPSocket.new('localhost', ftp)


    Do you mean 'ftp'?

    >
    > it works fine.


    OK, you have an FTP server running.

    >
    > Any idea why this doesn't work (or suggestions how I could troubleshoot)?


    "Connection refused" is pretty straighforward; there isn't a process
    accepting connections on the associated port. Or, you may be running tcp
    wrappers or something else that is causing the connection to be refused.
    In any event, it's not a Ruby issue.
    Bob Showalter, Dec 20, 2005
    #4
  5. ra88it ra88it

    mr ra88it Guest

    Thanks, everyone.

    Like I said, I know nothing about sockets and realize now I probably
    should have asked this question elsewhere. I assumed it was not a
    problem with ruby, but couldn't figure out what was wrong. I thought I
    had set up a process to listen on each port that I tried, but I had
    not.

    Yes, Bob, I meant to type 'ftp' in quotes.

    > Why are you randomly tring ports? What are you trying to do?


    I wasn't trying to do anything except send data over some sockets with
    ruby. Just playing.

    Thanks again,
    r
    mr ra88it, Dec 20, 2005
    #5
  6. mr ra88it wrote:
    > Thanks, everyone.
    >
    > Like I said, I know nothing about sockets and realize now I probably
    > should have asked this question elsewhere.


    It's fine. We just couldn't figure out what you're tying to do.

    > I wasn't trying to do anything except send data over some sockets with
    > ruby. Just playing.


    You need to go ahead and write a simple server and a client.

    Here's a simple "echo" server, that reads lines from the client and
    sends them back:

    require 'socket'
    sock = TCPServer.new(7777)
    while client = sock.accept
    puts "Connection received from #{client.peeraddr.inspect}"
    while s = client.gets
    client.puts s
    end
    puts "Connection from #{client.peeraddr.inspect} terminated"
    end

    Here's a client to test the server:

    require 'socket'
    server = TCPSocket.new('localhost', 7777)
    server.puts "Hello!"
    puts "Server says: #{server.gets}"
    server.close

    Start the server in one window. Then go to another window and run the
    server.

    You can also exercise your server with telnet:

    telnet localhost 7777

    Lines that you type will be echoed back by the server. (From telnet, you
    close the connection by typing the telnet "escape" character, often ^],
    and then typing "close")
    Bob Showalter, Dec 20, 2005
    #6
  7. Bob Showalter wrote:
    > Start the server in one window. Then go to another window and run the
    > server.


    Er, I mean go to another window and run the *client*
    Bob Showalter, Dec 20, 2005
    #7
  8. ra88it ra88it

    mr ra88it Guest

    Thanks again, Bob et al. It works! I appreciate your help...

    On 12/20/05, Bob Showalter <> wrote:
    > mr ra88it wrote:
    > > Thanks, everyone.
    > >
    > > Like I said, I know nothing about sockets and realize now I probably
    > > should have asked this question elsewhere.

    >
    > It's fine. We just couldn't figure out what you're tying to do.
    >
    > > I wasn't trying to do anything except send data over some sockets with
    > > ruby. Just playing.

    >
    > You need to go ahead and write a simple server and a client.
    >
    > Here's a simple "echo" server, that reads lines from the client and
    > sends them back:
    >
    > require 'socket'
    > sock =3D TCPServer.new(7777)
    > while client =3D sock.accept
    > puts "Connection received from #{client.peeraddr.inspect}"
    > while s =3D client.gets
    > client.puts s
    > end
    > puts "Connection from #{client.peeraddr.inspect} terminated"
    > end
    >
    > Here's a client to test the server:
    >
    > require 'socket'
    > server =3D TCPSocket.new('localhost', 7777)
    > server.puts "Hello!"
    > puts "Server says: #{server.gets}"
    > server.close
    >
    > Start the server in one window. Then go to another window and run the
    > server.
    >
    > You can also exercise your server with telnet:
    >
    > telnet localhost 7777
    >
    > Lines that you type will be echoed back by the server. (From telnet, you
    > close the connection by typing the telnet "escape" character, often ^],
    > and then typing "close")
    >
    >
    mr ra88it, Dec 20, 2005
    #8
  9. ra88it ra88it

    eching Guest

    I just started w/ Ruby myself (really digging it so far).

    This is the exact example I was looking for; simple, yet demonstrates
    so much for the beginner.

    Thanks for the great example (believe it or not samples like this are
    harder to find with google than one would think)
    eching, Dec 20, 2005
    #9
    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. Laszlo Nagy
    Replies:
    1
    Views:
    4,755
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    942
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    524
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    643
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    698
Loading...

Share This Page