客户端的udpserver没有响应

时间:2013-05-15 11:12:58

标签: delphi delphi-7 indy

我的服务器表单有stringgride,只接受4个tcp连接用户 为此,我将udpclient放在我的服务器中,将udpserver放在客户端

以服务器形式的tcp-onconnect事件:

   //Server Side has udp client
 procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin

      //when fifth user want to connect to tcpserver 
      //server first ensure at list one of the 4 connected user in grid has disconnected
      //by send udp broadcast to 4 user
      //but in this timeout None of these users do not respond , Although still connected

      IdUDPClient1.Broadcast('IP', 1718);
      For i:=1 To 4 Do
      begin
       IPList.Add(IdUDPClient1.ReceiveString(200););
      end;
end;


//Client Side has udp server
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
    ABinding: TIdSocketHandle);
begin
 //
 ip:=GetipAddress();
 ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, ip[1], Length(ip));
end;

我在客户端运行ethereal来检查网络并看到udp massage从服务器发送到客户端但客户端没有响应。

请帮我解决一下我的代码问题

1 个答案:

答案 0 :(得分:1)

您的TIdUDPServer.OnUDPRead事件处理程序没有正确的签名,甚至没有关闭。 TIdUDPServer根本不使用TIdPeerThread,只有TIdTCPServer。你需要做更多这样的事情:

服务器:

procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
  //when fifth user want to connect to tcpserver 
  //server first ensure at list one of the 4 connected user in grid has disconnected
  //by send udp broadcast to 4 user
  //but in this timeout None of these users do not respond , Although still connected

  IPList.Clear;
  IdUDPServer1.Broadcast('IP', 1718);
  Sleep(1000);
end;

procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
var
  S: String;
begin
  if AData.Size > 0 then
  begin
    SetLength(S, AData.Size);
    AData.ReadBuffer(S[1], AData.Size);
    IPList.Add(S);
  end;
end;

客户端:

procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
var
  S: String;
begin
  if AData.Size > 0 then
  begin
    SetLength(S, AData.Size);
    AData.ReadBuffer(S[1], AData.Size);
  end;
  if S = 'IP' then
  begin
    S := ABinding.IP;
    if (S = '') or (S = '0.0.0.0') then
      S := GStack.LocalAddress;
    ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, S[1], Length(S));
  end;
end;