views:

66

answers:

6

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 a socket. Does it start a new thread? I mean which of the following is true?

  1. Client application sends a request and immediately starts to execute following commands (not waiting for the answer).

  2. Client sends the request and waits for the answer. As soon as the answer is obtained, the client application continues to execute following commands.

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

In general it would be nice for the client to know what is the result of its request for the socket. For example I can imagine the following situations:

  1. The socket is opened by the server.

  2. The server refuses to open a socket. So, server exists, it got the request from the client but it says "no".

  3. There is no response from the server.

I know that new Socket(serverIP,serverPort) does not "return" this kind of information. But it throws exceptions. One of them is "UnkownHostException". When it is thrown? When the server is not responding for a while (for how long)?

ADDED:

I just found out that UnknownHostException is thrown to indicate that the IP address of a host could not be determined. So, it is unrelated with the above described situations (server is not responding, server refuses to open a socket).

+1  A: 

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

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

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

Omry
A: 

Well I see nothing in the documentation saying that it creates a new thread, so the answer to your question is NO

chburd
A: 

not sure if it opens new Thread, but i know it blocks until:

  • Successfully opens
  • Exception occurs
  • Timeout

You can control the timeout limit, other than that, you will have to wait for an answer

medopal
A: 
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.
neverend
A: 

The statement

new Socket(ip, port)

doesn't create a new thread. It just creates a client socket object.

For you to send and receive data you have to use getInputStream/getOutputStream methods on the client socket and then do a read/write operation. outStream.write() usually behaves like a non-blocking call whereas inStream.read() is a blocking call.

Ideally, client sockets are created in a separate thread so that the main process would continue to go ahead without waiting for socket related IO operations. I would recommend to follow similar thing by creating a new thread and assigning the client socket to that thread.

navr
A: 

what happens if the server does not open a socket and it does not say that it does not "want" to open the second (it can happen if the server does not exist or network is broken). What will happen in this case? Will server wait forever?

What server? If the server does not exist there is no server so how can it wait forever? If the network was broken how would the server ever know about the connection attempt?

Neither of these cases is 'the server [not wanting] to open a socket'.

In neither case will the server wait forever. Neither will the client. There is a default connection timeout of about a minute, platform-dependent, but you can shorten it as described in one of the other answers.

In general it would be nice for the client to know what is the result of its request for the socket.

It does. Either the code continues or an IOException is thrown. Which IOException it is indicates what went wrong.

EJP