![]() |
Determine when client socket request has been accepted.
How can it be determined when a connection request sent by a client socket has been accepted by the server socket vs. the request just sitting in the ServerSocket connection request queue awaiting to be accepted?
|
Why would you want to know this? I would assume that the creation of the client's socket would not return until the ServerSocket has 'accepted' the request, though the Java APIs don't explicitly specify this.
|
That's not true. The server socket has a queue that stores connection requests. When you call the accept method, it blocks until there is a connection request in the server socket queue. Creation of the socket on the client side only blocks until it gets put in the server sockets request queue you cannot use this socket for communication until it is accepted however.
|
Yes, I know, but you've misunderstood me. When I say
Quote:
Quote:
|
I did understand you and that is what I am saying. The call returns from the constructor as soon as it sends the request and finds the server socket waiting for connection requests. When you start a server socket, it is sitting there waiting for requests. It stores these requests in a queue. Then when you call the accept method, it pulls the request out of the queue and returns a Socket. Try it out, start a server socket, but do not call the accept method, but instead open multiple client sockets.
|
You are correct. However, I started using the client socket before it was accepted by sending a simple message. The ServerSocket apparently queues the message and responds after it accepts the request. So it appears that you can "use" a socket while it is queued.
|
I decided to set a timeout on the client socket and if the read times out, then reconnect. I ran into this problem having top laptop's with each one connected to a bridge and when the wireless signal went down, I could no longer depend on the client socket to throw an io exception. I was doing some reading and it seems that it is just a good idea to set a timeout on these things.
|
I think you should be using SocketChannels in the stead of Sockets.
In response to your question, if it "connects" without accepting the connection, you can't determine when it has been accepted from the client side. |
Right, so i put a timeout on the read from the client side, which will attempt to reconnect. I do not know much about socket channels, I will look in to them, but can you tell me why they would be better?
|
| All times are GMT -5. The time now is 1:15 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC