我找到一个代码:
procedure TForm1.btnConnectClick(Sender: TObject);
begin
IDTelnetDemo.Host := edtServer.Text;
//IDTelnetDemo.port := spnedtPort.Text;
IDTelnetDemo.port := 2101;
IdTelnetDemo.Connect;
end;
procedure TForm1.btnDisconnectClick(Sender: TObject);
begin
IdTelnetDemo.Disconnect;
end;
procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
if IdTelnetDemo.Connected then
IdTelnetDemo.SendCh(Key);
Key := #0;
end;
procedure TForm1.IdTelnetDemoDataAvailable(Sender: TIdTelnet;
const Buffer: String);
const
CR = #13;
LF = #10;
var
Start, Stop: Integer;
begin
if Memo1.Lines.Count = 0 then
Memo1.Lines.Add('');
Start := 1;
Stop := Pos(CR, Buffer);
if Stop = 0 then
Stop := Length(Buffer) + 1;
while Start <= Length(Buffer) do
begin
Memo1.Lines.Strings[Memo1.Lines.Count - 1] :=
Memo1.Lines.Strings[Memo1.Lines.Count - 1] +
Copy(Buffer, Start, Stop - Start);
if Buffer[Stop] = CR then
begin
Memo1.Lines.Add('');
{$IFNDEF Linux}
SendMessage(Memo1.Handle, WM_KEYDOWN, VK_UP, 1);
{$ENDIF}
end;
Start := Stop + 1;
if Start > Length(Buffer) then
Break;
if Buffer[Start] = LF then
Start := Start + 1;
Stop := Start;
while (Buffer[Stop] <> CR) and (Stop <= Length(Buffer)) do
Stop := Stop + 1;
end;
end;
procedure TForm1.IdTelnetDemoConnected(Sender: TObject);
begin
sbrStatus.SimpleText := 'Connected';
end;
procedure TForm1.IdTelnetDemoDisconnect(Sender: TObject);
begin
sbrStatus.SimpleText := 'Connect';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
// idtelnetdemo.Write('on 0 1'+#13);
idtelnetdemo.Write(edit1.Text+#13);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if PingHost('178.100.101.24') then
sbrStatus.SimpleText := 'ping is ok';
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
if UdpSocket1.Connect then
sbrStatus.SimpleText := 'udp is ok';
// UdpSocket1.s
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
//form1.close;
end;
但是发现接受的时候,每次返回的数据都不一样啊。有些数字会丢失啊。怎么回事,大家帮我看看代码有问题吗??