从网上找的一段服务端代码,可以正常收发我用(TidTCPClient)编的客户端数据
但用VB编的程序,只能连接上,但收不数据。高人给看看,问题出在什么地方
我把断点设在IdTCPServer1Execute里面,能跟踪到VB的程序连接上了。
unit Main;
interface
uses
SysUtils, Windows, Messages, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPServer;
type
TForm2 = class(TForm)
Label1: TLabel;
EdtPort: TEdit;
Label2: TLabel;
EdtData: TEdit;
LbLog: TListBox;
Label3: TLabel;
BtnStart: TButton;
BtnStop: TButton;
Button3: TButton;
IdTCPServer1: TIdTCPServer;
procedure BtnStartClick(Sender: TObject);
procedure BtnStopClick(Sender: TObject);
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
private
{ Private declarations }
FLogEntry, FReceived : String;
public
{ Public declarations }
procedure DisplayData();
procedure AddLogEntry();
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.AddLogEntry;
begin
LbLog.Items.Add(FLogEntry);
end;
procedure TForm2.BtnStartClick(Sender: TObject);
begin
IdTCPServer1.DefaultPort := StrToInt(EdtPort.Text);
IdTCPServer1.Active := True;
BtnStart.Enabled := False;
BtnStop.Enabled := True;
LbLog.Items.Add('服务器已成功启动!');
end;
procedure TForm2.BtnStopClick(Sender: TObject);
begin
IdTCPServer1.Active := False;
BtnStart.Enabled := True;
BtnStop.Enabled := False;
LbLog.Items.Add('服务器已成功停止!');
end;
procedure TForm2.DisplayData;
begin
EdtData.Text := FReceived;
end;
procedure TForm2.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
LbLog.Items.Add('来自主机 '
+ AThread.Connection.Socket.Binding.PeerIP
+ ' 的连接请求已被接纳!');
AThread.Connection.WriteLn('100: 欢迎连接到简单TCP服务器!');
end;
procedure TForm2.IdTCPServer1Execute(AThread: TIdPeerThread);
var
sCommand: string;
begin
with AThread.Connection do
begin
sCommand := ReadLn();
FLogEntry := sCommand + ' 来自于主机 '
+ AThread.Connection.Socket.Binding.PeerIP;
AThread.Synchronize(AddLogEntry);
if AnsiStartsText('DATA ', sCommand) then
begin
FReceived := RightStr(sCommand, Length(sCommand)-5);
WriteLn('200: 数据接收成功!');
AThread.Synchronize(DisplayData);
end
else if SameText(sCommand, 'QUIT') then begin
FLogEntry := '断开同主机 '
+ AThread.Connection.Socket.Binding.PeerIP
+ ' 的连接!';
AThread.Synchronize(AddLogEntry);
Disconnect;
end
else begin
WriteLn('500: 无法识别的命令!');
FLogEntry := '无法识别命令:' + sCommand;
AThread.Synchronize(AddLogEntry);
end;//endif
end;
end;
end.