TWSocket.Dup

From Overbyte
Jump to: navigation, search

Main page -> ICS component reference -> TWSocket -> Dup

Definition

method Dup: ?;

Description

Dup is used to assign a new window handle to a TWSocket. This sounds complicated but is normally used in a TCP-Server (TWSocket.OnSessionAvailable) to transfer a newly accepted connection to another socket which will be used then to deal with that connection.

Example

The following example is the accepting of a single incomming TCP connection on the server side. The asoc named TWSocket should have a OnDataAvailable handler assigned to handle the incomming data.

   asoc     :TWSocket; // socket where the connection will be transfered to, 
                       // would normally be outside on SessionAvailable
 
 procedure TServerSocket.MyOnSessionAvailable(Sender: TObject; ErrCode: Word);
 var NewSocket:TSocket;  // information about the incomming connection
 begin
   // only accept it when asoc doesn't yet exist
   if not assigned(asoc) then
   begin
     try
       // We need to accept the client connection
       NewSocket := Accept; // accept the incomming connection
 
       // and then associate this connection with our client socket
       asoc:=TAnswerSocket.Create(nil);
       asoc.Dup(NewSocket);
     except
       On e:exception do
       begin
         ShowMessage(e.message);
       end;
     end;
   end; 
 end;

Best practices

How to