Friends, tell me what the problem is! I wrote the simplest echo server and client to it, as in many examples on the Internet. The server listens to port 45000. The client establishes excellent communication with port 45000 when specifying the ip 127.0.0.1 socket, my internal IP is 192.168.1.2, and even the IP of the included Hamachi =) But you should specify the external IP (I absolutely have it) - and that's it: java.net.ConnectException: Connection refused. Everywhere it is written that this means that this port does not obey any service. Via telnet the same situation. But here's the ill luck: when I check whether the 45000th port is open, for example, via http://2ip.ru/check-port/ , then this site is quite normally accessing the port and shows that it is open, and the server writes to console "Client connected", that is, the idea is that everything works, but not from my computer. I just can not understand what the problem is and how to solve it. Perhaps the problem is somehow related to the fact that I have ADSL, but I have forwarding port 45000, so in theory everything should be OK.

Just in case, server and client code:

Server:

public class Server { public static void main(String[] args) throws IOException { System.out.println("Welcome to Server side"); BufferedReader in = null; PrintWriter out= null; ServerSocket servers = null; Socket fromclient = null; // create server socket try { servers = new ServerSocket(45000); } catch (IOException e) { System.out.println("Couldn't listen to port 45000"); System.exit(-1); } try { System.out.print("Waiting for a client..."); fromclient= servers.accept(); System.out.println("Client connected"); } catch (IOException e) { System.out.println("Can't accept"); System.exit(-1); } in = new BufferedReader(new InputStreamReader(fromclient.getInputStream())); out = new PrintWriter(fromclient.getOutputStream(),true); String input,output; System.out.println("Wait for messages"); while ((input = in.readLine()) != null) { if (input.equalsIgnoreCase("exit")) break; out.println("S ::: "+input); System.out.println(input); } out.close(); in.close(); fromclient.close(); servers.close(); } } 

Customer:

 public class Client { public static void main(String[] args) throws IOException { System.out.println("Welcome to Client side"); Socket fromserver = null; InetAddress ipAddress = InetAddress.getByName("127.0.0.1"); System.out.println("Connecting to... 127.0.0.1"); fromserver = new Socket(ipAddress,45000); BufferedReader in = new BufferedReader(new InputStreamReader(fromserver.getInputStream())); PrintWriter out = new PrintWriter(fromserver.getOutputStream(),true); BufferedReader inu = new BufferedReader(new InputStreamReader(System.in)); String fuser,fserver; while ((fuser = inu.readLine())!=null) { out.println(fuser); fserver = in.readLine(); System.out.println(fserver); if (fuser.equalsIgnoreCase("close")) break; if (fuser.equalsIgnoreCase("exit")) break; } out.close(); in.close(); inu.close(); fromserver.close(); } } 
  • @Tuhlom, give stacktrace? Well, or at least at what point the exception is thrown? - Drac5

1 answer 1

You cannot connect via DNAT to your server on an external IP, being on the same subnet behind the same server ..

  • Thank! That is, in principle, should normally connect from all computers, in addition, on which the server is running? - Tuhlom
  • one
    In addition to all the computers on your subnet. Try testing through proxy or socks - that is, through a third (external) server. - SilverIce