Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Twsocket as Twsocketserver

Posted on 2012-09-10
6
Medium Priority
?
1,850 Views
Last Modified: 2012-09-12
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
0
Comment
Question by:skiuia
  • 3
  • 3
6 Comments
 
LVL 28

Expert Comment

by:Sinisa Vuk
ID: 38385680
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

0
 

Author Comment

by:skiuia
ID: 38386955
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?
0
 
LVL 28

Expert Comment

by:Sinisa Vuk
ID: 38387078
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

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:skiuia
ID: 38387169
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?
0
 

Author Comment

by:skiuia
ID: 38387901
i need connect reverse i can do it whit twsocktserver?
twsock listen twsockserver or twsocketserver is only for listen?
0
 
LVL 28

Accepted Solution

by:
Sinisa Vuk earned 900 total points
ID: 38389840
As you can see in code ClientClass is property of TWSocketServer which is inherited from TWSocket. ClientClass is type of TWSocketClientClass and must be a TWSocketClient or inherited from TWSocketClient after all. Two TWSocket object can listen but this is low level class so you need to write more code to handle connection. Best way is to put one TWSocketServer component to form to act as server and one TWSocket component to connect to some other server (address). Server listen all incomming calls from other distant clients, accept and handle client data. But when you want to act as client (and send some strings) - use TWSocket component (connected to other similar server).

there are more examples like this one:
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_11911520.html
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question