procedure TForm1.Button1Click(Sender: TObject);
var
  strarr:array[0..1000] of char;
  Str:string;
  len:integer;
begin
  Str:=Edit2.Text;
  if length(str)=0 then len:=1 else len:=length(str);
  UdpSocket1.RemoteHost:= '127.0.0.1';
  UdpSocket1.RemotePort:='888';
  StrpCopy(strarr, str);
  UdpSocket1.SendBuf(strarr,len);
end;procedure TForm1.UdpSocket1Receive(Sender: TObject; Buf: PAnsiChar;
  var DataLen: Integer);
var
  str:array[0..1000] of char;
begin
  str:='';
  UdpSocket1.ReceiveBuf(str,DataLen);
  Memo1.Lines.Add(str);
end;为什么接收不到数据,receive事件没有触发,怎样才能触发该事件?请高手指点!

解决方案 »

  1.   

    大概写一下;
    1.你的程序好象是自己发,自己收.你最好用两个winsock试.试成之后再用一个试.
    2.UDP协议要求winsock与远程机通讯时必须Bind一个本地端口(声明自己占用了,要不winsock怎么知道用哪个端口与远程机通讯?).
    UdpSocket1.RemoteHost:= '127.0.0.1';
    UdpSocket1.RemotePort:='8088';
    UdpSocket1.localport:='8089';
    UdpSocket1.bind '8089';
    3.另一个winsock:
    UdpSocket1.RemoteHost:= '127.0.0.1';
    UdpSocket1.RemotePort:='8089';
    UdpSocket1.localport:='8088';
    UdpSocket1.bind '8088';
    4.收发测试
      

  2.   

    udpsocket好像没有bind这个方法,用了这个方法后程序出错。