Java, "Connection refused: connect" - Yes, the server is running.

O

Omega

I've been happily playing away with my client/server program for most
of this weekend. I decided earlier today to take it to the next step
and try running it on two machines.

....Boy did that not go over well.

The funny thing is, I did everything I could possibly thing of to make
the program real-network-ready. But then I got this:

-=-
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
-=-

Not descriptive, but okay, I did a google for this, and got hundreds
of:

"That error means you don't have a server running on that port."

I do. Trust me. I wrote the server, I had them communicating before.
The only time things started to go crazy was when I told the server
socket to bind to "getLocalHost()", and when I ran the client on a
different machine, pointing it to the server's IP.

Firewall? Nothing, no Windows firewall, no router, no frayed wires.
I've even tried by setting up a scenario where the server's IP is a
true internet IP address. No dice.
Both machines can ping each other, Samba works. I don't doubt my
connections for all the gymnastics I put them through (Yay SSH
tunnels!).

When I don't bind to "InetAddress.getLocalHost()"
-=-
TCP 0.0.0.0:2112 0.0.0.0:0 LISTENING
-=-

When I do bind to "InetAddress.getLocalHost()"
-=-
TCP MY.IP.AD.DY:2112 0.0.0.0:0 LISTENING
-=-

The only option for my clients at this point is to be told the IP
address of the server. Looking at the socket's code, whether I pass a
string or an InetAddress makes no difference as the socket turns it all
into an InetAddress anyway. So it's all the same.

That leaves the only variances in behaviour up to my server, which
because it was working over "localhost", I assume the bulk of the code
is sound.

Is there anyone out there who can help me figure out why my
client/server refuse to play together when my program is taken over
networks?
Keep in mind, EVERYTHING works when I code it all back to defaults and
it all seems to bind on "localhost".

I'm usually good for figuring things out myself, but there appear to be
a lot of dead end search results on this topic. Chalk it up as one of
Java's few undocumented quirks.
For anyone who wants the end-all-and-be-all solution to be available
for all to see, let's figure this one out. I've tried to clearly cover
all my bases here for good responses..

- Alexander Trauzzi
( Omega )
 
K

Knute Johnson

Omega said:
I've been happily playing away with my client/server program for most
of this weekend. I decided earlier today to take it to the next step
and try running it on two machines.

...Boy did that not go over well.

The funny thing is, I did everything I could possibly thing of to make
the program real-network-ready. But then I got this:

-=-
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
-=-

Not descriptive, but okay, I did a google for this, and got hundreds
of:

"That error means you don't have a server running on that port."

I do. Trust me. I wrote the server, I had them communicating before.
The only time things started to go crazy was when I told the server
socket to bind to "getLocalHost()", and when I ran the client on a
different machine, pointing it to the server's IP.

Firewall? Nothing, no Windows firewall, no router, no frayed wires.
I've even tried by setting up a scenario where the server's IP is a
true internet IP address. No dice.
Both machines can ping each other, Samba works. I don't doubt my
connections for all the gymnastics I put them through (Yay SSH
tunnels!).

When I don't bind to "InetAddress.getLocalHost()"
-=-
TCP 0.0.0.0:2112 0.0.0.0:0 LISTENING
-=-

When I do bind to "InetAddress.getLocalHost()"
-=-
TCP MY.IP.AD.DY:2112 0.0.0.0:0 LISTENING
-=-

The only option for my clients at this point is to be told the IP
address of the server. Looking at the socket's code, whether I pass a
string or an InetAddress makes no difference as the socket turns it all
into an InetAddress anyway. So it's all the same.

That leaves the only variances in behaviour up to my server, which
because it was working over "localhost", I assume the bulk of the code
is sound.

Is there anyone out there who can help me figure out why my
client/server refuse to play together when my program is taken over
networks?
Keep in mind, EVERYTHING works when I code it all back to defaults and
it all seems to bind on "localhost".

I'm usually good for figuring things out myself, but there appear to be
a lot of dead end search results on this topic. Chalk it up as one of
Java's few undocumented quirks.
For anyone who wants the end-all-and-be-all solution to be available
for all to see, let's figure this one out. I've tried to clearly cover
all my bases here for good responses..

- Alexander Trauzzi
( Omega )

Since you decided not to include your code, we can't really tell what's
wrong but here is where I would start:

If it works when both the client and server are on the same machine

Don't bind your sockets, just create them with the appropriate port
new ServerSocket(12345);
new Socket(AddressOfServer,12345);

Make sure there are no firewalls, McAfee, Zone Alarm, Windows Firewall etc.

Make sure you are actually waiting for a connection on the server end
serverSocket.accept();

And most of all - Post some simple code that doesn't work!
 
G

Gordon Beaton

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
[...]

I had them communicating before. The only time things started to go
crazy was when I told the server socket to bind to "getLocalHost()",
and when I ran the client on a different machine, pointing it to the
server's IP.
[...]

When I don't bind to "InetAddress.getLocalHost()"
-=-
TCP 0.0.0.0:2112 0.0.0.0:0 LISTENING
-=-

