D
delphiboy
Unregistered / Unconfirmed
GUEST, unregistred user!
客户端:
procedure TForm1.BtnConnectClick(Sender: TObject);
var
Fram:array of char;
begin
IdTCPClient.Host := EdtHost.Text;
IdTCPClient.Port := StrToInt(EdtPort.Text);
LbLog.Lines.Add('正在连接 ' + EdtHost.Text + '...');
try
IdTCPClient.Connect(5000);
try
LbLog.Lines.Add(IdTCPClient.ReadLn());
BtnConnect.Enabled := False;
BtnSend.Enabled := True;
BtnDisconnect.Enabled := True;
except
LbLog.Lines.Add('远程主机无响应!');
IdTCPClient.Disconnect();
end;
except
LbLog.Lines.Add('无法建立到' + EdtHost.Text + '的连接!');
end;
end;
procedure TForm1.BtnSendClick(Sender: TObject);
var
Sinfo:array of char;
Str :STRING ;
I:Integer ;
begin
LbLog.Lines.Add('DATA ' + EdtData.Text);
try
Str :=Trim('DATA '+EdtData.Text) ;
SetLength(Sinfo,Length(Str)) ;
For i:=0 To Length(Str)-1 DO
Begin
Sinfo :=Str ;
End;
IdTCPClient.WriteBuffer(Sinfo,Sizeof(Sinfo),True);
IdTCPClient.CloseWriteBuffer ;
LbLog.Lines.Add(IdTCPClient.ReadLn()) ;
except
Str :='发送数据失败!';
LbLog.Lines.Add(Str);
IdTCPClient.Disconnect();
LbLog.Lines.Add('同主机 ' + EdtHost.Text + ' 的连接已断开!');
BtnConnect.Enabled := True;
BtnSend.Enabled := False;
BtnDisconnect.Enabled := False;
end;//end try
end;
procedure TForm1.BtnDisconnectClick(Sender: TObject);
var
Received: array of char ;
Rstr :String ;
i:integer ;
begin
LbLog.Lines.Add('退出');
try
Rstr :='QUIT!';
SetLength(Received,Length(Rstr)) ;
For i:=0 To Length(Rstr) Do
Begin
Received :=RStr ;
End;
IdTCPClient.WriteBuffer(Received,Sizeof(Received),True);
finally
IdTCPClient.Disconnect();
LbLog.Lines.Add('同主机 ' + EdtHost.Text + ' 的连接已断开!');
BtnConnect.Enabled := True;
BtnSend.Enabled := False;
BtnDisconnect.Enabled := False;
end;//end try
end;
服务器端:
private
{ Private declarations }
procedure DisplayData();
procedure AddLogEntry();
public
{ Public declarations }
FLogEntry ,FReceived:String ;
end;
var
FormMain: TFormMain;
implementation
{$R *.dfm}
procedure TFormMain.AddLogEntry;
begin
LbLog.Lines.Add(FLogEntry);
end;
procedure TFormMain.BtnStartClick(Sender: TObject);
begin
IdTCPServer.DefaultPort := StrToInt(EdtPort.Text);
IdTCPServer.Active := True;
BtnStart.Enabled := False;
BtnStop.Enabled := True;
lblog.Lines.Add('服务器已成功启动!');
end;
procedure TFormMain.BtnStopClick(Sender: TObject);
begin
IdTCPServer.Active := False;
BtnStart.Enabled := True;
BtnStop.Enabled := False;
lblog.Lines.Add('服务器已成功停止!');
end;
procedure TFormMain.DisplayData;
begin
Edit2.Text := FReceived;
end;
procedure TFormMain.IdTCPServerConnect(AThread: TIdPeerThread);
var
Received: array of char ;
Rstr :String ;
i:integer ;
Begin
LbLog.Lines.Add('来自主机 '+ AThread.Connection.Socket.Binding.PeerIP+ ' 的连接请求已被接纳!');
Rstr :='100: 欢迎连接到简单TCP服务器!';
SetLength(Received,Length(Rstr)) ;
For i:=0 To Length(Rstr) Do
Begin
Received :=RStr ;
End;
AThread.Connection.WriteBuffer(Received,SizeOf(Received),True);
end;
procedure TFormMain.IdTCPServerExecute(AThread: TIdPeerThread);
var
sCommand: string;
Received: array of char ;
Rstr :String ;
i:integer ;
begin
sCommand := AThread.Connection.ReadLn() ;
FLogEntry := sCommand + ' 来自于主机 '+ AThread.Connection.Socket.Binding.PeerIP;
AThread.Synchronize(AddLogEntry);
if AnsiStartsText('DATA',sCommand) then
begin
FReceived := RightStr(sCommand,Length(sCommand)-5);
Rstr :='200: 数据接收成功!' ;
SetLength(Received,Length(Rstr)) ;
For i:=0 To Length(Rstr) Do
Begin
Received :=RStr ;
End;
AThread.Connection.WriteBuffer(Received,Sizeof(Received),True);
AThread.Synchronize(DisplayData);
end
else if SameText(sCommand, 'QUIT') then
begin
FLogEntry := '断开同主机 '+ AThread.Connection.Socket.Binding.PeerIP+ ' 的连接!';
AThread.Synchronize(AddLogEntry);
AThread.Connection.Disconnect;
Rstr :='500: 无法识别的命令!';
SetLength(Received,Length(Rstr)) ;
For i:=0 To Length(Rstr) Do
Begin
Received :=RStr ;
End;
AThread.Connection.WriteBuffer(Received,Sizeof(Received));
end
else begin
Rstr :='500: 无法识别的命令!';
SetLength(Received,Length(Rstr)) ;
For i:=0 To Length(Rstr) Do
Begin
Received :=RStr ;
End;
AThread.Connection.WriteBuffer(Received,Sizeof(Received));
FLogEntry := '无法识别命令:' + sCommand;
AThread.Synchronize(AddLogEntry);
end;
end;
end.
服务器端启动后,客户端可以连,但连上后,客户端就象死机了一样。哪里我处理的不对?
procedure TForm1.BtnConnectClick(Sender: TObject);
var
Fram:array of char;
begin
IdTCPClient.Host := EdtHost.Text;
IdTCPClient.Port := StrToInt(EdtPort.Text);
LbLog.Lines.Add('正在连接 ' + EdtHost.Text + '...');
try
IdTCPClient.Connect(5000);
try
LbLog.Lines.Add(IdTCPClient.ReadLn());
BtnConnect.Enabled := False;
BtnSend.Enabled := True;
BtnDisconnect.Enabled := True;
except
LbLog.Lines.Add('远程主机无响应!');
IdTCPClient.Disconnect();
end;
except
LbLog.Lines.Add('无法建立到' + EdtHost.Text + '的连接!');
end;
end;
procedure TForm1.BtnSendClick(Sender: TObject);
var
Sinfo:array of char;
Str :STRING ;
I:Integer ;
begin
LbLog.Lines.Add('DATA ' + EdtData.Text);
try
Str :=Trim('DATA '+EdtData.Text) ;
SetLength(Sinfo,Length(Str)) ;
For i:=0 To Length(Str)-1 DO
Begin
Sinfo :=Str ;
End;
IdTCPClient.WriteBuffer(Sinfo,Sizeof(Sinfo),True);
IdTCPClient.CloseWriteBuffer ;
LbLog.Lines.Add(IdTCPClient.ReadLn()) ;
except
Str :='发送数据失败!';
LbLog.Lines.Add(Str);
IdTCPClient.Disconnect();
LbLog.Lines.Add('同主机 ' + EdtHost.Text + ' 的连接已断开!');
BtnConnect.Enabled := True;
BtnSend.Enabled := False;
BtnDisconnect.Enabled := False;
end;//end try
end;
procedure TForm1.BtnDisconnectClick(Sender: TObject);
var
Received: array of char ;
Rstr :String ;
i:integer ;
begin
LbLog.Lines.Add('退出');
try
Rstr :='QUIT!';
SetLength(Received,Length(Rstr)) ;
For i:=0 To Length(Rstr) Do
Begin
Received :=RStr ;
End;
IdTCPClient.WriteBuffer(Received,Sizeof(Received),True);
finally
IdTCPClient.Disconnect();
LbLog.Lines.Add('同主机 ' + EdtHost.Text + ' 的连接已断开!');
BtnConnect.Enabled := True;
BtnSend.Enabled := False;
BtnDisconnect.Enabled := False;
end;//end try
end;
服务器端:
private
{ Private declarations }
procedure DisplayData();
procedure AddLogEntry();
public
{ Public declarations }
FLogEntry ,FReceived:String ;
end;
var
FormMain: TFormMain;
implementation
{$R *.dfm}
procedure TFormMain.AddLogEntry;
begin
LbLog.Lines.Add(FLogEntry);
end;
procedure TFormMain.BtnStartClick(Sender: TObject);
begin
IdTCPServer.DefaultPort := StrToInt(EdtPort.Text);
IdTCPServer.Active := True;
BtnStart.Enabled := False;
BtnStop.Enabled := True;
lblog.Lines.Add('服务器已成功启动!');
end;
procedure TFormMain.BtnStopClick(Sender: TObject);
begin
IdTCPServer.Active := False;
BtnStart.Enabled := True;
BtnStop.Enabled := False;
lblog.Lines.Add('服务器已成功停止!');
end;
procedure TFormMain.DisplayData;
begin
Edit2.Text := FReceived;
end;
procedure TFormMain.IdTCPServerConnect(AThread: TIdPeerThread);
var
Received: array of char ;
Rstr :String ;
i:integer ;
Begin
LbLog.Lines.Add('来自主机 '+ AThread.Connection.Socket.Binding.PeerIP+ ' 的连接请求已被接纳!');
Rstr :='100: 欢迎连接到简单TCP服务器!';
SetLength(Received,Length(Rstr)) ;
For i:=0 To Length(Rstr) Do
Begin
Received :=RStr ;
End;
AThread.Connection.WriteBuffer(Received,SizeOf(Received),True);
end;
procedure TFormMain.IdTCPServerExecute(AThread: TIdPeerThread);
var
sCommand: string;
Received: array of char ;
Rstr :String ;
i:integer ;
begin
sCommand := AThread.Connection.ReadLn() ;
FLogEntry := sCommand + ' 来自于主机 '+ AThread.Connection.Socket.Binding.PeerIP;
AThread.Synchronize(AddLogEntry);
if AnsiStartsText('DATA',sCommand) then
begin
FReceived := RightStr(sCommand,Length(sCommand)-5);
Rstr :='200: 数据接收成功!' ;
SetLength(Received,Length(Rstr)) ;
For i:=0 To Length(Rstr) Do
Begin
Received :=RStr ;
End;
AThread.Connection.WriteBuffer(Received,Sizeof(Received),True);
AThread.Synchronize(DisplayData);
end
else if SameText(sCommand, 'QUIT') then
begin
FLogEntry := '断开同主机 '+ AThread.Connection.Socket.Binding.PeerIP+ ' 的连接!';
AThread.Synchronize(AddLogEntry);
AThread.Connection.Disconnect;
Rstr :='500: 无法识别的命令!';
SetLength(Received,Length(Rstr)) ;
For i:=0 To Length(Rstr) Do
Begin
Received :=RStr ;
End;
AThread.Connection.WriteBuffer(Received,Sizeof(Received));
end
else begin
Rstr :='500: 无法识别的命令!';
SetLength(Received,Length(Rstr)) ;
For i:=0 To Length(Rstr) Do
Begin
Received :=RStr ;
End;
AThread.Connection.WriteBuffer(Received,Sizeof(Received));
FLogEntry := '无法识别命令:' + sCommand;
AThread.Synchronize(AddLogEntry);
end;
end;
end.
服务器端启动后,客户端可以连,但连上后,客户端就象死机了一样。哪里我处理的不对?