Pregunta

I'm having trouble with my java socket programm. I am opening a ServerSocket on host A. On host B, which is in the same private network, I try to bind to the server, but I always get a timeout.

Host A | Server | IP: 192.168.56.1
Host B | Client | IP: 192.168.47.1

On the server host A::

welcomeSocket = new ServerSocket(2323);
socket = welcomeSocket.accept();

After opening that serverSocket; on host B:

socket = new Socket("192.168.56.1", 2323);

Then after a few seconds, there is the Exception:

java.net.ConnectException: Connection timed out: connect

The socket does work well when I start the server on either host A or B and enter "localhost".
What's wrong here? Thanks for your help.

¿Fue útil?

Solución

If your subnet mask is 255.255.255.0 (which is the norm for class C private subnets) then your two machines are on different subnets:

Host A | Server | IP: 192.168.56.1
Host B | Client | IP: 192.168.47.1

the .56. and .47. being the operative elements. Try to change either IP to be either in 192.168.56.0/24 or 192.168.47.0/24 and you'll be fine.

Alternatively, you'll need to setup a static route between the two subnets.

Cheers,

Otros consejos

Javadoc for setSoTimeout(int) says this:

Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a call to accept() for this ServerSocket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the ServerSocket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

Try this:

welcomeSocket.setSoTimeout(0);

Add this statement before you block the server for accepting request from the client.

Note: Make sure that both the machines are accessible to each other to ensure proper connection.

This usually means incoming port 2323 is not open on host A.
So host B cannot connect to port 2323 on host A.

In a LAN not all ports are open. You cannot connect
to any machine (from your LAN) on any port you want.

Try this from host B:

telnet 92.168.56.1 2323

If you cannot connect this way, then your client java program
will not connect too, and that's normal.

You should then contact your network administrator.

If you can connect this way, only then you should look at
fixing your java programs in some way.

Thank you for helping me!

My program is working fine now. Thanks to Anders' hint with the different subnet mask, I recognized that the IP is wrong. Looking in the Windows control panel revealed me a different IP adress for host A. Actually they are in the same subnet mask.

I was rather rather expecting the adress given by

InetAddress.getLocalHost().getHostAddress()

as my adress.
So how can I show my "real" IP adress in a java program?

Add an Entry in HOST file if IP is reachble

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top