tags:

views:

56

answers:

2

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?

EDIT: I played around with anti virus and now it is working!

+1  A: 

The Socket constructor connects right away and will throw an IOException if it doesn't succeed. So apparently you have connected successfully to a server (this could be one you didn't make yourself).

Thirler
Well i am testing it on the local machine with local IP. so there are no servers running. weird?
iEisenhower
If you are using your local IP, it could be using that as your server.
Anthony Forloney
get a connection without a server running?
iEisenhower
to be more precise, you will get a `ConnectException` (which is subclass of `IOException`) in that specific case. It's a good idea to test for that one and providing a sensible error message to the user.
mihi
+3  A: 

Had that exact same situation a few days ago on a corporate computer, and searched for it for hours.

Check your antivirus, some antivirus (like E*** N**32) use live TCP scanning that make a connection succeed even if nothing is listening on the target port but will reset it later when you try to read/write from the socket.

Add this to your code:

commSocket.getOutputStream().write(0);
commSocket.getInputStream().read();

If you get a SocketException now, you should really consider to change your antivirus.

Alternatively, set a breakpoint in your application right after creating the socket, and then use netstat -ano (on Windows) to check which process id is associated with the other endpoint of your socket (which should be on your machine if you connect to localhost).

I would suggest you to disable your antivirus, but in some cases even that does not help to unload their broken live TCP scanning driver...

mihi
commSocket.getInputStream().write(0); does not work. i could not find the write method.
iEisenhower
Super! I just toggled my antivirus to game more to normal mode and now it is working! Great TIP!
iEisenhower
@ikurtz use write on the output stream, and read on the input stream. :-)
glowcoder
I corrected the code above to use the output stream :)
mihi
@mihi: thanks. but tweaking with the av worked.
iEisenhower