Ruby Socket Programming: No connection could be made error

M

Merhawi Fissehaye

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
 
R

Robert Klemme

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

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
 
M

Merhawi Fissehaye

************** 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.
 
M

Merhawi Fissehaye

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

Robert Klemme

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

Works like a charm for me. Did you maybe start the client before the
server?

Kind regards

robert
 
M

Merhawi Fissehaye

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

Robert Klemme

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. :)

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

Kind regards

robert
 
S

Simon Krahnke

* Robert Klemme said:
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
 
M

Merhawi Fissehaye

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

Simon Krahnke

* Merhawi Fissehaye said:
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.

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
 

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

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top