源代码如下【客户端】
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, SHUDPSocket;
type
  TCommBlock = record
    username, pwd, lx: string[100];
  end;
type
  TCommBlock1 = record
    msg: string[100];
  end;
type
  TForm1 = class(TForm)
    UDPSocket: TSHUDPSocket;
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Edit3: TEdit;
    Edit4: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
   private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
  sComm: TCommBlock1;
implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
  dl: TCommBlock;
  i: Integer;
begin
  UDPSocket.Port := 8888;
  UDPSocket.Active := true;
  for i := 0 to 1000 do   //向服务器端发送1000条数据
  begin
    FillChar(dl, SizeOf(TCommBlock), 0);
    dl.username := '字符串1';
    dl.pwd :='字符串2;
    dl.lx := inttostr(i); //记录编号
    UDPSocket.SendBuf(dl, SizeOf(dl), '127.0.0.1',9999);
  end;
end;
end.
服务端代码:
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, SHUDPSocket, StdCtrls;
type
  TCommBlock = record
    username, pwd, lx: string[100];
  end;
type
  TCommBlock1 = record
    msg: string[100];
  end;
type
  TForm1 = class(TForm)
    SHUDPSocket1: TSHUDPSocket;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure SHUDPSocket1DataRead(UDPSocket: TSHUDPSocket;
      const PeerInf: TPeerInfo);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
  dl: TCommBlock;
implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
  SHUDPSocket1.Port := 9999;
  SHUDPSocket1.Active := true;
end;procedure TForm1.SHUDPSocket1DataRead(UDPSocket: TSHUDPSocket;
  const PeerInf: TPeerInfo);
var
  IsRecv: boolean;
  ms: TCommBlock1;
begin
  try
    IsRecv := SHUDPSocket1.RecvBuf(dl, SizeOf(dl));
  except
    IsRecv := false;
  end;
  if IsRecv then
  begin
  memo1.Lines.Add(dl.username+'  '+dl.pwd+'   '+dl.lx);
  end;
end;
end.每次发送 服务端最多只能接受到50条左右  还有950条接受不到 该怎么解决 忘高手能指点迷津!在此谢谢!

解决方案 »

  1.   

    本来就是无连接协议,如果需要,还是用TCP吧
      

  2.   

    如何udp能保证不掉包,还要tcp干嘛?
      

  3.   


    同上.否则就改用TCP吧.UDP必然会掉包,只要在可容忍程度内就无视吧.
      

  4.   

    在发送端发送数据包后延迟20-30毫秒再发送下一包,可解决相当部分UDP服务器丢数据的现象。
      

  5.   

    UDP协议本身没有处理丢包的策略,不保证数据能够到终到达,所以需要在客户端与服务器之间增加相应的丢包重传策略.如果不想那么麻烦就改用TCP协议(如果可能的话).
      

  6.   

    同建议TCP,UDP除了会掉包,还不能保证包的接收顺序。网络环境很好才考虑UDP。