Có vài thiết bị uPNP trong mạng của tôi. Tôi đang cố gửi yêu cầu M-SEARCH tới mạng và hy vọng nhận được một số phản hồi từ mạng đó. Đây là những gì tôi đang cố gắng:Tôi có thể sử dụng IdUDPClient để gửi yêu cầu M-SEARCH không?
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;
Tôi không nhận được gì từ ứng dụng khách UDP. Tôi sử dụng Wireshark để theo dõi lưu lượng mạng và không có tin nhắn nào được gửi từ máy chủ của tôi.
Bất kỳ ý tưởng nào? Cảm ơn bạn.
Tôi tìm thấy câu trả lời cuối cùng:
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.
tôi thêm một AppendLine mới() đối tượng stringbuilder, nhưng tôi cũng không nhận được bất kỳ phản hồi nào. Tôi cũng không nhận thấy yêu cầu gửi từ máy chủ của tôi khi theo dõi lưu lượng truy cập qua WireShark. –
Bạn có chắc chắn rằng '239.255.255.250' là IP phát sóng chính xác cho mạng LAN của bạn không? IP địa phương và subnet nào được gán cho PC của khách hàng của bạn? –
Nếu tôi phát sóng tới 239.255.255.255, tôi có thể thấy lưu lượng truy cập được gửi từ máy chủ của tôi. Nhưng phát sóng tới 239.255.255.250, không phát hiện thấy lưu lượng truy cập nào. –