Difficulty opening a socket


#1

Hello,

I write this with some trepidation because it is probably a simple
problem (I know next to nothing about sockets), but I’ve tried to find
the answer on Google and no luck.

When I do this:

sock = TCPSocket.new(‘localhost’, 8080)

I get the the “connection refused” error: Errno::ECONNREFUSED

I have tried other ports, but anyway I don’t think that’s the problem.
(No luck with the others, of course.)

When I do this:

sock = TCPSocket.new(‘localhost’, ftp)

it works fine.

Any idea why this doesn’t work (or suggestions how I could
troubleshoot)?

Thanks,
r


#2

On Tuesday 20 December 2005 12:35, ra88it ra88it wrote:

When I do this:

sock = TCPSocket.new(‘localhost’, 8080)

I get the the “connection refused” error: Errno::ECONNREFUSED

Is there something listening on port 8080 on your local machine.
Sockets are
a two way beast - there has to be something at the other end willing to
accept your connection.

Caleb


#3

telnet localhost 8080

this should indicate what’s listening, if anything.


#4

ra88it ra88it wrote:

I get the the “connection refused” error: Errno::ECONNREFUSED
OK, that means no process is accepting connections on port 8080. What
are you trying to do?

I have tried other ports, but anyway I don’t think that’s the problem.
(No luck with the others, of course.)

Why are you randomly tring ports? What are you trying to do?

When I do this:

sock = TCPSocket.new(‘localhost’, ftp)

Do you mean ‘ftp’?

it works fine.

OK, you have an FTP server running.

Any idea why this doesn’t work (or suggestions how I could troubleshoot)?

“Connection refused” is pretty straighforward; there isn’t a process
accepting connections on the associated port. Or, you may be running tcp
wrappers or something else that is causing the connection to be refused.
In any event, it’s not a Ruby issue.


#5

Thanks, everyone.

Like I said, I know nothing about sockets and realize now I probably
should have asked this question elsewhere. I assumed it was not a
problem with ruby, but couldn’t figure out what was wrong. I thought I
had set up a process to listen on each port that I tried, but I had
not.

Yes, Bob, I meant to type ‘ftp’ in quotes.

Why are you randomly tring ports? What are you trying to do?

I wasn’t trying to do anything except send data over some sockets with
ruby. Just playing.

Thanks again,
r


#6

Bob S. wrote:

Start the server in one window. Then go to another window and run the
server.

Er, I mean go to another window and run the client


#7

mr ra88it wrote:

Thanks, everyone.

Like I said, I know nothing about sockets and realize now I probably
should have asked this question elsewhere.

It’s fine. We just couldn’t figure out what you’re tying to do.

I wasn’t trying to do anything except send data over some sockets with
ruby. Just playing.

You need to go ahead and write a simple server and a client.

Here’s a simple “echo” server, that reads lines from the client and
sends them back:

require ‘socket’
sock = TCPServer.new(7777)
while client = sock.accept
puts “Connection received from #{client.peeraddr.inspect}”
while s = client.gets
client.puts s
end
puts “Connection from #{client.peeraddr.inspect} terminated”
end

Here’s a client to test the server:

require ‘socket’
server = TCPSocket.new(‘localhost’, 7777)
server.puts “Hello!”
puts “Server says: #{server.gets}”
server.close

Start the server in one window. Then go to another window and run the
server.

You can also exercise your server with telnet:

telnet localhost 7777

Lines that you type will be echoed back by the server. (From telnet, you
close the connection by typing the telnet “escape” character, often ^],
and then typing “close”)


#8

Thanks again, Bob et al. It works! I appreciate your help…


#9

I just started w/ Ruby myself (really digging it so far).

This is the exact example I was looking for; simple, yet demonstrates
so much for the beginner.

Thanks for the great example (believe it or not samples like this are
harder to find with google than one would think)