delphi - 我可以使用IdUDPClient发送M-SEARCH请求吗?

标签 delphi indy ssdp

我的网络中几乎没有 uPNP 设备。我正在尝试向网络发送 M-SEARCH 请求,并希望收到一些响应。这就是我正在尝试的:

var sIP, sOut: string;
    iPort: Word;
    S: TStringBuilder;
begin
  S := TStringBuilder.Create;
  try
    S.Append('M-SEARCH * HTTP/1.1').AppendLine
     .Append('HOST: 239.255.255.250:1900').AppendLine
     .Append('MAN: "ssdp:discover"').AppendLine
     .Append('MX: 10').AppendLine
     .Append('ST: ssdp:all').AppendLine;

    IdUDPClient1.ReceiveTimeout := 3000;
    IdUDPClient1.Broadcast(S.ToString, 1900, '239.255.255.250');
    sOut := IdUDPClient1.ReceiveString(sIP, iPort);
    Memo1.Lines.Add(sIP);
    Memo1.Lines.Add(IntToStr(iPort));
    Memo1.Lines.Add(sOut);
  finally
    S.Free;
  end;
end;

我没有从 UDP 客户端收到任何信息。我使用 Wireshark 监控网络流量,但我的主机没有发送任何消息。

有什么想法吗?谢谢。

我终于找到答案了:

uses
  System.SysUtils, IdUDPClient, IdStack;

var S: TStringBuilder;
    U: TIdUDPClient;
    iPeerPort: Word;
    sPeerIP, sResponse: string;
begin
  U := TIdUDPClient.Create(nil);
  S := TStringBuilder.Create;
  try
    S.Append('M-SEARCH * HTTP/1.1').AppendLine
     .Append('HOST: 239.255.255.250:1900').AppendLine
     .Append('MAN: "ssdp:discover"').AppendLine
     .Append('MX: 3').AppendLine
     .Append('ST: ssdp:all').AppendLine
     .AppendLine;

    U.BoundIP := GStack.LocalAddress;
    U.Send('239.255.255.250', 1900, S.ToString);

    U.ReceiveTimeout := 1000;
    repeat
      sResponse := U.ReceiveString(sPeerIP, iPeerPort);
      if iPeerPort <> 0 then begin
        WriteLn(Format('%s:%d', [sPeerIP, iPeerPort]));
        WriteLn(sResponse);
      end;
    until iPeerPort = 0;
    ReadLn;
  finally
    S.Free;
    U.Free;
  end;
end.

最佳答案

在字符串生成器末尾调用两次AppendLine()。 HTTP 请求 header 由两个 CRLF 对终止,但您仅附加一对,因此您发送的是不完整的请求。

关于delphi - 我可以使用IdUDPClient发送M-SEARCH请求吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10909462/

相关文章:

java - 无法使用 SSDP 接收正确的 UDP 数据包

forms - 在同一应用程序中,以另一种独立的形式更改控件的状态

Delphi XE2 DataSnap - 访问服务器方法模块中的 REST 连接属性

delphi - "Delphi is created in California, so we discourage the use of notation"是什么意思?

用于 Delphi 的 Javascript 引擎

delphi - 无法将 Indy 更新到最新版本

delphi - Indy tcp 服务器 - 确定服务器的 IP 和端口

delphi - TIdHTTP.Get 超时,而使用 Postman 完成的相同调用成功 : possible reasons?

python - 如何在 Python 中使用套接字响应 SSDP 搜索?

embedded - SSDP用于设备发现