Ruby Socket Programming: No connection could be made error

Discussion in 'Ruby' started by Merhawi Fissehaye, Apr 28, 2012.

  1. I am a beginner and I wanted to see how network programs are written in Ruby and I tried a sample Ruby code I found on the internet. There are two programs server.rb running in the background and client.rb trying to connect to my server program at the port number specific to the server program.
    I found the following error when running client.rb :

    client.rb:6:in 'initialize' : No connection could be made because the target machine actively refused it. - connect(2) (Errno::ECONNREFUSED)
    from client.rb:6:in 'open'
    from client.rb:6:in '<main>'

    I am running the program from DOS
     
    Merhawi Fissehaye, Apr 28, 2012
    #1
    1. Advertisements

  2. Could be any number of reasons from that you have misconfigured ports or
    the server is listening on 127.0.0.1 only which cannot be connected from
    a different machine or firewall blocking access...

    Can you give more details, show the code?
    Are you serious about that?

    Kind regards

    robert
     
    Robert Klemme, Apr 28, 2012
    #2
    1. Advertisements

  3. ************** Here is clients.rb
    require 'socket'

    hostname = 'localhost'
    port = 2000

    s = TCPSocket.open( hostname, port )

    while line = s.gets # Read lines from the socket
    puts line.chop # And print with platform line terminator
    end
    s.close # Close the socket when done

    ************ Here is server.rb
    require 'socket' # Get socket from stdlib

    server = TCPServer.open( 2000 ) # Socket to listen port 2000
    loop { # Servers run forever
    client = server.accept # Wait for a client to connect
    client.puts( Time.now.ctime ) # Send the time to the client
    client.puts "Closing the connection. Bye!"
    client.close # Disconnect from the client
    }

    I am just beginning to write socket programs! I would love to hear any recommendations pls.
     
    Merhawi Fissehaye, Apr 28, 2012
    #3
  4. ************* Here is client.rb
    require 'socket'

    hostname = 'localhost'
    port = 2000

    s = TCPSocket.open( hostname, port )

    while line = s.gets # Read lines from the socket
    puts line.chop # And print with platform line terminator
    end
    s.close # Close the socket when done


    ************* Here is server.rb
    require 'socket' # Get socket from stdlib

    server = TCPServer.open( 2000 ) # Socket to listen port 2000
    loop { # Servers run forever
    client = server.accept # Wait for a client to connect
    client.puts( Time.now.ctime ) # Send the time to the client
    client.puts "Closing the connection. Bye!"
    client.close # Disconnect from the client
    }
    I am just beginning to write socket programs. I would be glad to hear recommendations pls
     
    Merhawi Fissehaye, Apr 28, 2012
    #4
  5. Works like a charm for me. Did you maybe start the client before the
    server?

    Kind regards

    robert
     
    Robert Klemme, Apr 28, 2012
    #5
  6. Are there any modules to be included or at least to be installed (...the gem install things )? Or is there any way of running my ruby programs from something other than the DOS command window?
     
    Merhawi Fissehaye, Apr 28, 2012
    #6
  7. Command window is perfectly OK. I was just wondering about your
    operating system. I haven't heard someone use DOS in a while. :)

    Now, what's with your connection issue? Does it work now?

    Kind regards

    robert
     
    Robert Klemme, Apr 28, 2012
    #7
  8. Also you probably need two command windows, one to start the server in,
    and the other to start the client in while the server is still running.

    mfg, simon .... l
     
    Simon Krahnke, Apr 28, 2012
    #8
  9. In DOS I meant that I launched command prompt with ruby and tried to run ruby filename from there. Well it has worked for me very well in Ubuntu.
     
    Merhawi Fissehaye, May 2, 2012
    #9
  10. The usage of DOS as a description of a Windows-NT command prompt would
    be wrong, but of a Linux shell that is totally inappropriate.

    mfg, simon .... l
     
    Simon Krahnke, May 3, 2012
    #10
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.