var
TimeOut: integer;
StrServerReturn: String;
客户端:
//出错
procedure TfrmMain.myClientSocketError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
Application.MessageBox(PChar('不能连接到服务端,可能是监听程序没有运行或网络故障!'),'连接错误',MB_OK+MB_ICONSTOP);
ErrorCode := 0;
end;
//请求
procedure TfrmMain.myClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Socket.SendText('1');
end;
//获取
procedure TfrmMain.myClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
StrServerReturn := Socket.ReceiveText;
end;
//主窗口
procedure TfrmMain.FormCreate(Sender: TObject);
begin
myClientSocket.Address := '192.168.100.15';//strServerIP;
myClientSocket.Port := 8899;
myClientSocket.Open;
showmessage(StrServerReturn);//这时是个空值
end;
服务端:
procedure TfrmService.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
i: integer;
begin
i := Socket.SendText('服务器返回的字符串');
end;
procedure TfrmService.FormCreate(Sender: TObject);
var
vStartTime: integer;
begin
ServerSocket1.Open;
TimeOut := 6000; {超时时间设置, 这个可以自己定}
vStartTime := GetTickCount;
while true do
begin
if GetTickCount - vStartTime >= TimeOut then
begin
{超时处理}
end;
if StrServerReturn = '某个值' then
begin
{处理代码}
end;
Application.ProcessMessages;
end;
end;