When I do bind to "InetAddress.getLocalHost()"
-=-
TCP MY.IP.AD.DY:2112 0.0.0.0:0 LISTENING
-=-

[...]

That's the problem then.

When you create the ServerSocket without binding to any particular
address, it uses the *wildcard* address (0.0.0.0), and will accept
connections arriving on any interface. If on the other hand you bind
the ServerSocket to a specific address, then it will accept
connections *on that interface only* while refusing all others.

So the standard answer still holds: you are not connecting where your
server is listening. In this case it's because your server isn't
listening where you think it is.

/gordon
 
R

Rodrigo Zechin

Try running this simple code, if it doesn't work, u gotta call an
Exorcist ;)

Run on server:
new ServerSocket(8647).accept().close(); // or any other port number

AFTER that, run on client:
new Socket("ServerHostGoesHere", 8647).close(); // or any other port
number

Rodrigo
 
O

Omega

Knute, here's some code from my server for you to have a look at. It's
fairly straightforward.

It's fairly commented, so if you doubt my variables, just read the
spelled out logic ;)
The two end of line comments that start at "//Borked" are the ones I
flip flop between to break it or go local only. The heartbeat
mentioned below is used for sending out timed checks on all the
clients, I don't see it impacting my code at all.
Please keep in mind, ALL of this works when I bind without specifying
an address - I just can't connect from anywhere else. The clients say
"connection refused", so I assume that they DO find the server. When I
telnet to the server on any port, it again gives me a connection
refused, so there's something I've got to have overlooked on the
server. The client is pretty honest.

Here's the snippet in my constructor:
-=-
try {

// Bind the server to the socket and start listening.
//inComms = new ServerSocket(port, 0,
InetAddress.getLocalHost()); // Borked
inComms = new ServerSocket(port); // Works, but only local.
listenThread = new Thread(this);
listenThread.start();

// Start the server's heartbeat.
heart = new Timer(heartBeat, this);
heart.start();

// Send a debug notice saying the server has started.
parentUI.serverNotice("Server bound to: " +
inComms.getInetAddress().getHostAddress());

}
catch(IOException ex) {

// There was a problem binding to the socket.
parentUI.serverError(ex.getMessage());

}
-=-

This is the code I've used for my listening thread. 'clients' is a
vector containing references to objects denoting connected clients.
It's constructor takes a socket and a reference back to the server
itself.

-=-
while(listening) {

parentUI.serverNotice("Listening for connections.");

try {

// Add a new ClientUser object to the collection of
connected clients.
clients.add(new ClientUser(inComms.accept(), this));

} catch(IOException ex) {

// Throw the error to the UI.
parentUI.serverError(ex.getMessage());

}
-=-

If you need anything more like my client code, give me a shout. But I
can't possibly think what ANYONE can do differently with a client
(TCP/UDP aside, we're dealing with TCP here).

Again, thanks in advance for the warm responses!

- Alexander Trauzzi
 
K

Knute Johnson

Omega said:
Knute, here's some code from my server for you to have a look at. It's
fairly straightforward.

It's fairly commented, so if you doubt my variables, just read the
spelled out logic ;)
The two end of line comments that start at "//Borked" are the ones I
flip flop between to break it or go local only. The heartbeat
mentioned below is used for sending out timed checks on all the
clients, I don't see it impacting my code at all.
Please keep in mind, ALL of this works when I bind without specifying
an address - I just can't connect from anywhere else. The clients say
"connection refused", so I assume that they DO find the server. When I
telnet to the server on any port, it again gives me a connection
refused, so there's something I've got to have overlooked on the
server. The client is pretty honest.

Here's the snippet in my constructor:
-=-
try {

// Bind the server to the socket and start listening.
//inComms = new ServerSocket(port, 0,
InetAddress.getLocalHost()); // Borked
inComms = new ServerSocket(port); // Works, but only local.
listenThread = new Thread(this);
listenThread.start();

// Start the server's heartbeat.
heart = new Timer(heartBeat, this);
heart.start();

// Send a debug notice saying the server has started.
parentUI.serverNotice("Server bound to: " +
inComms.getInetAddress().getHostAddress());

}
catch(IOException ex) {

// There was a problem binding to the socket.
parentUI.serverError(ex.getMessage());

}
-=-

This is the code I've used for my listening thread. 'clients' is a
vector containing references to objects denoting connected clients.
It's constructor takes a socket and a reference back to the server
itself.

