比如字符数组类的,不确定大小的 

解决方案 »

  1.   


    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.
      

  2.   

    可以,把数据分割后多次发送,至于怎样合并,就看你功力了,UDP的话一般都是在分割时标记序号,收到后再合并。
      

  3.   

    不确定大小,最好从包的设计着手.  
    比如: CmdType+PackSize+DataLeng+CheckSum ...
      

  4.   

    http://blog.csdn.net/tigerii/archive/2009/08/18/4460292.aspx