end;
end;
---- 5、Button2 的 OnClick
事件代码如下:
procedure TForm1.Button2Click(Sender: TObject);
begin
if ConnectFlag then
ClientSocket1.Socket.SendText(Edit1.Text)
//
如果连接成功则发送字符串
else
Application.MessageBox(PChar('
没有连接到地址
:
' + Edit2.Text+'。'+
#13+'检查 IP 地址是否存在或是否已经连接!'),'
提示信息
',MB_OK+MB_ICONSTOP);
end;
三、编写被控程序
---- 1、新建一个项目 Server.dpr,并把一个 TServerSocket 元件放到 Form
上,
它的属性设置如下:
Active 设为 True;Port 设为 100
,其它属性按默认值。
---- 2、ServerSocket1 的 OnClientRead
事件代码如下:
procedure TForm1.ServerSocket1ClientRead
(Sender: TObject;Socket: TCustomWinSocket);
var
Str:PChar;
Code,V:Integer;
begin
Str:=PChar(Socket.ReceiveText); //
接收字符串
Val(Str,V,Code); //
处理接收的字符串,首先转换字符串为整数
if Code< >0 then
Application.MessageBox(Str,'提示信息',
MB_OK+MB_ICONINFORMATION)
//
如果转换后的数据不是一个整型数就通过消息框来显示字符串
else
if (V=0)or(V=1)or(V=2) then
ExitWindowsEx(V,0)
//调用 Windows 的 API 函数(V=0
表示重新登录、
V=1 表示关机、V=2 表示重启动)
else
Application.MessageBox(Str,'提示信息',
MB_OK+MB_ICONINFORMATION);
//转换后数据不为 0、1、2
时在消息框内显示字符串
end;
---- 3、修改项目文件,不让窗口显示并且防止它运行两次,修改 begin 和 end
之
间的代码如下:
begin
Application.Initialize;
if FindWindow('TForm1','Form1')=0 then begin
//当没有找到 Form1
时执行下面代码