{"id":286373,"date":"2017-05-15T19:00:09","date_gmt":"2017-05-15T15:00:09","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=286373"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2017-05-15T15:00:09","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=286373","title":{"rendered":"\u041f\u043e\u0438\u0441\u043a \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u0432 \u0441\u0435\u0442\u0438 \u043f\u043e SSDP \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Poco"},"content":{"rendered":"<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0437\u0430\u043c\u0435\u0442\u043a\u0435-\u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u044f \u043e\u043f\u0438\u0448\u0443 \u043a\u0430\u043a \u043d\u0430\u0439\u0442\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0432 \u0441\u0435\u0442\u0438 \u043f\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443 <b>SSDP<\/b>(Simple Service Discovery Protocol) \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 Poco \u043d\u0430 C++.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u041e\u0433\u043e\u0432\u043e\u0440\u044e, \u0447\u0442\u043e \u0432 \u043f\u043b\u0430\u0442\u043d\u0443\u044e \u043f\u043e\u043b\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e Poco \u0432\u0445\u043e\u0434\u044f\u0442 \u043a\u043b\u0430\u0441\u0441\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b UpnP.<br \/>  \u041d\u043e \u0434\u043b\u044f \u043c\u043e\u0438\u0445 \u0446\u0435\u043b\u0435\u0439 \u0432\u043f\u043e\u043b\u043d\u0435 \u0445\u0432\u0430\u0442\u0438\u043b\u043e \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 Poco, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0438 \u0442\u0430\u043a \u0443\u043c\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 UDP.<\/p>\n<p>  \u041d\u0430 \u0441\u0447\u0435\u0442 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 SSDP, \u043e\u043d \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u0442\u0430\u0440\u044b\u0439 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439 \u043f\u043e \u043d\u0435\u043c\u0443 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u044f \u0441\u043c\u043e\u0433 \u043d\u0430\u0439\u0442\u0438 \u043e\u043a\u0430\u0437\u0430\u043b\u0441\u044f <a href=\"https:\/\/tools.ietf.org\/html\/draft-cai-ssdp-v1-03\">\u0447\u0435\u0440\u043d\u043e\u0432\u0438\u043a \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438<\/a><br \/>  \u0421 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e\u0439 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0431\u0443\u043a\u043e\u0432\u043e\u043a. \ud83d\ude09<\/p>\n<p>  \u0421\u0443\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f:<br \/>  \u041f\u043e\u0441\u043b\u0430\u0442\u044c \u0432 \u0441\u0435\u0442\u0438 \u0448\u0438\u0440\u043e\u043a\u043e\u0432\u0435\u0449\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439(broadcast) \u0437\u0430\u043f\u0440\u043e\u0441 \u2014 UDP \u043f\u0430\u043a\u0435\u0442 \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 239.255.255.250, \u043f\u043e\u0440\u0442 \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f 1900.<br \/>  \u0421\u0430\u043c\u043e \u0442\u0435\u043b\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430(\u043f\u0430\u043a\u0435\u0442\u0430) \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u043a\u043e\u0434\u0435. \u041e\u0433\u043e\u0432\u043e\u0440\u044e\u0441\u044c, \u0447\u0442\u043e \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u043f\u043e\u043b\u0435\u043c, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043c\u0435\u043d\u044f \u044d\u0442\u043e ST: \u0432 \u043d\u0435\u043c \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0442\u0438\u043f \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0442\u0432\u0435\u0442.<br \/>  \u0422\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b UDP, \u0442\u0443\u0442 \u043d\u0435\u0442 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043e\u0442\u0432\u0435\u0442\u0430 \u043a\u0430\u043a \u0432\u044b \u043c\u043e\u0433\u043b\u0438 \u043f\u0440\u0438\u0432\u044b\u043a\u043d\u0443\u0442\u044c \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 HTTP. HTTP \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 \u0437\u0430\u043f\u0440\u043e\u0441-\u043e\u0442\u0432\u0435\u0442.<br \/>  \u0412 \u043d\u0430\u0448\u0435\u043c \u0436\u0435 \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u0441\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0430\u043d\u043e\u043d\u0441\u0438\u0440\u0443\u044e\u0442 \u0441\u0435\u0431\u044f \u0432 \u0441\u0435\u0442\u044c, \u043f\u043e\u0441\u044b\u043b\u0430\u044e\u0442 UDP \u043f\u0430\u043a\u0435\u0442 \u0432 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u0430\u0434\u0440\u0435\u0441 \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u044b\u043b \u043f\u043e\u0441\u043b\u0430\u043d \u0437\u0430\u043f\u0440\u043e\u0441, \u0412\u0410\u0416\u041d\u041e, \u043e\u0442\u0432\u0435\u0442 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043d\u0435 \u043d\u0430 1900 \u043f\u043e\u0440\u0442, \u0430 \u043d\u0430 \u043f\u043e\u0440\u0442 \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u044b\u043b \u043f\u043e\u0441\u043b\u0430\u043d \u0437\u0430\u043f\u0440\u043e\u0441(Source Port)<br \/>  \u0422\u0430\u043a \u043a\u0430\u043a UPD \u043d\u0435 \u0434\u0430\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0439 \u043a\u0440\u043e\u043c\u0435 \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0441\u0442\u0438 \u0441\u0430\u043c\u0438\u0445 \u043f\u0430\u043a\u0435\u0442\u043e\u0432.<br \/>  \u0422\u043e \u0431\u0443\u0434\u0435\u043c \u043d\u0430 \u043f\u0440\u043e\u0442\u044f\u0436\u0435\u043d\u0438\u0438 3 \u0441\u0435\u043a\u0443\u043d\u0434 \u0441\u043b\u0443\u0448\u0430\u0442\u044c Socket(\u043f\u043e\u0440\u0442) \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u044b\u043b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d \u0437\u0430\u043f\u0440\u043e\u0441.<\/p>\n<p>  \u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u043e\u0442\u0432\u0435\u0442\u044b, \u0430 \u043f\u043e\u0442\u043e\u043c \u043f\u0430\u0440\u0441\u0438\u043c \u043e\u0442\u0432\u0435\u0442\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0441 \u0442\u043e\u0439 \u0436\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 Poco.<\/p>\n<p>  \u0415\u0441\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043b\u0443\u0448\u0430\u0442\u044c MulticastSocket, \u044d\u0442\u043e\u0442 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d <a href=\"https:\/\/pocoproject.org\/slides\/200-Network.pdf\">\u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043a Poco<\/a> \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 17.<br \/>  \u041d\u043e \u043c\u043d\u0435 \u043e\u043d \u043d\u0435 \u043f\u043e\u0434\u043e\u0448\u0435\u043b, \u0442\u0430\u043a \u043a\u0430\u043a \u0438\u0441\u043a\u043e\u043c\u043e\u0435 \u043c\u043d\u043e\u0439 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u043d\u0435 \u0430\u043d\u043e\u043d\u0441\u0438\u0440\u0443\u044e\u0442 \u0441\u0435\u0431\u044f \u0432 \u0441\u0435\u0442\u044c.<\/p>\n<p>  \u0412 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043f\u043e\u043b\u0435 ST \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f  <\/p>\n<ul>\n<li>upnp:rootdevice<\/li>\n<li>ssdp:all<\/li>\n<\/ul>\n<p>  \u042d\u0442\u043e \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0432\u0441\u0435\u0445 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432.<br \/>  \u0412 \u043c\u043e\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0437\u0434\u0435\u0441\u044c \u044f \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0445\u043e\u0447\u0443 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c.<br \/>  \u041d\u043e \u0434\u043b\u044f \u0441\u0442\u0430\u0442\u044c\u0438 \u044f \u043e\u0441\u0442\u0430\u0432\u0438\u043b upnp:rootdevice<\/p>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u043e\u0433\u043e\u0432\u043e\u0440\u044e\u0441\u044c, \u0447\u0442\u043e C++ \u0434\u043b\u044f \u043c\u0435\u043d\u044f \u043d\u043e\u0432\u044b\u0439 \u044f\u0437\u044b\u043a.<\/p>\n<p>  \u0418\u0442\u0430\u043a:  <\/p>\n<pre><code class=\"cpp\">#include &lt;iostream&gt;  #include &quot;Poco\/Net\/DatagramSocket.h&quot; #include &quot;Poco\/Net\/SocketAddress.h&quot; #include &quot;Poco\/Timespan.h&quot; #include &quot;Poco\/Exception.h&quot; #include &quot;Poco\/RegularExpression.h&quot; #include &quot;Poco\/String.h&quot;  using std::string; using std::vector; using std::cin; using std::cout; using std::endl;  using Poco::Net::SocketAddress; using Poco::Net::DatagramSocket; using Poco::Timespan; using Poco::RegularExpression;  void MakeSsdpRequest(vector&lt;string&gt;& responses,string st = &quot;&quot;) { \tif (st.empty()) st = &quot;upnp:rootdevice&quot;; \t\/\/if (st.empty()) st = &quot;ssdp:all&quot;;  \tstring message = &quot;M-SEARCH * HTTP\/1.1\\r\\n&quot;; \tmessage += &quot;HOST: 239.255.255.250:1900\\r\\n&quot;; \tmessage += &quot;ST:&quot; + st + &quot;\\r\\n&quot;; \tmessage += &quot;MAN: \\&quot;ssdp:discover\\&quot;\\r\\n&quot;; \tmessage += &quot;MX:1\\r\\n\\r\\n&quot;;  \tDatagramSocket dgs; \tSocketAddress destAddress(&quot;239.255.255.250&quot;, 1900); \tdgs.sendTo(message.data(), message.size(), destAddress); \tdgs.setSendTimeout(Timespan(1, 0)); \tdgs.setReceiveTimeout(Timespan(3, 0)); \tchar buffer[1024]; \ttry { \t\t\/\/ \u0417\u0434\u0435\u0441\u044c \u043c\u043e\u0436\u043d\u043e \u0438 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0446\u0438\u043a\u043b, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u0442\u0432\u0430\u043b\u0438\u043c\u0441\u044f \u043f\u043e timeout. \u041d\u043e \u043d\u0430 \u0432\u0441\u044f\u043a\u0438\u0439 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u044e 1000 \u043f\u0430\u043a\u0435\u0442\u0430\u043c\u0438, \u0442\u0430\u043a \u043a\u0430\u043a, \u0435\u0441\u043b\u0438 \u043a\u0442\u043e-\u0442\u043e \u0440\u0435\u0448\u0438\u0442 \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e, timeout \u043d\u0435 \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u0442. \t\tfor (int i = 0; i &lt; 1000; i++) { \t\t\tint n = dgs.receiveBytes(buffer, sizeof(buffer)); \t\t\tbuffer[n] = '\\0'; \t\t\tresponses.push_back(string(buffer)); \t\t} \t} \tcatch (Poco::TimeoutException) { } }  string ParseIP(string str) { \ttry { \t\tRegularExpression re(&quot;(location:.*:\/\/)([a-zA-Z_0-9\\.]*)([:\/])&quot;, RegularExpression::RE_CASELESS); \t\tvector&lt;string&gt; vec; \t\tre.split(str, 0, vec); \t\tif (vec.size() &gt; 2) return vec[2]; \t} \tcatch (const Poco::RegularExpressionException) { cout &lt;&lt; &quot;RegularExpressionException&quot; &lt;&lt; endl; } \treturn &quot;&quot;; }  int main() { \tvector&lt;string&gt; ips, responses; \tMakeSsdpRequest(responses);  \tfor (string response : responses) { \t\t\/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u044e \u0441\u0442\u0430\u0442\u0443\u0441 \u043e\u0442\u0432\u0435\u0442\u0430. \t\tif (response.find(&quot;HTTP\/1.1 200 OK&quot;, 0) == 0) { \t\t\tstring ip = ParseIP(response); \t\t\tif (!ip.empty()) ips.push_back(ip); \t\t} \t}  \tsort(ips.begin(), ips.end()); \tips.erase(unique(ips.begin(), ips.end()), ips.end()); \tfor (string ip : ips) { \t\tcout &lt;&lt; &quot;IP: &quot; &lt;&lt; ip &lt;&lt; endl; \t}  \tcout &lt;&lt; &quot;Press Enter&quot; &lt;&lt; endl; \tcin.get(); \treturn  0; } <\/code><\/pre>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habrahabr.ru\/post\/328726\/\"> https:\/\/habrahabr.ru\/post\/328726\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0437\u0430\u043c\u0435\u0442\u043a\u0435-\u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u044f \u043e\u043f\u0438\u0448\u0443 \u043a\u0430\u043a \u043d\u0430\u0439\u0442\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0432 \u0441\u0435\u0442\u0438 \u043f\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443 <b>SSDP<\/b>(Simple Service Discovery Protocol) \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 Poco \u043d\u0430 C++.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-286373","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/286373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=286373"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/286373\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=286373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=286373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=286373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}