Hey,
Iam trying to connect a server with a client via public ip, but it is always is unable to connect.
When I try it with using localhost (in the same network) its working fine.
I already forwarded my ports in my router and firewall, but it will not connect when I use my
public ip address.
Error
Exception in thread "main" 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 main.ClientMain.main(ClientMain.java:10)
Client
public class ClientMain { public static void main(String[] args) throws IOException { Socket client = new Socket("localhost", 4545); DataOutputStream output = new DataOutputStream(client.getOutputStream()); output.writeUTF("Message from the client."); DataInputStream input = new DataInputStream(client.getInputStream()); System.out.println(input.readUTF()); client.close(); } }
Server
public class ServerMain { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(4545); while(true) { System.out.println("Waiting for client on port " + server.getLocalPort() + "."); Socket client = server.accept(); System.out.println("Client connected from " + client.getInetAddress() + "."); DataInputStream input = new DataInputStream(client.getInputStream()); System.out.println(input.readUTF()); DataOutputStream output = new DataOutputStream(client.getOutputStream()); output.writeUTF("Message from the server."); client.close(); } } }
Thanks for help!