-=-
while(listening) {

parentUI.serverNotice("Listening for connections.");

try {

// Add a new ClientUser object to the collection of
connected clients.
clients.add(new ClientUser(inComms.accept(), this));

} catch(IOException ex) {

// Throw the error to the UI.
parentUI.serverError(ex.getMessage());

}
-=-

If you need anything more like my client code, give me a shout. But I
can't possibly think what ANYONE can do differently with a client
(TCP/UDP aside, we're dealing with TCP here).

Again, thanks in advance for the warm responses!

- Alexander Trauzzi

Alexander:

Try these. Change the address in the Socket constructor to your server
address. If these don't work then you've got a firewall or an address
error somewhere.

import java.io.*;
import java.net.*;

public class Client {
public static void main(String[] args) {
try {
Socket s = new Socket("192.168.3.5",12345);
System.out.println(
"Connected to: " + s.getInetAddress().getHostAddress());
s.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

import java.io.*;
import java.net.*;

public class Server implements Runnable {
public Server() {
new Thread(this).start();
}

public void run() {
while (true) {
try {
ServerSocket ss = new ServerSocket(12345);
Socket s = ss.accept();
ss.close();
System.out.println(
"Connected to: " + s.getInetAddress().getHostAddress());
s.close();
System.out.println("Disconnected");
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}

public static void main(String[] args) {
new Server();
}
}
 
C

Chris Smith

Omega said:
Please keep in mind, ALL of this works when I bind without specifying
an address - I just can't connect from anywhere else.
//inComms = new ServerSocket(port, 0,
InetAddress.getLocalHost()); // Borked
inComms = new ServerSocket(port); // Works, but only local.

So there are two problems here.

1. If you bind without specifying an interface, you can only connect
from the local machine.

2. If you bind to getLocalHost, you can't connect from anywhere.

My thoughts are:

#1: Do you have some kind of firewall running, such as the default
Windows Firewall that's enabled with Windows XP SP2? If so, you should
inform the firewall software that you want to allow outside connections
to that port. If not, you should check the physical connection between
client and server to look for firewalls (sometimes confusing called
"routers" in the consumer cable modem world even though the word
"router" means something different) that might be causing the problem.

#2: The most obvious explanation is that you're binding to a different
IP address than the one you're trying to connect to. Most systems will
have two IP addresses: a constant 127.0.0.1 (called the loopback
address) and another IP that's actually assigned to a network card. If
you bind to only one of them, as you've done here, then you obviously
won't be able to connect to the other.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
N

Nigel Wade

Omega said:
Knute, here's some code from my server for you to have a look at. It's
fairly straightforward.

It's fairly commented, so if you doubt my variables, just read the
spelled out logic ;)
The two end of line comments that start at "//Borked" are the ones I
flip flop between to break it or go local only. The heartbeat
mentioned below is used for sending out timed checks on all the
clients, I don't see it impacting my code at all.
Please keep in mind, ALL of this works when I bind without specifying
an address - I just can't connect from anywhere else. The clients say
"connection refused", so I assume that they DO find the server. When I
telnet to the server on any port, it again gives me a connection
refused, so there's something I've got to have overlooked on the
server. The client is pretty honest.
[code snipped]
If you need anything more like my client code, give me a shout. But I
can't possibly think what ANYONE can do differently with a client
(TCP/UDP aside, we're dealing with TCP here).

Again, thanks in advance for the warm responses!

You only provided the server code.

How is the client attempting to connect to the server? Maybe it's not attempting
to connect to the server you think it is. In the client, do you specify the
server by hostname or IP? If it's a hostname, how does the client resolve the
hostname? There could be a simple hosts/DNS name resolution problem.
 
R

Rob Skedgell

Chris said:
So there are two problems here.

1. If you bind without specifying an interface, you can only connect
from the local machine.

All interfaces here - [0.0.0.0], not [127.0.0.1].
2. If you bind to getLocalHost, you can't connect from anywhere.

Here (1.5.0_06, linux 2.6.15 x86_64), it binds to the LAN interface, not
loopback. InetAddress.getByName(null) gets you the loopback interface.

<code>
import java.net.*;

public class SockAddrTest {
public static void main(String[] args) {
try {
// ServerSocket on tcp/12345
ServerSocket ss = new ServerSocket(12345);
InetAddress ssAddr = ss.getInetAddress();
System.out.printf("ServerSocket bound to: [%1$s]%n",
ssAddr.getHostAddress());
// InetAddress from getLocalHost()
InetAddress localhost = InetAddress.getLocalHost();
System.out.printf("InetAddress.getLocalHost() -> [%1$s]%n",
localhost.getHostAddress());
// loop forever
while (true);
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
}
</code>

$ java SockAddrTest &
[1] 24711
ServerSocket bound to: [0.0.0.0]
InetAddress.getLocalHost() -> [10.0.0.3]

$ netstat --listening --tcp --numeric --program 2>/dev/null \
| grep '^Proto\|:12345'
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program
name
tcp 0 0 :::12345 :::* LISTEN 24711/java
 
O

Omega

Problem solved.
I'd never have believed it unless I had just done it myself.

The server is working, is being tested and is working GREAT.

Turns out the little GUI I was slapping together with netbeans was
instantiating the client with the "default" value of a textbox used to
determine the server.
Lucky me, that textfield always says "localhost" at program start.
Hah.

Okay, so suffice to say, I made a huge booboo here in determining my
problem.

Thank you all for the help, and maybe - just maybe - this can be the
solution: You really are doing something wrong if you get this problem!
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top