Socket help, getting Errno::ECONNREFUSED

Hi,

I'm kindof new to ruby, and I'm trying to figure out some networking
stuff for ruby, but I keep getting "Errno::ECONNREFUSED". I am on
Windows 7, and I'm just using the cmd prompt to run my code. I disabled
my firewall and run it, but that didn't work. Any suggestions?

This is what happens when I run my code:

···

--------------------------------------
$- ruby serverexample.rb
serverexample.rb:5:in `initialize': No connection could be made because
the targ
et machine actively refused it. - connect(2) (Errno::ECONNREFUSED)
        from serverexample.rb:5:in `open'
        from serverexample.rb:5:in `<main>'
----------------------------------------

Here is a copy of serverexample.rb (which I got from
http://www.tutorialspoint.com/ruby/ruby_socket_programming.htm)

----------------------------
require 'socket'

hostname = 'localhost'
port = 2000
s = TCPSocket.open(hostname, port)

while line = s.gets
  puts line.chomp
end
s.close
---------------------------------

Thank you for the help.

--
Posted via http://www.ruby-forum.com/.

Well, the first thing I would try is using a much higher port number,
say something over 12,000 so you aren't in conflict with a reserved
port. See here:

···

--
Posted via http://www.ruby-forum.com/.

Lol, okay. Guys I messed up! I got my client and server files switched.
That was causing me to run my "server" file but it the code for the
client, and vice versa.

So, I guess the lesson learned here is to make sure you proofread your
stuff before you go bother strangers on the internet!

···

--
Posted via http://www.ruby-forum.com/.

Lol, okay. Guys I messed up! I got my client and server files switched.
That was causing me to run my "server" file but it the code for the
client, and vice versa.

So, I guess the lesson learned here is to make sure you proofread your
stuff before you go bother strangers on the internet!

···

--
Posted via http://www.ruby-forum.com/.

Okay, fixed my previous problem. But my client is still giving me the
ECONNREFUSED error.

First I run this script, which seems to work fine:

···

----------
require 'socket' # Get sockets from stdlib

server = TCPServer.open(9900) # Socket to listen on 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
}
---------------------

then I run the client in a different command prompt (with the server
script still running)

---------------------
require 'socket' # Sockets are in standard library

hostname = 'localhost'
port = 9900

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

Sorry, if I'm spamming, but I would really like to get past this mental
hurdle. And I'm just stuck :confused: Thank you again for you help!

--
Posted via http://www.ruby-forum.com/.

Okay, now it's fixed. For anyone else who gets this problem read this
forum:

http://www.ruby-forum.com/topic/208699

Sorry, for all the posts :confused:

···

--
Posted via http://www.ruby-forum.com/.

That's trying to connect to port 2000 on localhost, but nothing's listening there. You'll need a service listening on the port before you can connect to it.

···

On Sunday, 21 October 2012 at 10:50 AM, 7stud -- wrote:

Well, the first thing I would try is using a much higher port number,
say something over 12,000 so you aren't in conflict with a reserved
port. See here:

List of TCP and UDP port numbers - Wikipedia

--
Posted via http://www.ruby-forum.com/\.

Arlen Cuss wrote in post #1080557:

That's trying to connect to port 2000 on localhost, but nothing's
listening there. You'll need a service listening on the port before you
can connect to it.

Thank you Arlen. I thought that what was the purpose of
TCPSocket.open(hostname, port).

···

--
Posted via http://www.ruby-forum.com/\.