Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Determine when client socket request has been accepted. (http://www.programmingforums.org/showthread.php?t=11499)

hoffmandirt Oct 5th, 2006 12:30 AM

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?

jaeusm Oct 5th, 2006 11:02 AM

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.

hoffmandirt Oct 5th, 2006 11:10 AM

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.

jaeusm Oct 5th, 2006 11:33 AM

Yes, I know, but you've misunderstood me. When I say
Quote:

creation of the client's socket would not return until the ServerSocket has 'accepted' the request
I mean that the client Socket will not return from the constructor call until it has been accepted by the ServerSocket, including the time it sits in the queue. Again, this is speculation since it isn't specified in the Socket or ServerSocket APIs.

Quote:

Creation of the socket on the client side only blocks until it gets put in the server sockets request queue
Where did you get this information?

hoffmandirt Oct 5th, 2006 11:52 AM

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.

jaeusm Oct 5th, 2006 12:19 PM

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.

hoffmandirt Oct 5th, 2006 1:22 PM

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.

Harakim Oct 5th, 2006 6:22 PM

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.

hoffmandirt Oct 6th, 2006 10:50 AM

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