Java Socket Timesout when I try connecting to server with external IP - java

I'm trying to make a simple pong game with multiplayer support, but when I try to connect to the server application with my external ip it fails.
What I've Tried
localhost, 127.0.0.1, and 192.168.0.10 all work.
When I check to see if my port is open with an external tool it always returns true right away if I have the server running.
Turning off firewall or adding exceptions hasn't helped.
pinging my external IP returns instant response.
Code for sockets creation in Java and exception
Server Socket Creation
serverSocket = new ServerSocket(7777);
for(;;){
socket = serverSocket.accept();
}
Client Socket Creation
socket = new Socket(IP, 7777);
Exception thrown by client
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Client.<init>(Client.java:93)
at Client.main(Client.java:153)

From what you've told us, the setup looks like this:
You have port forwarded port 7777 from external IP to internal IP B
Your server listens on internal IP B and 127.0.0.1
Your client successfully connects to internal IP B:7777 and 127.0.0.1:7777
Your client does not connect if you point it to external IP:7777
This setup just does not work with most home routers/NAT gateways, they will not port forward a TCP connection destined for the external IP that comes from the internal network itself - it'll only port forward connections that actually comes from the outside(the internet).

Have you tried to accept the connection client yet?
By accept method from serverSocket
boolean isStopped = false;
while(!isStopped){
Socket clientSocket = serverSocket.accept();

Connection time out means your client doesn't get response of its request, possible problems are:
a) The IP/domain or port is incorrect
b) The IP/domain or port (i.e service) is down
c) The IP/domain is taking longer than your default timeout to respond
d) You have a firewall that is blocking requests or responses on whatever port you are using
e) You have a firewall that is blocking requests to that particular host
f) Your internet access is down
Note that firewalls and port or IP blocking may be in place by your ISP

Related

Socket connection from PC to device fails

I'm using sockets for a client-server application (i.e. the client sends data and the server received the data). My code is below. When I use the client in an Android 5.1 application on a tablet and the server in a Java application on a Windows 7 PC it works very well (i.e. the tablet can send data to the PC). But when I try to use the server code on the Android 5.1 application on the tablet and the client code on the PC (i.e. sending data from the PC to the tablet) the connection cannot be established and I'm getting the error:
java.net.ConnectException: Connection timed out: connect
The tablet and the PC are in the same network.
What is wrong?
Server:
ServerSocket ss = new ServerSocket(13005);
Socket socket = ss.accept();
ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
Client:
socket = new Socket();
socket.connect(new InetSocketAddress("10.2.130.125", 13005));
oos = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
Edit:
I have now executed netstat -nap. The output is as follows (I have altered the foreign addresses slightly). 13005 seems not to be used.
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 417 0 10.2.130.125:36844 xxx.xxx.23.74:443 CLOSE_WAIT
tcp 417 0 10.2.130.125:35996 xxx.xxx.23.110:443 CLOSE_WAIT
tcp6 1 0 ::ffff:10.2.130.125:44994 ::ffff:xxx.xx.205.170:443 CLOSE_WA
IT
tcp6 0 0 ::ffff:10.2.130.125:43995 ::ffff:xx.xx.119.188:5228 ESTABL
ISHED
tcp6 1 0 ::ffff:10.2.130.125:42353 ::ffff:xx.xx.23.110:80 CLOSE_WAI
T
tcp6 1 0 ::ffff:10.2.130.125:35722 ::ffff:xx.xx.205.170:443 CLOSE_WA
IT
tcp6 1 0 ::ffff:10.2.130.125:48101 ::ffff:xx.xx.205.110:443 CLOSE_WA
IT
I have also executed nmap -sS -Pn -p- 10.2.130.125. The output is:
Starting Nmap 7.60 ( https://nmap.org ) at 2018-01-18 16:10 W. Europe Standard T
ime
Nmap scan report for public-docking-cx-0635.ethz.ch (10.2.130.125)
Host is up (0.059s latency).
Not shown: 65534 filtered ports
PORT STATE SERVICE
22/tcp closed ssh
Nmap done: 1 IP address (1 host up) scanned in 159.90 seconds
Here is the stacktrace from the client. It is just a timeout exception. From the server I don't get any exception.
java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at inf.ethz.ch.streaming.server.main.Main$1.run(Main.java:164)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Maybe the port 13005 is being used, probably the last connection was not closed.
Connect your device via USB. Then go to the folder where you have adb and run the following:
adb shell
netstat -nap
This will show you all the active ports in the android device. You will be able to check if port 13005 is being used.
If you have lot of connections, then is better to apply grep to step 2.
netstat -na |grep 13005
Then try to use another port, for example 5001. If the port is not restricted and is free, you should be able to connect.
Are you sure that the port is available from outside?
You have to open the port in your router configuration.

Refusing connection while trying to connect localhost

I was trying to develop a p2p file transfer application on java and as for the beginning I decided to run some transfer tests using localhost, as for the server, between local drives by some codes I found on the internet marked as working. The problem is for every port number I tried so far(+20) I got a "connection refused" error. I've installed microsoft loopback adapter as precaution, yet couldn't find any way to solve it. Any help would be appreciated.
just in case, I'm writing some code part related to socket in the client class.
// localhost for testing
Socket sock = new Socket("127.0.0.1",15123);
System.out.println("Connecting...");
and here is the error message
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Client.main(Client.java:12)
line 12 is Socket sock = servsock.accept();
Most often , I saw that this exception comes when there is no service available to listen.Try to use another(free) port and make it sure that your server is running.You can find free port by writing netstat -a -o -n in cmd(on windows)

Netty connection timeout on other pc

I wrote server and client on Java using Netty.When i run client on my PC it works just fine. When i am trying to run client on other PC it throws me:
java.net.ConnectException: connection timed out
at org.jboss.netty.channel.socket.nio.NioClientSocketPipelineSink$Boss.processConnectTimeout(NioClientSocketPipelineSink.java:391)
at org.jboss.netty.channel.socket.nio.NioClientSocketPipelineSink$Boss.run(NioClientSocketPipelineSink.java:289)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
17-Sep-2012 10:58:55 AM org.jboss.netty.channel.SimpleChannelHandler
What is the reason of this?
Check the connection parameters.
Is the server visible from another client? (try pinging the server from the client).
Are there any firewalls between? Try switching them off.
Check the connection string. Make sure you aren't connecting to localhost.
Check the server configuration. Does it listen on the proper network interface.
If you check everything and it seems OK. Post the network connection code here.
Happy coding :)

