0

Hi a similar question has been asked here: Can't connect to my server when I put in my IP in Java However I have a different error. I am a newbie to java going through the tutorial code from oracle to try and learn how to set up a client/server.

/*
 * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */ 

import java.io.*;
import java.net.*;

public class KnockKnockClient {
    public static void main(String[] args) throws IOException {

        if (args.length != 2) {
            System.err.println(
                "Usage: java EchoClient <host name> <port number>");
            System.exit(1);
        }

        String hostName = args[0];
        int portNumber = Integer.parseInt(args[1]);

        try (
            Socket kkSocket = new Socket(hostName, portNumber);
            PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(
                new InputStreamReader(kkSocket.getInputStream()));
        ) {
            BufferedReader stdIn =
                new BufferedReader(new InputStreamReader(System.in));
            String fromServer;
            String fromUser;

            while ((fromServer = in.readLine()) != null) {
                System.out.println("Server: " + fromServer);
                if (fromServer.equals("Bye."))
                    break;

                fromUser = stdIn.readLine();
                if (fromUser != null) {
                    System.out.println("Client: " + fromUser);
                    out.println(fromUser);
                }
            }
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host " + hostName);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to " +
                hostName);
            System.exit(1);
        }
    }
}

/*
 * Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */ 

import java.net.*;
import java.io.*;

public class KnockKnockServer {
    public static void main(String[] args) throws IOException {

        if (args.length != 1) {
            System.err.println("Usage: java KnockKnockServer <port number>");
            System.exit(1);
        }

        int portNumber = Integer.parseInt(args[0]);

        try ( 
            ServerSocket serverSocket = new ServerSocket(portNumber);
            Socket clientSocket = serverSocket.accept();
            PrintWriter out =
                new PrintWriter(clientSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(
                new InputStreamReader(clientSocket.getInputStream()));
        ) {

            String inputLine, outputLine;

            // Initiate conversation with client
            KnockKnockProtocol kkp = new KnockKnockProtocol();
            outputLine = kkp.processInput(null);
            out.println(outputLine);

            while ((inputLine = in.readLine()) != null) {
                outputLine = kkp.processInput(inputLine);
                out.println(outputLine);
                if (outputLine.equals("Bye."))
                    break;
            }
        } catch (IOException e) {
            System.out.println("Exception caught when trying to listen on port "
                + portNumber + " or listening for a connection");
            System.out.println(e.getMessage());
        }
    }
}

The program running through cmd looks like this:

    Microsoft Windows [Version 6.1.7601]
            Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

            C:\Users\FRYS ELECTRONICS>java KnockKnockServer 4444

            C:\Users\FRYS ELECTRONICS>



    Microsoft Windows [Version 6.1.7601]
            Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

            C:\Users\FRYS ELECTRONICS>java KnockKnockClient localhost 4444
            Server: Knock! Knock!
            Who's there?
            Client: Who's there?
            Server: Turnip
            Turnip who?
            Client: Turnip who?
            Server: Turnip the heat, it's cold in here! Want another? (y/n)
            n
            Client: n
            Server: Bye.

            C:\Users\FRYS ELECTRONICS>

On the "java KnockKnockClient localhost 4444" line, it sets the host and port #. localhost or 127.0.0.1 works, but if I replace it with my IP address, I get this error: Couldn't get I/O for the connection to [My IP Address]

I tried doing it with my firewall turned off but it didn't work. What I'm really trying to do is put the KnockKnockServer program on my home computer, KnockKnockClient program on any other computer, and be able to connect to the Server using the Client program no matter where that other computer is. What am I doing wrong? Thanks.

I added printStackTrace():

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 KnockKnockClient.main(knockknockclient.java:48)

I'm guessing this means the connection was not made. I tried opening the port on my router settings, but it's still not working. I have a cisco Linksys E2000 router, could it be that it just doesn't allow those ports to be open?

Community
  • 1
  • 1
philc90
  • 9
  • 1
  • 4
  • 2
    Did you try the steps in the answer in your linked post? – Fluffmeister General Mar 28 '15 at 23:05
  • 1
    you are ignoring/*silently eating* that tells you **exactly** what is wrong. `e.printStackTrace();` would tell you exactly what is going wrong. –  Mar 28 '15 at 23:24
  • Change that useless error message to print the actual exception. I complained to Sun years ago about using those stupid error messages in their tutorials. I think they finally fixed some of them. Apparently not all. – user207421 Mar 28 '15 at 23:24
  • @JarrodRoberson Very poor choice of duplicate. The one he cites himself is a far better choice: http://stackoverflow.com/questions/1919990/cant-connect-to-my-server-when-i-put-in-my-ip-in-java – user207421 Mar 29 '15 at 01:26
  • I know this is old but I wanted to put my solution it in case it helps anyone else. I was trying to connect through local area connection from a laptop to desktop computer. The problem is that there are separate switches for wireless and wired connections in my router, resulting in the laptop not connecting to the desktop because they are actually on separate local networks. So, I tested it and laptop-laptop or desktop-desktop connections work fine. Hope it helps. – philc90 Aug 06 '15 at 02:36

2 Answers2

1

Look at the last few lines of the KnockKnockClient class. It's printing that error because the code you posted is telling it to print that when you get an exception. Add in a line to print out the exception so you get more information. Like this:

} catch (IOException e) {
  System.err.println("Couldn't get I/O for the connection to " + hostName);
  e.printStackTrace();
  System.exit(1);
}
Quicksilver002
  • 735
  • 5
  • 12
1

Possibly you are using a router, if so please be sure you have the open ports. Depends on the model of router is the method varies but usually must get into the configuration of the router (192.168.1.1) -> Virtual Server, you add ip and port to release.

  • I added port forwarding and put in my local ip address, but it's not working. I went to http://www.canyouseeme.org/ and tried seeing if the port is open and no dice – philc90 Mar 29 '15 at 19:18