INDY怎么接收发送不确定大小的数据 比如字符数组类的,不确定大小的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPServer,StrUtils;type TForm1 = class(TForm) Label1: TLabel; Edit1: TEdit; Label2: TLabel; Edit2: TEdit; Label3: TLabel; ListBox1: TListBox; Button1: TButton; Button2: TButton; Button3: TButton; IdTCPServer1: TIdTCPServer; procedure Button3Click(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure IdTCPServer1Execute(AThread: TIdPeerThread); procedure IdTCPServer1Connect(AThread: TIdPeerThread); private { Private declarations } public { Public declarations } procedure DisplayData(); procedure AddLogEntry(); end;var Form1: TForm1; FLogEntry:string; FReceived:string;implementation{$R *.dfm} procedure TForm1.DisplayData();begin edit2.Text := FReceived;end;procedure TForm1.AddLogEntry();begin self.ListBox1.Items.Add(FLogEntry);end;procedure TForm1.Button3Click(Sender: TObject);begin close;end;procedure TForm1.Button1Click(Sender: TObject);begin IdTCPServer1.DefaultPort := StrToInt(edit1.Text); IdTCPServer1.Active := True; self.Button1.Enabled := False; self.Button2.Enabled := True; self.ListBox1.Items.Add('服务器已成功启动!');end;procedure TForm1.Button2Click(Sender: TObject);begin IdTCPServer1.Active := False; self.Button1.Enabled := True; self.Button2.Enabled := False; self.ListBox1.Items.Add('服务器已成功停止!');end;procedure TForm1.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;procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);begin self.ListBox1.Items.Add('来自主机 ' + AThread.Connection.Socket.Binding.PeerIP + ' 的连接请求已被接纳!'); AThread.Connection.WriteLn('100: 欢迎连接到简单TCP服务器!');end;end. 可以,把数据分割后多次发送,至于怎样合并,就看你功力了,UDP的话一般都是在分割时标记序号,收到后再合并。 不确定大小,最好从包的设计着手. 比如: CmdType+PackSize+DataLeng+CheckSum ... http://blog.csdn.net/tigerii/archive/2009/08/18/4460292.aspx 发现TClientDataSet.Locate函数有bug,请各位大侠援手,十分感谢! 做程序员的怎么PLMM怎么少呀。。。。 一题面试题做不出来 关于树形结构 求代码或帮我改改! 问一个菜鸟问题. 为什么D6启动不了? 如何实现oicq的左键菜单? 急?关于CGI问题 在线等待 fastreport 报表中如何知道哪些页已经打印哪些页没有打印? 你会用ASP操作Paradox(*.db)数据库吗?请看。 多线程中使用局部不定长数组问题 Delphi调用C#写的DLL,string[]参数类型如何转换?
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPServer,StrUtils;type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
Button3: TButton;
IdTCPServer1: TIdTCPServer;
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
procedure DisplayData();
procedure AddLogEntry();
end;var
Form1: TForm1;
FLogEntry:string;
FReceived:string;implementation{$R *.dfm} procedure TForm1.DisplayData();
begin
edit2.Text := FReceived;
end;procedure TForm1.AddLogEntry();
begin
self.ListBox1.Items.Add(FLogEntry);
end;procedure TForm1.Button3Click(Sender: TObject);
begin
close;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
IdTCPServer1.DefaultPort := StrToInt(edit1.Text);
IdTCPServer1.Active := True;
self.Button1.Enabled := False;
self.Button2.Enabled := True;
self.ListBox1.Items.Add('服务器已成功启动!');
end;procedure TForm1.Button2Click(Sender: TObject);
begin
IdTCPServer1.Active := False;
self.Button1.Enabled := True;
self.Button2.Enabled := False;
self.ListBox1.Items.Add('服务器已成功停止!');
end;procedure TForm1.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;procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
self.ListBox1.Items.Add('来自主机 '
+ AThread.Connection.Socket.Binding.PeerIP
+ ' 的连接请求已被接纳!');
AThread.Connection.WriteLn('100: 欢迎连接到简单TCP服务器!');
end;end.
比如: CmdType+PackSize+DataLeng+CheckSum ...