Java sockets - java.net.ConnectException: Connection refused: connect

I've created a simple chat program which communicates using sockets. Everything works fine when I'm running it on localhost. However, the problems occur when I try to link the client and server programs using my IP.
http://www.canyouseeme.org/ can connect to my server on port 9999 so I know that the server is fine and the port is open. However, my client can't connect.
The error log...
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Client.connect(Client.java:129)
at Client.main(Client.java:47)
Does anybody have any idea what might be causing this? Thanks in advance.
Edit:
Links to the full source code:
http://pastebin.com/2XftHtn9
Have a look at the answers to: java.net.ConnectException: Connection refused
My first suspicion however would be a firewall issue.....
Is your client on same LAN as your server? I think you should re-check IP address / host name and port number to which your client is connecting.

java RMI connection to server

I have a very simple rmi client / server application. I don't use the "rmiregistry" application though, I use this to create the server:
server = new RemoteServer();
registry = LocateRegistry.createRegistry(PORT);
registry.bind("RemoteServer", server);
The client part is:
registry = LocateRegistry.getRegistry(IPADDRESS, PORT);
remote = (IRemoteServer) registry.lookup("RemoteServer");
Here is the fascinating problem: The application works perfectly when both server and client are running in my (private) local network. As soon as I put the server on a public server, the application hangs for a minute, then gives me the following error:
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.ConnectException: Connection refused to host: 192.168.x.y; nested exception is:
java.net.ConnectException: Connection timed out: connect
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source ... (the rest is truncated)
The key I think is that the client (running on my private network) cannot connect to myself (my address is 192.168.x.y where x.y is some other numbers, but the real error message shows my ip address listed there)
If I kill the rmi server on the public internet, then I instantly get a "connection refused to host: a.b.c.d") message, so I know that something at the server end is at least working.
Any suggestions?
EDIT: just to make this a little more clear: 192.168.x.y is the client address, a.b.c.d is the server address. The stacktrace shows the client cannot connect to the client.
Try running the server with this parameter for the virtual machine:
-Djava.rmi.server.hostname=hostname_of_the_server
192.168.* contains private IP addresses (as does 10.*). These will not be routed on the open internet. You need to make sure that you are using an public IP address for the server, and any firewalls (including NAT) are configured for access. You can do a quick check with telnet on the required port.
I would believe that the server tries to open a socket back to the client and it fails, and the exception is a bit unclear on its wording.
RMI is not very NAT-friendly, IIRC.

Categories