Writing a TCP server - Now understanding it:)

  • Thread starter Thread starter Crirus
  • Start date Start date
C

Crirus

Hello

I just wrote a TCP server.
For that I used MSDN sample.
So, I have a asyncron server. As they say in MSDN, I have this methods:

----------------------
Private Sub StartListening() that is the entry point of the server main
thread.
Private Sub AcceptCallback(ByVal ar As IAsyncResult)
Private Sub Receive(ByVal ar As IAsyncResult)
Private Sub Send(ByVal handler As Socket, ByVal data As Byte())
Private Sub SendCallback(ByVal ar As IAsyncResult)
-----------------------

Now, I start the server with StartListening.
When a connection was made, the socket that listen call the AcceptCallback
in a new thread.
AcceptCallback create a new socket "handler" and pass it forward to
BeginReceive end finish it's thread.
Then Receive is called in a new thread, the "handler" socked is extracted
from the parameter, finish the receive, read the data and process it...
The response is prepared to be send with Send method, using the same
"handler" socket (handler.BeginSend(...)). Here, the receive thread is
finished.
When send is finished, the SendCallback is called in a new thread, the
"handler" is activated again from parameter, handler.EndSend finish the
send. Here the "handler" is shutdown and closed.

Here coms the questions:
This system is connectionless, right?
If so, how can I make it "Keep-Alive"?
I use WebClient for client requests. That class keep the connection opened
if I dont close the "handler"?
I can reuse this handler for further requests?
How the Server, as it is now, will react on such cases, because I see that
any new request spawn a new thread with a new handler, so, what happend with
the first handler socket?


Thanks if you read all this,
more many thanks if you will illuminate me :)
 
I noticed from another sample the fact that to keep connection alive they
just make another call to beginReceive with the same "handler" socket.
WebClient keep the first socked open until the server close it and use the
same one everytime it need?
 
Back
Top