TWSocketServer Last Updated: 01/27/2007 |
Frequently Asked Questions for TWSocketServer:
What is it
Francois Piette francois.piette@overbyte.be 31/07/2003 |
TWSocketServer is a socket component made to listen for incomming connections. For each new connection TWSocketServer will instanciate a new socket component to handle the connection. When the connection is closed, TWSocketServer will cleanup things.
TWSocketServer is able to handle an unlimited number of clients. The only limits are imposed by your operating system version and the hardware resources available. To help write the code to handle each client connection, TWSocketServer allow to specify your own client socket. By defining yours, you can then add any data or functions needed to handle your protocol. You derive your socket from TClientSocket and assign his type to TWSocketServer.ClientClass. When a client is connecting, TWSocketServer will instanciate your component and trigger OnClientConnect event. From that event handler you can initialize anything required for your application. TWSocketServer has a few properties and events to help manage your server. For example, there is an indexed Client[] property which gives you access to all active clients. You know how many clients are connected with the ClientCount property. As all other ICS component, TWSocketServer is non-blocking. It will never block your application, even if used in a single-threaded application. |
24/07 Server
Wilfried Mestdagh wilfried@mestdagh.biz 31/07/2003 |
I want to write a server that have to run 24/7. How can I be sure if the listening socket has an error or close, that the server stay on listening. Also what about the connected clients, when the server stops listening.
When you stop listening the server, all your clients stay connected, unless you close them yourself of course. This is because all clients have their own TWSocket at server side. About the server, very simple. In OnBGException you set CanClose to True. So when a background exception should come the server will be closed. Leaving it listening may work, but you are not sure. Then in OnSessionClosed you post a message to a custom message handler to let it listen again. You can use the same message handler to activate / deactivate the server. The code could look something like this: procedure TMain.WMConnect(var Msg: TMessage); begin try if Srv.State <> wsClosed then Srv.Close else if SrvActivate.Checked then begin Srv.Addr := SrvInterface.Text; Srv.Port := SrvPort.Text; Srv.Listen; end; except end; end; Note that this code will fail to retry if there is an exception in the call to Listen. |
ClientClass in CBuilder
Francois Piette <francois.piette@overbyte.be> 14/01/2006 |
WSocketServer->ClientClass = __classid(TClient); |
ThreadAttach and ThreadDetach
Francois PIETTE francois.piette@overbyte.be 14/01/2006 |
1. Does ThreadDetach have to happen in the same context as TWSocket creation?
Yes it has to. 2. I assume that a detached TWSocket cannot receive packets. Is this correct? When detached, asynchronous notification will be stopped. But this will not prevent the socket from receiving data. Simply you will not be notifyed (no OnDataAvailable event). When attached again, you'll get the events again and should not loose any data for TCP sessions. For UDP, you'll loose data as soon as winsock buffer is full. In any case, you should keep the time between detach and attach as short as possible. |