Author: Stewart Moss
How to send a shutdown command in a network?
Answer:
1 2 {-----------------------------------------------------------------------------3 Unit Name: formClient4 Author: Stewart Moss5 6 Creation Date: 27 February, 2002 (16:30)7 Documentation Date: 27 February, 2002 (16:30)8 9 Version 1.010 -----------------------------------------------------------------------------11 12 Description:13 14 This is to demonstrate shutting down a machine over the network.15 16 ** Tobias R. requests the article "How to send a shutdown command in a network?" 17 **18 19 This is not really what you want. I think you are looking for some kind20 of IPC or RPC command. But this will work. Each machine needs to run21 a copy of this server.22 23 It uses the standard delphi ServerSocket found in the "ScktComp" unit.24 25 Create a form (name frmClient) with a TServerSocket on it (name ServerSocket)26 set the Port property of ServerSocket to 5555. Add a TMemo called Memo1.27 28 It listens on port 5555 using TCP/IP.29 30 It has a very simple protocol.31 Z = Show message with "Z"32 B = Beep33 S = Shutdown windows34 35 Run the program.. Then from the command prompt type in36 "telnet localhost 5555". Type in one of the three commands above37 (all in uppercase) and the server will respond.38 39 Copyright 2002 by Stewart Moss. All rights reserved.40 -----------------------------------------------------------------------------}41 42 unit formClient;
43 44 interface45 46 uses47 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
48 ScktComp, StdCtrls;
49 50 type51 TfrmClient = class(TForm)
52 ServerSocket: TServerSocket;
53 Memo1: TMemo;
54 procedure ServerSocketClientRead(Sender: TObject;
55 Socket: TCustomWinSocket);
56 procedure FormCreate(Sender: TObject);
57 procedure FormClose(Sender: TObject; var Action: TCloseAction);
58 private59 { Private declarations }60 public61 { Public declarations }62 end;
63 64 var65 frmClient: TfrmClient;
66 67 implementation68 69 {$R *.DFM}70 71 procedure TfrmClient.ServerSocketClientRead(Sender: TObject;
72 Socket: TCustomWinSocket);
73 var74 Incomming: string;
75 begin76 // read off the socket77 Incomming := Socket.ReceiveText;
78 79 memo1.Lines.Add(incomming);
80 81 if Incomming = 'S' then// Shutdown Protocol82 ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN, 0);
83 84 if Incomming = 'B' then// Beep Protocol85 Beep;
86 87 if Incomming = 'Z' then// Z protocol88 showmessage('Z');
89 end;
90 91 procedure TfrmClient.FormCreate(Sender: TObject);
92 begin93 ServerSocket.Active := true;
94 end;
95 96 procedure TfrmClient.FormClose(Sender: TObject; var Action: TCloseAction);
97 begin98 ServerSocket.Active := false;
99 end;
100 101 end.