Link to home
Start Free TrialLog in
Avatar of skiuia
skiuia

asked on

Twsocket as Twsocketserver

how i set the clientclass in twsocket ?
I want to use as server twsock!

procedure TForm1.FormCreate(Sender: TObject);
begin
  try
    with wsocketserver do
    begin
      //ClientClass   := TMyClient;
      //BannerTooBusy := '';
      //Banner := '';
      Addr   := '0.0.0.0';
      Port   := '340';
      Listen;
    end;

end;

Open in new window


thanks
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

If you use ics then there are few examples. Simply define custom class as successor of
TWSocketClient. On Server component define some events (OnClientConnect, OnClientDisconnect, OnBgException, ...)

type
  TMyClient = class(TWSocketClient)
  public
    ClientIP   : String;
    ConnectTime : TDateTime;
  end;
...

procedure StartServer;
begin
   WSocketServer1.Proto       := 'tcp';         
    WSocketServer1.Port        := '340';      
    WSocketServer1.Addr        := '0.0.0.0';     
    WSocketServer1.ClientClass := TMyClient; 
    WSocketServer1.Listen;                      
end;
...

procedure TForm1.WSocketServer1ClientConnect(Sender : TObject; 
  Client : TWSocketClient; Error  : Word);
begin
    with Client as TMyClient do begin
        OnDataAvailable := ClientDataAvailable;
        OnBgException := ClientBgException;
        ConnectTime := Now;
        ClientIP := GetPeerAddr;
...
    end;
end;

procedure TForm1.ClientDataAvailable(Sender : TObject; Error  : Word);
begin
    with Sender as TMyClient do begin      
      ProcessData(Sender as TMyClient);
    end;
end;

procedure TForm1.ClientBgException(Sender: TObject; E: Exception;
    var CanClose : Boolean);
begin
    CanClose := TRUE;   
end;
...

Open in new window

Avatar of skiuia
skiuia

ASKER

If you use ics then there are few examples. Simply define custom class as successor of
TWSocketClient. On Server component define some events (OnClientConnect, OnClientDisconnect, OnBgException, ...)

right i know,  but i need use twsocket as twsocketserver understand?
I have to put the twsocketclient?
twsocketclient cannot be a server. You must set client event DataAvailable and process incomming data. Client makes socket connection to server and through that connection he receive data (raise event DataAvailable). On the other side, server can use current client connection to send some data.

...

for i:=1 to FSrv.ClientCount do
begin
    try
       Client := TMyClient(FSrv.Client[i-1]);
       if (Client.State in [wsConnected]) and (TMyClient(FSrv.Client[i-1]).PeerAddr = Dessaddr) then
       begin
          SendSocket.SendStr('some text to client');
       end;
    except
    end;
end;                
...

Open in new window

Avatar of skiuia

ASKER

you do not understand I know twsocketclient can not be a server!
there are two more twsocket the component (client and server) and twsocketserver (Twsocketclient) right? I want to use twsocket, most do not know to associate with twsocket twsocketclient !

see it !

http://wiki.overbyte.be/wiki/index.php/ICS_Components_Reference

I have to use twsocketclient in twsockt?
Avatar of skiuia

ASKER

i need connect reverse i can do it whit twsocktserver?
twsock listen twsockserver or twsocketserver is only for listen?
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial