How do I connect to the server socket using the ip address and port number? (client is running on a different machine than server)

61,119

Solution 1

To connect in your code you use:

Socket socket = new Socket(server_IP,server_Port);

So you could use:

Socket socket = new Socket("192.168.1.4", 5555);

It looks like you have this in your code so I'm not sure what problem you're having.

Don't forget that you have to setup your router to forward ports if it is located outside of your local network.

http://www.wikihow.com/Set-Up-Port-Forwarding-on-a-Router

Don't forget that if you are running a firewall, this can also interfere with the connection.

Solution 2

Update /etc/hosts
Add following line

127.0.1.1 192.168.10.109

Share:
61,119
Amr Hamada
Author by

Amr Hamada

Updated on July 29, 2022

Comments

  • Amr Hamada
    Amr Hamada almost 2 years

    Client program

    public class client implements Runnable {
    
    protected static String server_IP = "141.117.57.42";
    private static final int server_Port = 5555 ;
    protected static String client_IP ;
    
    
    public static void main(String[] args) throws IOException{
        final  String host = "localhost";
        int init = 0  ;
        
        try {
            InetAddress iAddress = InetAddress.getLocalHost();
            client_IP = iAddress.getHostAddress();
            System.out.println("Current IP address : " +client_IP);
        } catch (UnknownHostException e) {
        }
        
        try {System.out.println("hello1");
            Socket socket = new Socket(server_IP,server_Port);
            System.out.println("hello3");
            init = initialize(socket);
           
        }catch (SocketException e) {
            System.out.println("Error: Unable to connect to server port ");
        }
    
        
        if (init ==  0 ){
            System.out.println("error: Failed to initialize ");
            System.exit(0);
            
        }
        //Thread init_Thread = new Thread();
    }
    private static int initialize(Socket socket ) throws IOException{
        System.out.println("hello");
        int rt_value = 0 ;
        
        OutputStream os = socket.getOutputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter pw = new PrintWriter(os, true);
        
        System.out.println("server: " + br.readLine());
        pw.println("192.343.34.321");
       // BufferedReader userInputBR = new BufferedReader(new InputStreamReader(System.in));
        //String userInput = userInputBR.readLine();
        
        //out.println(userInput);
        
        socket.close();
        return rt_value = 1 ;
        
        
        
    }
    public void run(){
        
    }
    }
    

    server side program

    public class server {
    
    protected static String server_IP ;
    
    public static void main(String[] args) throws IOException {
       
        
        int server_Port = 5555 ;
    
    
    
        try {
            InetAddress iAddress = InetAddress.getLocalHost();
            server_IP = iAddress.getHostAddress();
            System.out.println("Server IP address : " +server_IP);
        } catch (UnknownHostException e) {
        }
        
            ServerSocket serverSocket = new ServerSocket(server_Port);
        
        
            while (true) {
                Socket socket = serverSocket.accept();
                
                 OutputStream os = socket.getOutputStream();
                PrintWriter pw = new PrintWriter(os, true);
                InputStreamReader isr = new InputStreamReader(socket.getInputStream());
                pw.println("Connection confirmed ");
                BufferedReader br = new BufferedReader(isr);
                String str = br.readLine();
                
                pw.println("your ip address is " + str);
                
                pw.close();
               //socket.close();
    
                //System.out.println("Just said hello to:" + str);
            }
        
    

    How do I connect to the server socket using the ip address and port number (client is running on a different machine than server).

    When I change the server_IP in client to "local host", it works perfectly.

  • user1274820
    user1274820 over 9 years
    Some universities setup their networks so that connections cannot be made between computers on certain ports. It's possible thats an issue as well. You may want to try changing the port it uses to 80 or 8080 or something similar. What error is your code throwing?
  • Amr Hamada
    Amr Hamada over 9 years
    no error , it gets suspended at Socket socket = new Socket(server_IP,server_Port);. It prints "hello1" but not "hello2"
  • user1274820
    user1274820 over 9 years
    I would assume some kind of port control policy is in effect (or a firewall on the target machine). At my old school, I was unable to connect to IRC because port 6667 was denied, although usually networked computers have less restrictions. Try pointing your code to a web address or something and see if it still hangs (www.google.com - change the port to 80). If it works on a local machine, then you know it's an issue with the network.
  • atlex2
    atlex2 about 7 years
    Some services depend on the 127.0.1.1 loopback address to not be redirected. I wouldn't recommend this as the solution, but it is a neat trick!
  • Vahe Gharibyan
    Vahe Gharibyan over 6 years
    Thanks atlex, its a news for me.