Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ruby Socket Programming: No connection could be made error

46 views
Skip to first unread message

Merhawi Fissehaye

unread,
Apr 28, 2012, 6:45:38 AM4/28/12
to
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

Robert Klemme

unread,
Apr 28, 2012, 6:53:30 AM4/28/12
to
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?

> I am running the program from DOS

Are you serious about that?

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Merhawi Fissehaye

unread,
Apr 28, 2012, 7:07:10 AM4/28/12
to
************* 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
Message has been deleted

Robert Klemme

unread,
Apr 28, 2012, 9:35:01 AM4/28/12
to
Works like a charm for me. Did you maybe start the client before the
server?

Merhawi Fissehaye

unread,
Apr 28, 2012, 9:41:05 AM4/28/12
to
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?

Robert Klemme

unread,
Apr 28, 2012, 2:30:03 PM4/28/12
to
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?

Simon Krahnke

unread,
Apr 28, 2012, 3:59:28 PM4/28/12
to
* Robert Klemme <short...@googlemail.com> (20:30) schrieb:

>On 28.04.2012 15:41, Merhawi Fissehaye wrote:
>> 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?
>
> Command window is perfectly OK. I was just wondering about your
> operating system. I haven't heard someone use DOS in a while. :-)

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

Merhawi Fissehaye

unread,
May 2, 2012, 10:31:55 AM5/2/12
to
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.

Simon Krahnke

unread,
May 3, 2012, 11:19:02 AM5/3/12
to
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
0 new messages