---- 4、Button1的OnClick事件代码如下:
procedure TForm1.Button1Click(Sender: TObject);
begin
with ClientSocket1 do begin
if Active then begin
Active := False;//在连接之前首先关闭
ConnectFlag:=False; //置连接标志为假
end;
if Length(Edit2.Text) > 0 then begin
Host := Edit2.Text; //指定IP地址或主机名
Active := True;
end;
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;
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时执行下面代码
Application.ShowMainForm:=False; //不显示主窗口
Application.CreateForm(TForm1, Form1);
Application.Run;
end;
end.