Aamer
Aamer

Reputation: 258

Troubleshoot connecting client to server in java

I have been having this problem for some time now and although my efforts and my friends help I can't seem to get past this problem .

My problem is I am trying to establish a connection between client and a server using sockets its very common actually, but for some reason client can't seem to connect to the server don't know why , here is my attempts to solve the problem

1- I used http://portforward.com/ to open the used port on my router which is of type "zhone" 2- I changed the port multiple times and every time I used PFPortChecker to see if my port is open

my code is fairly simple it opens server and when client connects to it , the server sends the date and time

my server code looks like this

public class DateServer {

    /** Runs the server. */
    public static void main(String[] args) throws IOException {
        ServerSocket listener = new ServerSocket(6780);
        try {
            while (true) {
                Socket socket = listener.accept();
                try {
                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                    out.println(new Date().toString());
                } finally {
                    socket.close();
                }
            }
        } finally {
            listener.close();
        }
    }
}

my client code looks like this

public class DateClient {

    /** Runs the client as an application. First it displays a dialog box asking for the IP address or hostname of a host running the date server, then connects to it and displays the date that it serves. */
    public static void main(String[] args) throws IOException {
        //I used my serverAddress is my external ip address 
        Socket s = new Socket(serverAddress, 6780);
        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String answer = input.readLine();
        JOptionPane.showMessageDialog(null, answer);
        System.exit(0);
    }
}

even though I continued my attempts

3- I closed my firewall just in case

4- I added connection time out in my server socket

with all my attempts I always get this 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 DateClient.main(DateClient.java:13)

note that DateClient.java:13 is this line Socket s = new Socket(serverAddress, 6780);

please help me with this problem , thanks in advance

Upvotes: 1

Views: 4569

Answers (2)

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21748

  • Close the PrintWriter on the server side
  • Be sure you use localhost (try 127.0.0.1) as the server address. Depending on how are you connecting the Internet, the external Internet address (as shown by various "get my ip" tools on the web) may be different from the actual address to that your network interface is configured and not work from the same machine. More here.

Upvotes: 1

cvbattum
cvbattum

Reputation: 799

I tried running your code. First, localhost (127.0.0.1) could solve your problem. On the other side, I changed the ports and IP to my own, and it just works fine (even my external IP). So probably there is something wrong with your port/IP.

  • In case it works using localhost, your IP was not the right one, or something on your computer is blocking external connections. Your client code should look like this, for some reason new Socket(String host, int port) wont work.

    public class DateClient {
    
    /** Runs the client as an application. First it displays a dialog box asking for the IP address or hostname of a host running the date server, then connects to it and displays the date that it serves. */
    public static void main(String[] args) throws IOException {
        //I used my serverAddress is my external ip address 
        InetAddress serverAddress = InetAddress.getByName(String host);
        Socket s = new Socket(serverAddress, 6780);
        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String answer = input.readLine();
        JOptionPane.showMessageDialog(null, answer);
        System.exit(0);
    }
    

    }

  • If it doesn't work using localhost, your port is not forwarded correctly. Try to log in to your router and forward the port from there.

And indeed, like @Audrius Meškauskas said, you might want to close your PrintWriter on your server, right before closing your ServerSockect listener.

Upvotes: 1

Related Questions