Does Socket open another thread? Is he returning something?

In the client application, I call new Socket(serverIP,serverPort) . As a result, the client application sends a request to the server application to open the socket. Does it open a new stream? I mean, which of the following is true?

  • The client application sends a request and immediately starts the execution of the following commands (without waiting for a response).

  • The client sends a request and waits for a response. As soon as a response is received, the client application continues to execute the following commands.

The second case seems more realistic and logical to me. However, I do not understand what will happen if the server does not open the socket, and it does not say that it "does not want" to open the second one (this can happen if the server does not exist or the network does not work). What will happen in this case? Will the server wait forever?

In general, it would be nice if the client knew what was the result of its socket request. For example, I can imagine the following situations:

  • The socket is opened by the server.

  • The server refuses to open the socket. Thus, the server exists, it received a request from the client, but it says no.

  • There is no response from the server.

I know that new Socket(serverIP,serverPort) does not return this information. But this causes exceptions. One of them is "UnkownHostException". When is he cast? When the server does not respond for a while (how long)?

ADDED:

I just found out that an UnknownHostException is thrown to indicate that the host IP cannot be determined. Thus, it is not connected with the situations described above (the server does not respond, the server refuses to open the socket).

+4
source share
6 answers

new Socket (host, port) does not open a new thread. it starts synchronous tcp connection establishment. this may take a few seconds or a timeout after some default timeout.

if the server refuses to accept the connection or the client cannot resolve the host ip or access the server (unreachable), an appropriate exception is thrown.

(if you want to control the timeout, use the default constructor and the connect method (SocketAddress endpoint, int timeout)).

+2
source

I don't see anything in the documentation, stating that it creates a new thread, so the answer to your question is NO

0
source

not sure if he is opening a new topic, but I know that it blocks until:

  • Opens successfully
  • Exception occurs
  • Time-out

You can control the timeout limit, in addition, you have to wait for an answer

0
source
 server is not responding maybe means before timeout the server dosen't response the request. server refuses to open a socket maybe means there hasn't any server process listen listen on this port,the port dosen't open. 
0
source

Statement

 new Socket(ip, port) 

does not create a new thread. It just creates a client socket object.

To send and receive data, you need to use the getInputStream / getOutputStream methods on the client socket, and then perform a read / write operation. outStream.write () usually behaves like a non-blocking call, while inStream.read () is a blocking call.

Ideally, client sockets are created in a separate thread, so that the main process continues to move forward without waiting for socket I / O. I would recommend following a similar thing by creating a new thread and assigning a client socket to this thread.

0
source

what will happen if the server does not open the socket and it does not say that it doesn’t β€œwant” to open the second (it can happen if the server does not exist or the network is broken). What will happen in this case? Does the server wait forever?

Which server? If the server does not exist, there is no server, so how can it wait forever? If the network has been broken, how will the server ever know about a connection attempt?

None of these cases is a "server that does not want to open a socket."

In any case, the server will not wait. Also there will be no client. The default connection timeout is about a minute, it depends on the platform, but you can shorten it as described in one of the other answers.

In general, it would be nice to know what the result of his request for a socket is.

This is true. Either the code continues, or an IOException is thrown. Which IOException indicates what went wrong.

0
source

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


All Articles