Mega Code Archive

 
Categories / Delphi / LAN Web TCP
 

How to use Serversocket and Clientsocket

Title: How to use Serversocket and Clientsocket unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, ScktComp; type TForm1 = class(TForm) Clientsocket1: TClientSocket; StatusBar1: TStatusBar; Button1: TButton; Button2: TButton; Edit1: TEdit; Label1: TLabel; Button3: TButton; CheckBox1: TCheckBox; Checkbox2: TCheckBox; procedure Button1Click(Sender : TObject); procedure Button2Click(Sender : TObject); procedure Clientsocket1Error(Sender : TObject; Socket : TCustomWinSocket; ErrorEvent : TErrorEvent; var ErrorCode : integer); procedure Clientsocket1Disconnect(Sender : TObject; Socket : TCustomWinSocket); procedure Clientsocket1Connect(Sender : TObject; Socket : TCustomWinSocket); procedure Button3Click(Sender : TObject); procedure FormClose(Sender : TObject; var Action : TCloseAction); procedure FormDestroy(Sender : TObject); private { Private declarations } public { Public declarations } end; var Form1 : TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender : TObject); begin Clientsocket1.Active := True; end; procedure TForm1.Button2Click(Sender : TObject); begin Clientsocket1.Active := False; end; procedure TForm1.Clientsocket1Error(Sender : TObject; Socket : TCustomWinSocket; ErrorEvent : TErrorEvent; var ErrorCode : integer); begin errorcode := 0; StatusBar1.SimpleText := 'Error'; end; procedure TForm1.Clientsocket1Disconnect(Sender : TObject; Socket : TCustomWinSocket); begin StatusBar1.SimpleText := 'Disconnect'; end; procedure TForm1.Clientsocket1Connect(Sender : TObject; Socket : TCustomWinSocket); begin StatusBar1.SimpleText := Clientsocket1.Address; end; procedure TForm1.Button3Click(Sender : TObject); var ukaz : string; orders : string; Text : string; box : string; begin ukaz := edit1.Text; Clientsocket1.Socket.SendText(ukaz); if checkbox1.Checked = True then begin orders := 'power'; Clientsocket1.Socket.SendText(orders); end; if Checkbox2.Checked = True then begin Text := 'reset'; Clientsocket1.Socket.SendText(Text); end; end; procedure TForm1.FormClose(Sender : TObject; var Action : TCloseAction); begin Clientsocket1.Active := False; end; procedure TForm1.FormDestroy(Sender : TObject); begin Clientsocket1.Active := False; end; end. // Client Program unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ScktComp, StdCtrls, ShellApi; type TForm1 = class(TForm) Label1: TLabel; Serversocket1: TServerSocket; procedure FormClose(Sender : TObject; var Action : TCloseAction); procedure FormDestroy(Sender : TObject); procedure FormCreate(Sender : TObject); procedure Serversocket1ClientError(Sender : TObject; Socket : TCustomWinSocket; ErrorEvent : TErrorEvent; var ErrorCode : integer); procedure Serversocket1ClientRead(Sender : TObject; Socket : TCustomWinSocket); private { Private declarations } public { Public declarations } end; var Form1 : TForm1; implementation {$R *.dfm} procedure TForm1.FormClose(Sender : TObject; var Action : TCloseAction); begin Serversocket1.Active := False; end; procedure TForm1.FormDestroy(Sender : TObject); begin Serversocket1.Active := False; end; procedure TForm1.FormCreate(Sender : TObject); begin Serversocket1.Active := True; end; procedure TForm1.Serversocket1ClientError(Sender : TObject; Socket : TCustomWinSocket; ErrorEvent : TErrorEvent; var ErrorCode : integer); begin errorcode := 0; end; procedure TForm1.Serversocket1ClientRead(Sender : TObject; Socket : TCustomWinSocket); var ukaz : string; orders : string; Text : string; box : string; begin ukaz := socket.ReceiveText; label1.Caption := 'reciving...'; ShellExecute(Handle, 'open', PChar(ukaz), PChar(''), nil, sw_show); Text := socket.ReceiveText; orders := socket.ReceiveText; if orders = 'power' then begin ShellExecute(Handle, 'open', PChar('shutdown.exe'), PChar('-s'), nil, sw_show); Application.MessageBox('You will be turned off', 'Warning', mb_iconexclamation); Serversocket1.Active := False; Form1.Close; end; if Text = 'reset' then begin ShellExecute(Handle, 'open', PChar('shutdown.exe'), PChar('-r'), nil, sw_show); Application.MessageBox('You will be reset', 'Warning', mb_iconexclamation); Serversocket1.Active := False; Form1.Close; end; end; end.