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

Here is a copy of serverexample.rb (which I got from
Ruby - Socket Programming | Tutorialspoint)


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.

First, I would use a much higher port number,
say something over 12,000, so you aren’t in conflict with a reserved
port. See here:

Then you are going to need to start a sever script before trying to
establish a connection with your client script. See the example here:

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.

Arlen C. 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).

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!

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!

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!

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: