やりたいこと
以下のpythonコードと同じことをC#でもやりたいのですがうまくいかないので助けていただきたいです。
やっているのはM-SEARCHってやつらしいです。
Python
1import socket 2 3host = '239.255.255.250' 4port = 1900 5messages = "M-SEARCH * HTTP/1.1\r\n" +\ 6 "HOST: 239.255.255.250:1900\r\n" +\ 7 "MX: 5\r\n" +\ 8 "MAN: \"ssdp:discover\"\r\n" +\ 9 "ST: upnp:rootdevice\r\n\r\n\r\n" 10sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 11sock.sendto(messages.encode(), (host, port)) 12while True: 13 try: 14 res = sock.recv(4096) 15 print(res.decode()) 16 except socket.timeout: 17 break 18sock.close()
問題のC#のコード
C#
1Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 2 3 try 4 { 5 IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900); 6 client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000); 7 8 string str = 9 "M-SEARCH * HTTP/1.1\r\n" + 10 "HOST: 239.255.255.250:1900\r\n" + 11 "MX: 5\r\n" + 12 "MAN: \"ssdp:discover\"\r\n" + 13 "ST: upnp:rootdevice\r\n"; 14 byte[] q = Encoding.UTF8.GetBytes(str); 15 16 client.SendTo(q, q.Length, SocketFlags.None, endPoint); 17 IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); 18 EndPoint senderEP = sender; 19 20 List<string> strList = new List<string>(); 21 while (true) 22 { 23 try 24 { 25 byte[] data = new byte[1024]; 26 client.ReceiveFrom(data, ref senderEP); 27 Console.WriteLine(Encoding.UTF8.GetString(data)); 28 } 29 catch(Exception) 30 { 31 break; 32 } 33 } 34 } 35 catch(Exception e) 36 { 37 Console.WriteLine(e.Message); 38 } 39 finally 40 { 41 if (client != null) 42 client.Close(); 43 }
#うまくいっていない部分の説明
こちらがPythonの実行結果です。デバイスが4つ見つかっています。
Python
1HTTP/1.1 200 OK 2CACHE-CONTROL: max-age=120 3Location: http://192.168.0.1:2869/upnp/rootdevice.xml 4SERVER: IGD-HTTP/1.1 UPnP/1.0 UPnP-Device-Host/1.0 5ST: upnp:rootdevice 6EXT: 7 8HTTP/1.1 200 OK 9Location: http://192.168.0.9:49154/MediaRenderer/desc.xml 10Cache-Control: max-age=1800 11Content-Length: 0 12Server: Linux/3.2 UPnP/1.0 Network_Module/1.0 (RX-V485) 13EXT: 14ST: upnp:rootdevice 15 16HTTP/1.1 200 OK 17CACHE-CONTROL: max-age=1800 18DATE: Tue, 07 Dec 2021 14:30:18 GMT 19EXT: 20LOCATION: http://192.168.0.4:60606/***/Server0/ddd 21SERVER: Linux/4.0 UPnP/1.0 Panasonic-UPnP-MW/1.0 22ST: upnp:rootdevice 23 24HTTP/1.1 200 OK 25CACHE-CONTROL: max-age=1800 26DATE: Tue, 07 Dec 2021 14:30:00 GMT 27EXT: 28LOCATION: http://192.168.0.2:38400/***/description.xml 29SERVER: NetBSD/1.6.1, UPnP/1.0, Portable SDK for UPnP devices/1.6.6, DLNADOC/1.50 30ST: upnp:rootdevice 31
こちらはC#の実行結果です。デバイスが2つしか見つけられていません...なぜ...
C#
1HTTP/1.1 200 OK 2CACHE-CONTROL: max-age=120 3Location: http://192.168.0.1:2869/upnp/rootdevice.xml 4SERVER: IGD-HTTP/1.1 UPnP/1.0 UPnP-Device-Host/1.0 5ST: upnp:rootdevice 6EXT: 7 8 9HTTP/1.1 200 OK 10Location: http://192.168.0.9:49154/MediaRenderer/desc.xml 11Cache-Control: max-age=1800 12Content-Length: 0 13Server: Linux/3.2 UPnP/1.0 Network_Module/1.0 (RX-V485) 14EXT: 15ST: upnp:rootdevice
#修正履歴
##2021/12/08
修正前
Python
1messages = "M-SEARCH * HTTP/1.1\r\n" + 2 "HOST: 239.255.255.250:1900\r\n" + 3 "MX: 5\r\n" + 4 "MAN: \"ssdp:discover\"\r\n" + 5 "ST: upnp:rootdevice\r\n";
C#
1string str = 2 "M-SEARCH * HTTP/1.1\r\n" + 3 "HOST: 239.255.255.250:1900\r\n" + 4 "MX: 5\r\n" + 5 "MAN: \"ssdp:discover\"\r\n" + 6 "ST: upnp:rootdevice\r\n"; 7 8while (true) 9 { 10 try 11 { 12 byte[] data = new byte[1024]; 13 client.ReceiveFrom(data, ref senderEP); 14 Console.WriteLine(Encoding.UTF8.GetString(data)); 15 } 16 catch(Exception) 17 { 18 break; 19 } 20 }
修正後
Python
1messages = "M-SEARCH * HTTP/1.1\r\n" +\ 2 "HOST: 239.255.255.250:1900\r\n" +\ 3 "MX: 5\r\n" +\ 4 "MAN: \"ssdp:discover\"\r\n" +\ 5 "ST: upnp:rootdevice\r\n\r\n\r\n"
C#
1string str = 2 "M-SEARCH * HTTP/1.1\r\n" + 3 "HOST: 239.255.255.250:1900\r\n" + 4 "MX: 5\r\n" + 5 "MAN: \"ssdp:discover\"\r\n" + 6 "ST: upnp:rootdevice\r\n\r\n\r\n"; 7 8while (true) 9 { 10 try 11 { 12 byte[] data = new byte[1024]; 13 client.ReceiveFrom(data, ref senderEP); 14 strList.Add(Encoding.UTF8.GetString(data)); 15 Console.WriteLine(Encoding.UTF8.GetString(data)); 16 } 17 catch(SocketException) 18 { 19 break; 20 } 21 }
回答1件
あなたの回答
tips
プレビュー