Java Socket Returns True

Posted by ikurtz on Stack Overflow See other posts from Stack Overflow or by ikurtz
Published on 2010-05-10T12:54:38Z Indexed on 2010/05/10 13:04 UTC
Read the original article Hit count: 268

Filed under:
|

I hope you can help. Im fairly new to progamming and Im playing around with java Sockets.

The problem is the code below. for some reason commSocket = new Socket(hostName, portNumber); is returning true even when it has not connected with the server (server not implemented yet!). Any ideas regarding this situation?

For hostName Im passing my local machine IP and for port a manually selected port.

public void networkConnect(String hostName, int portNumber){

    try {
        networkConnected = false;
        netMessage = "Attempting Connection";
        NetworkMessage networkMessage = new NetworkMessage(networkConnected, netMessage);

        commSocket = new Socket(hostName, portNumber);

        // this returns true!!
        System.out.println(commSocket.isConnected());

        networkConnected = true;
        netMessage = "Connected: ";


        System.out.println("hellooo");

    } catch (UnknownHostException e){
        System.out.println(e.getMessage());

    } catch (IOException e){
        System.out.println(e.getMessage());

    }
}

Many thanks.

EDIT: new Socket(.., ..); is blocking isnt it? i thought in that case if that was processed without exceptions then we have a true connection?

© Stack Overflow or respective owner

Related posts about java

Related posts about socket