概要
cscの作法、調べてみた。
udpを叩いてみた。
練習問題やってみた。
練習問題
upnpのm-searchを実装せよ。
サンプルコード
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace ConsoleTest
{
class Program {
static void Main(string[] args) {
UdpClient udp = new UdpClient(50000);
udp.EnableBroadcast = true;
string str = "M-SEARCH * HTTP/1.1\r\n" +
"HOST: 239.255.255.250:1900\r\n" +
"MX: 5\r\n" +
"MAN: \"ssdp:discover\"\r\n" +
"ST: upnp:rootdevice\r\n\r\n\r\n";
byte[] sendBytes = Encoding.UTF8.GetBytes(str);
//while (true)
{
udp.Send(sendBytes, sendBytes.Length, new IPEndPoint(IPAddress.Broadcast, 1900));
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] receiveBytes = udp.Receive(ref endPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine("recv: " + returnData.ToString());
}
}
}
}
実行結果
>ms
recv: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
Location: http://192.168.1.1:2869/upnp/rootdevice.xml
SERVER: IGD-HTTP/1.1 UPnP/1.0 UPnP-Device-Host/1.0
ST: upnp:rootdevice
EXT:
USN: uuid:af4238da-50bdc726::upnp:rootdevice
以上。