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>'
----------------------------------------
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:
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!
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 Thank you again for you help!
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:
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).