简体   繁体   中英

Socket connection program(TCP/IP) working for localhost/common network but not for other IP

I am trying to make connection between a machine(Server) connected to a network(eg via hotspot of Network X) and another(Client) connected to hotspot of Network Y.

Issue 1:

The piece of code is working fine if Server and client(can be multiple) are connected to same Network(say X) but if Server and client are on different Network(X and Y) then I am getting connection timeout error.

Issue 2:

If server and client are on same Network(here via router) then also they are unable to connect with the same above error. I have done port forwarding(here : 5555) with my router and Firewall and defender put to Off of both client and server.

What am I missing.Please review !!

  • Attaching code snippet for Server :

     import java.net.*; import java.io.*; class ServerSideConnection{ public static void main(String args[]){ try{ while(true){ ServerSocket socket = new ServerSocket(5555); Socket serverinput = socket.accept(); Mutithrd_excutn mutithrd_excutn_obj = new Mutithrd_excutn(serverinput); mutithrd_excutn_obj.start(); socket.close(); } }catch(IOException e){ } } } import java.net.*; import java.io.*; class Mutithrd_excutn extends Thread{ public Socket serverinput; public Mutithrd_excutn(Socket serverinput){ this.serverinput = serverinput; } public void run(){ try{ BufferedReader br = new BufferedReader(new InputStreamReader(serverinput.getInputStream())); PrintWriter pw = new PrintWriter(serverinput.getOutputStream()); BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); String str_client = ""; String str_server = ""; while(true){ str_client = br.readLine(); System.out.println("Client : " + str_client); if(str_client.equals("stop")){ return; } str_server = br1.readLine(); pw.println(str_server); pw.flush(); } //serverinput.close(); //socket.close(); }catch(IOException e){ } } }
  • Attaching code snippet for Client (here in localhost I am entering the IP address of Server machine):

     import java.io.*; import java.net.*; public class ClientSideConnection { public static void main(String[] args) { try { Socket s = new Socket("localhost",5555); PrintWriter pw = new PrintWriter(s.getOutputStream()); BufferedReader br_client_input = new BufferedReader(new InputStreamReader(System.in)); BufferedReader br_server_output = new BufferedReader(new InputStreamReader(s.getInputStream())); String str_client = ""; String str_server = ""; while(!str_client.equals("stop")) { str_client = br_client_input.readLine(); pw.println(str_client); pw.flush(); if(!str_client.equals("stop")) { str_server = br_server_output.readLine(); System.out.println("Server : " + str_server); } } br_client_input.close(); br_server_output.close(); s.close(); }catch(IOException e) { } }

    }

Also,please do write in comment if any point I may have missed n elaborating the problem.

If it is working in the same network but not between different networks then I see the following options:

  • there is some firewall blocking the connection
  • or the port forwarding you've setup is done wrong
  • or you are using the wrong target IP, ie the private IP in the target network instead of the public visible IP from the port forwarding

Unfortunately your question does not contain enough details to limit the options further, ie it is unknown how the port forwarding is exactly done and which IP address (internal or external) you use as server address in your tests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM