Java Socket returns True

I hope you can help. I am new to programming and I play with Java sockets.

The problem is the code below. for some reason commSocket = new Socket(hostName, portNumber);returns true, even if it is not connected to the server (the server has not yet been implemented!). Any ideas on this situation?

For hostName Im, pass my local computer IP address and port that is manually selected.

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 (.., ..); blocks, right? I thought that in this case, if it was handled without exception, then do we have a true join?

EDIT: I played with antivirus and now it works!

+3
source share
2 answers

.

, (, E*** N**32) TCP-, , , reset , / .

:

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

SocketException, .

, , netstat -ano ( Windows), , ( , localhost).

, ...

+3

Socket IOException, . -, ( , ).

+1

Source: https://habr.com/ru/post/1744769/


All Articles