{"id":281967,"date":"2016-12-04T19:50:04","date_gmt":"2016-12-04T16:50:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=281967"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=281967","title":{"rendered":"WebSocket-\u0441\u0435\u0440\u0432\u0435\u0440 \u043b\u0430\u0439\u0442-\u0432\u0435\u0440\u0441\u0438\u044f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/8ca\/a13\/15a\/8caa1315a2f64ff9a3b37f80c9365a94.png\"\/><\/p>\n<p>  \u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u0442\u0435\u043c, \u043a\u043e\u043c\u0443 \u0437\u0430\u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 WebSocket-\u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430 \u0421\u0418.<\/p>\n<p>  <i>\u041d\u0435 \u0437\u043d\u0430\u043b \u043a\u0430\u043a\u0443\u044e \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c \u041a\u0414\u041f\u0412&#8230;<\/i><br \/>  <a name=\"habracut\"><\/a><\/p>\n<p>  \u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435.<\/p>\n<p>  <i>\u0423\u0432\u043b\u0435\u043a\u0430\u044f\u0441\u044c \u043f\u043e\u0441\u0442\u0440\u043e\u0439\u043a\u043e\u0439 \u00ab\u0443\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043c\u0430\u00bb \u043c\u043d\u0435 \u0437\u0430\u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u044b\u0439 web-\u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430 websocket. \u0418\u0434\u0435\u044f \u0431\u044b\u043b\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0438 \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0430, \u043e\u0434\u043d\u0430\u043a\u043e \u044f \u043f\u043e\u043a\u0430 \u0447\u0442\u043e \u0442\u0430\u043a \u0438 \u043d\u0435 \u043f\u043e\u043d\u044f\u043b, \u0447\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u0435\u0435 <i>(\u0434\u043b\u044f \u043c\u0435\u043d\u044f)<\/i>, \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u0439 \u0441\u0435\u0440\u0432\u0435\u0440 \u0438\u043b\u0438 websocket.<\/i> <\/p>\n<p>  \u0412\u043f\u0440\u043e\u0447\u0435\u043c \u0440\u0435\u0447\u044c \u043d\u0435 \u043e\u0431 \u044d\u0442\u043e\u043c, \u0430 \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0432 \u0441\u0435\u0442\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 js-\u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430\u0445, PHP \u0438 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043d\u0430 \u0421++, \u043c\u043d\u0435 \u0436\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043d\u0430 \u0447\u0438\u0441\u0442\u043e\u043c \u0421\u0418. \u041d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0431\u044b\u043b\u0438 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u044b (\u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445) \u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u0438 \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0441\u0432\u043e\u0451 \u0438 \u0437\u0430\u043e\u0434\u043d\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438 websocket.<\/p>\n<p>  <i>\u0417\u0430\u0440\u0430\u043d\u0435\u0435 \u0441\u043a\u0430\u0436\u0443 \u0447\u0442\u043e \u0432 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 <b>\u043d\u0435<\/b> \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0430 \u043f\u043e &quot;<b>wss:\/\/&#8230;<\/b>&quot; (https), <b>\u043d\u0435<\/b> \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u043b\u0438\u043d\u0430 \u00ab\u0442\u0435\u043b\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f\u00bb \u0431\u043e\u043b\u044c\u0448\u0435 125-\u0442\u0438 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0438 <b>\u043d\u0435<\/b> \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 (\u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043b\u0430\u0439\u0442-\u0432\u0435\u0440\u0441\u0438\u044f). <\/i><\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0412\u044b \u0435\u0449\u0451 \u043d\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u044b \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 WebSocket, \u0442\u043e \u0432\u043e\u0442 <a href=\"http:\/\/learn.javascript.ru\/websockets\">\u0442\u0443\u0442<\/a>, <a href=\"https:\/\/habrahabr.ru\/post\/179585\/\">\u0442\u0443\u0442<\/a> \u0438 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/WebSocket\">\u0442\u0443\u0442<\/a> \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e \u0432\u0441\u0451 \u0440\u0430\u0441\u043f\u0438\u0441\u0430\u043d\u043e, \u043d\u0443 \u0438 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 <a href=\"https:\/\/tools.ietf.org\/html\/rfc6455\">RFC 6455<\/a>. \u042f \u0436\u0435 \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u043e\u0434 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e WebSocket-\u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043d\u0430 \u0421\u0418 \u0441 \u043f\u043e\u044f\u0441\u043d\u0435\u043d\u0438\u044f\u043c\u0438 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0438 \u043a\u0430\u043a \u0442\u0430\u043c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442.<\/p>\n<hr\/>\n<p>  \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0442\u0430\u043a\u043e\u0432: \u0441\u0435\u0440\u0432\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u044b\u0439 web-\u0441\u0435\u0440\u0432\u0435\u0440 \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u044f \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u043e\u0440\u0442, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 80-\u044b\u0439, \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u2014 \u0444\u0430\u0439\u043b\u044b index.html, *.css, *.png, *.js, *.ttf. \u041a\u043e\u0433\u0434\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u043e\u0442\u0434\u0430\u0441\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0444\u0430\u0439\u043b <b>index.html<\/b> \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 js-\u043a\u043e\u0434, \u044d\u0442\u043e\u0442 \u043a\u043e\u0434, \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0432, \u0441\u0434\u0435\u043b\u0430\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043f\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443 &quot;<b>ws:\/\/&#8230;<\/b>&quot;, \u0441\u0435\u0440\u0432\u0435\u0440 \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043e\u0442\u0440\u0435\u0430\u0433\u0438\u0440\u0443\u0435\u0442 \u043d\u0430 \u044d\u0442\u043e \u00ab\u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u043c\u00bb \u043e\u0442\u0432\u0435\u0442\u043e\u043c \u0438 <b>\u043d\u0435<\/b> \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435. <br \/>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c <b>Websocket<\/b> \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u0441\u043b\u0435\u0433\u043a\u0430 \u043e\u0442\u0432\u043b\u0435\u0447\u044c\u0441\u044f \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c, \u0442\u043e \u0435\u0441\u0442\u044c \u0441 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u043e\u0439 <i>(\u0432\u043f\u0440\u043e\u0447\u0435\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438 \u0412\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435)<\/i>. \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u0437\u0434\u0435\u0441\u044c \u0443\u0436\u0435 \u0432\u0441\u0451 \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c\u0438 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u043e\u0432 \u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043a\u0443 <i>(index.html)<\/i> \u0441\u043e \u00ab\u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u043c\u00bb <b>js<\/b>-\u043a\u043e\u0434\u043e\u043c\u2026<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">index.html<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">&lt;!DOCTYPE html&gt; &lt;html&gt;     &lt;head&gt;         &lt;meta charset=&quot;utf-8&quot;&gt;         &lt;script src=&quot;https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.1.0\/jquery.min.js&quot;&gt;&lt;\/script&gt;         &lt;style type=&quot;text\/css&quot;&gt;            .knopka {            height: 40px;            width: 120px;            text-align: center;            line-height: 2.1;            cursor: pointer;            border: 1px solid #999;            border-radius: 10px;            }            v{}         &lt;\/style&gt;          &lt;script type=&quot;text\/javascript&quot;&gt;             $(function() {                 window.WebSocket = window.WebSocket || window.MozWebSocket;                 var websocket = new WebSocket('ws:\/\/192.168.5.197:80\/ws');                  websocket.onopen = function () {                     $('h1').css('color', '#65c178'); \/* green *\/                 };                  websocket.onclose = function (e) {                     console.log(&quot;WebSocket: &quot;, e)                     $('h1').css('color', '#fe457e'); \/* red *\/                 };                  websocket.onerror = function () {                     $('h1').css('color', '#fe457e'); \/* red *\/                 };                  websocket.onmessage = function (message) {                     console.log(message.data);                     $('v').append($('&lt;p&gt;', { text: message.data }));                 };                                  $('.knp0').click(function(e) {                     e.preventDefault();                     websocket.send($('input').val());                     $('input').val('');                 });                  $('.knp1').click(function(e) {                     e.preventDefault();                     websocket.send(&quot;hi&quot;);                 });                  $('.knp2').click(function(e) {                     e.preventDefault();                     websocket.send(&quot;ping&quot;);                 });             });         &lt;\/script&gt;         &lt;\/head&gt;     &lt;body&gt;         &lt;h1&gt;WebSocket&lt;\/h1&gt;           &lt;div class='knopka knp1'&gt;Hi...&lt;\/div&gt;&lt;br&gt;           &lt;div class='knopka knp2'&gt;Call PING&lt;\/div&gt;&lt;br&gt;           &lt;div class='knopka knp0'&gt;Send&lt;\/div&gt; &lt;input type=&quot;text&quot; \/&gt;           &lt;v&gt;&lt;\/v&gt;     &lt;\/body&gt; &lt;\/html&gt; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <i>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043e\u043d\u0430 \u0432\u043e\u0442 \u0442\u0430\u043a:<\/i><br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/b45\/394\/b9a\/b45394b9a45b4239a45ce3c1d24cfd75.png\"\/><br \/>  <i>\u0415\u0441\u043b\u0438 \u043d\u0430\u0434\u043f\u0438\u0441\u044c \u0437\u0435\u043b\u0451\u043d\u0430\u044f, \u0442\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e, \u0430 \u0435\u0441\u043b\u0438 \u043a\u0440\u0430\u0441\u043d\u0430\u044f, \u0442\u043e \u043d\u0435\u0442.<\/i> <\/p>\n<p>  \u0418\u0442\u0430\u043a \u043a\u043b\u0438\u0435\u043d\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u043b <b>index.html<\/b> c\u043e \u00ab\u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439\u00bb \u0441\u0442\u0440\u043e\u0447\u043a\u043e\u0439\u2026  <\/p>\n<pre><code class=\"bash\">var websocket = new WebSocket('ws:\/\/192.168.5.197:80\/ws');<\/code><\/pre>\n<p>  \u042d\u0442\u043e \u0438 \u0435\u0441\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043f\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443 <b>websocket<\/b>. \u0412 \u0441\u043a\u043e\u0431\u043a\u0430\u0445 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b, \u0430\u0434\u0440\u0435\u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 (\u0435\u0441\u043b\u0438 \u043f\u043e\u0440\u0442 80-\u044b\u0439, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043d\u0435 \u043f\u0438\u0441\u0430\u0442\u044c), \u0431\u0443\u043a\u0432\u044b \u043f\u043e\u0441\u043b\u0435 \u0441\u043b\u0435\u0448\u0430 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043b\u044e\u0431\u044b\u043c\u0438, \u0430 \u043c\u043e\u0436\u043d\u043e \u0438 \u0432\u043e\u0432\u0441\u0435 \u0431\u0435\u0437 \u043d\u0438\u0445, \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u00ab\u043c\u0430\u044f\u0447\u043e\u043a\u00bb \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430 <i>(\u0441\u043c.\u043d\u0438\u0436\u0435)<\/i>, \u0433\u043e\u0432\u043e\u0440\u044f\u0449\u0438\u0439 \u0435\u043c\u0443, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0441\u043f\u0430\u0440\u0441\u0438\u0442\u044c \u0432\u0435\u0441\u044c \u043f\u0430\u043a\u0435\u0442 \u0438 \u043f\u043e\u0438\u0441\u043a\u0430\u0442\u044c \u0442\u0430\u043c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u2014 <b>Sec-WebSocket-Key:<\/b> \u0441 \u0440\u0430\u043d\u0434\u043e\u043c\u043d\u044b\u043c \u043a\u043b\u044e\u0447\u0451\u043c \u2014 <b>9+4iSUx4slLlng0Xcv2zFw==<\/b> \u0432 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0435 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Base64\">Base64<\/a>.<\/p>\n<p>  \u041e\u0442 \u0440\u0430\u0437\u043d\u044b\u0445 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u043e\u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u043f\u043e \u0440\u0430\u0437\u043d\u043e\u043c\u0443, \u043d\u043e \u0432 \u0446\u0435\u043b\u043e\u043c \u0441\u0443\u0442\u044c \u043e\u0434\u043d\u0430 \u0438 \u0442\u0430 \u0436\u0435, \u0432\u043e\u0442 \u0442\u0430\u043a \u043e\u0442 FireFox:<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/b32\/7e2\/33c\/b327e233c9c54ec08293a5b87678456f.png\"\/><\/p>\n<p>  \u0422\u0430\u043a \u043e\u0442 Chromium:<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/e84\/015\/6d8\/e840156d8daf490d85409aa2a40a107e.png\"\/><\/p>\n<p>  \u0417\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 (\u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0438\u0445 \u0441\u0430\u043c) \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442:<br \/>  \u0417\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 websocket-\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435.<br \/>  \u041a\u043b\u044e\u0447 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0443.<br \/>  \u041f\u043e\u0434\u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u044b \u2014 \u043e\u043d\u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u043c \u0432 \u043d\u0430\u0447\u0430\u043b\u0435. <i>\u0414\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u043e\u043d\u0438 \u043d\u0435 \u0432\u0430\u0436\u043d\u044b.<\/i><br \/>  \u041f\u0440\u043e\u0447\u0430\u044f \u00ab\u0434\u0440\u0435\u0431\u0435\u0434\u0435\u043d\u044c\u00bb.<\/p>\n<p>  <\/p>\n<h3>\u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435<\/h3>\n<p>  <i>\u0414\u0430\u043b\u0435\u0435 \u044f \u0431\u0443\u0434\u0443 \u0441\u043e\u043f\u0440\u043e\u0432\u043e\u0436\u0434\u0430\u0442\u044c \u0442\u0435\u043a\u0441\u0442 \u0432\u044b\u0440\u0435\u0437\u043a\u0430\u043c\u0438 \u0438\u0437 \u043a\u043e\u0434\u0430, \u0430 \u043f\u043e\u0441\u043b\u0435 \u0431\u0443\u0434\u0435\u0442 \u043a\u043e\u0434 \u0446\u0435\u043b\u0438\u043a\u043e\u043c&#8230;<\/i><\/p>\n<p>  \u0421\u0435\u0440\u0432\u0435\u0440, \u043f\u043e\u043b\u0443\u0447\u0438\u0432 \u00ab\u043c\u0430\u044f\u0447\u043e\u043a\u00bb (ws) \u0440\u0430\u0441\u043f\u0430\u0440\u0441\u0438\u0432\u0430\u0435\u0442 \u0432\u0435\u0441\u044c \u043f\u0430\u043a\u0435\u0442, \u0432\u044b\u0447\u043b\u0435\u043d\u044f\u0435\u0442 \u0438\u0437 \u043d\u0435\u0433\u043e \u043a\u043b\u044e\u0447 \u2014 <b>9+4iSUx4slLlng0Xcv2zFw==<\/b> <i>(\u043a\u043b\u044e\u0447 \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b)<\/i> \u0438 \u043a\u043b\u0430\u0434\u0451\u0442 \u0435\u0433\u043e \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 <b>resultstr<\/b> <i>(\u0441\u043c. \u043d\u0438\u0436\u0435)<\/i>. <br \/>  \u0422\u0435\u043f\u0435\u0440\u044c \u043a \u043a\u043b\u044e\u0447\u0443 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0438\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 <i>(<a href=\"https:\/\/ru.wikipedia.org\/wiki\/GUID\">GUID<\/a>)<\/i> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u0443\u044e \u0432 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 RFC 6455:  <\/p>\n<pre><code class=\"cpp\">char GUIDKey[] = &quot;258EAFA5-E914-47DA-95CA-C5AB0DC85B11&quot;; \/\/36<\/code><\/pre>\n<p>  <i>\u042d\u0442\u043e \u0441\u0442\u0440\u043e\u043a\u0430 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f.<\/i><\/p>\n<p>  \u0421\u043a\u043b\u0435\u0438\u0432\u0430\u0435\u043c \u043a\u043b\u044e\u0447 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u044b\u0439 \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0430 \u0441 <b>GUIDKey<\/b>:<\/p>\n<pre><code class=\"cpp\">strcat(resultstr, GUIDKey);<\/code><\/pre>\n<p>  \u0412 \u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0432\u043e\u0442 \u0442\u0430\u043a\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443:<br \/>  <b>9+4iSUx4slLlng0Xcv2zFw==258EAFA5-E914-47DA-95CA-C5AB0DC85B11<\/b><\/p>\n<p>  \u0411\u043b\u043e\u043a \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u0437\u0430 \u044d\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f:  <\/p>\n<pre><code class=\"cpp\">    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ WS \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/     else if((strstr(str_from_buf, &quot;GET \/ws &quot;)) != NULL)       {        warning_access_log(buffer);         if((p = strstr(buffer, &quot;Sec-WebSocket-Key:&quot;)) != NULL)         {                                                             char resultstr[64] = {0,};           int i = 0, it = 0;           for(i = 19; it &lt; 24; i++, it++)            {              resultstr[it] = p[i];            }            strcat(resultstr, GUIDKey); <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0441\u043a\u043b\u0435\u0435\u043d\u043d\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <b>SHA1((unsigned char *)resultstr, strlen(resultstr), temp);<\/b> <i>(\u043e\u043d\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0430 \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435 #include &lt;openssl\/sha.h&gt;)<\/i> \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0443\u0436\u0435 <b>\u0445\u0435\u0448-\u0441\u0443\u043c\u043c\u0443<\/b> \u044d\u0442\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438\u2026<\/p>\n<pre><code class=\"cpp\">\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/sha1\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/           unsigned char temp[SHA_DIGEST_LENGTH] = {0,};           char buf[SHA_DIGEST_LENGTH*2] = {0,};            SHA1((unsigned char *)resultstr, strlen(resultstr), temp);            for(i=0; i &lt; SHA_DIGEST_LENGTH; i++)             {              sprintf((char*)&(buf[i*2]), &quot;%02x&quot;, temp[i]);            } <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <b>base64_encode(temp, key_out, sizeof(temp));<\/b> \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u043c <b>\u0445\u0435\u0448-\u0441\u0443\u043c\u043c\u0443<\/b> \u0432 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0443 Base64 \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u043a\u043b\u044e\u0447 \u2014 <b>s3pPLMBiTxaQ9kYGzzhZRbK+xOo=<\/b> (\u043e\u043d \u0442\u043e\u0436\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b)<\/p>\n<pre><code class=\"cpp\">\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/Base64\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/            unsigned char key_out[64] = {0,};           base64_encode(temp, key_out, sizeof(temp));<\/code><\/pre>\n<p>  \u041d\u0443 \u0438 \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0442\u0432\u0435\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0443\u2026<\/p>\n<pre><code class=\"cpp\">sem_init(&sem, 0, 0); char resp[131] = {0,}; snprintf(resp, 130, &quot;%s%s%s&quot;, response_ws, key_out, &quot;\\r\\n\\r\\n&quot;); if(send(client_fd, resp, sizeof(char) * strlen(resp), MSG_NOSIGNAL) == -1) warning_access_log(&quot;send response_ws.&quot;); <\/code><\/pre>\n<p>  \u2026 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cpp\">HTTP\/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= <\/code><\/pre>\n<p>  <i>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0438\u043b\u0438\u0441\u044c \u043d\u0430 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b WS \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u043c\u0443 \u043d\u0430\u0448 \u043a\u043b\u044e\u0447. \u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 <b>\u043d\u0435<\/b> \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a.<\/i><\/p>\n<pre><code class=\"cpp\">\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ START WS \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/           if(pthread_create(&ws_thread, NULL, &ws_func, &client_fd) != 0) error_log(&quot;creating WS.&quot;);           pthread_detach(ws_thread);           sem_wait(&sem); <\/code><\/pre>\n<p>  \u041f\u043e\u0434\u0432\u0435\u0434\u0451\u043c \u0438\u0442\u043e\u0433 \u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 (\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f):<br \/>  \u0411\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 index.html \u0441\u043e \u00ab\u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u043c\u00bb js-\u043a\u043e\u0434\u043e\u043c, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 ws-\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043a\u043b\u044e\u0447\u0451\u043c-\u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u043c. <br \/>  \u0421\u0435\u0440\u0432\u0435\u0440 \u00ab\u0441\u043a\u043b\u0435\u0438\u0432\u0430\u0435\u0442\u00bb \u044d\u0442\u043e\u0442 \u043a\u043b\u044e\u0447 \u0441\u043e \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u043e\u0439, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 <b>SHA1<\/b> \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0445\u0435\u0448-\u0441\u0443\u043c\u043c\u0443 \u0441\u043a\u043b\u0435\u0435\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438, \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442 \u044d\u0442\u0443 \u0445\u0435\u0448-\u0441\u0443\u043c\u043c\u0443 \u0432 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0443 Base64 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u043c. <br \/>  Websocket \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e.  <\/p>\n<hr\/>\n<p>  <\/p>\n<h3>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 Websocket<\/h3>\n<p>  \u0422\u0443\u0442 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435. \u041e\u0431\u043c\u0435\u043d \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0445\u0438\u0442\u0440\u043e\u0443\u043c\u043d\u044b\u0445 \u0444\u0440\u0435\u0439\u043c\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"bash\">     0                   1                   2                   3     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1    +-+-+-+-+-------+-+-------------+-------------------------------+    |F|R|R|R| \u043e\u043f\u043a\u043e\u0434 |\u041c| \u0414\u043b\u0438\u043d\u0430 \u0442\u0435\u043b\u0430  |    \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u043d\u0430\u044f \u0434\u043b\u0438\u043d\u0430 \u0442\u0435\u043b\u0430     |    |I|S|S|S|(4\u0431\u0438\u0442\u0430)|\u0410|   (7\u0431\u0438\u0442)    |            (1 \u0431\u0430\u0439\u0442)           |    |N|V|V|V|       |\u0421|             |(\u0435\u0441\u043b\u0438 \u0434\u043b\u0438\u043d\u0430 \u0442\u0435\u043b\u0430==126 \u0438\u043b\u0438 127) |    | |1|2|3|       |\u041a|             |                               |    | | | | |       |\u0410|             |                               |    +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +     +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +    |  \u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b \u0442\u0435\u043b\u0430, \u0435\u0441\u043b\u0438 \u0434\u043b\u0438\u043d\u0430 \u0442\u0435\u043b\u0430 = 127    |    + - - - - - - - - - - - - - - - +-------------------------------+     + - - - - - - - - - - - - - - - +-------------------------------+    |                               |  \u041a\u043b\u044e\u0447 \u043c\u0430\u0441\u043a\u0438, \u0435\u0441\u043b\u0438 \u041c\u0410\u0421\u041a\u0410 = 1   |    +-------------------------------+-------------------------------+     +-------------------------------+-------------------------------+    | \u041a\u043b\u044e\u0447 \u043c\u0430\u0441\u043a\u0438 (\u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435)      |       \u0414\u0430\u043d\u043d\u044b\u0435 \u0444\u0440\u0435\u0439\u043c\u0430 (&quot;\u0442\u0435\u043b\u043e&quot;)  |    +-------------------------------- - - - - - - - - - - - - - - - +     +-------------------------------- - - - - - - - - - - - - - - - +    :                     \u0414\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e\u0442\u0441\u044f ...                   :    + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +     + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +    |                     \u0414\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e\u0442\u0441\u044f ...                   |    +---------------------------------------------------------------+ <\/code><\/pre>\n<p>  \u041a\u0430\u0436\u0434\u044b\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u044b\u0439 \u0431\u043b\u043e\u043a \u2014 \u044d\u0442\u043e 32 \u0431\u0438\u0442\u0430.<\/p>\n<p>  \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0441\u0435\u0440\u0432\u0435\u0440 \u0443\u043c\u0435\u0435\u0442 \u043e\u0431\u043c\u0435\u043d\u0438\u0432\u0430\u0442\u044c\u0441\u044f <b>PING<\/b> \u2014 <b>PONG<\/b>\u0430\u043c\u0438 <i>(\u043d\u0443\u0436\u043d\u044b \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f)<\/i> \u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u043c\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f\u043c\u0438 \u0434\u043b\u0438\u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0435 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 125-\u0442\u0438 \u0431\u0430\u0439\u0442 (\u0431\u043e\u043b\u044c\u0448\u0435\u0433\u043e \u043c\u043d\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u043e\u0441\u044c). <\/p>\n<p>  \u0424\u0440\u0435\u0439\u043c \u0441 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a\u2026<\/p>\n<p>  \u041f\u0435\u0440\u0432\u044b\u0439 \u0431\u0430\u0439\u0442:<\/p>\n<pre><code class=\"cpp\">      0 1 2 3 4 5 6 7       +-+-+-+-+-------+      |F|R|R|R| opcode|      |I|S|S|S|  (4)  |      |N|V|V|V|       |      | |1|2|3|       |      +-+-+-+-+-------+ <\/code><\/pre>\n<p>  \u0412\u0442\u043e\u0440\u043e\u0439 \u0431\u0430\u0439\u0442:<\/p>\n<pre><code class=\"cpp\">      0 1 2 3 4 5 6 7       +---------------+      |M| Payload len |      |A|     (7)     |                 |S|             |        |K|             |                   +---------------+ <\/code><\/pre>\n<p>  \u0422\u0440\u0435\u0442\u0438\u0439, \u0447\u0435\u0442\u0432\u0451\u0440\u0442\u044b\u0439, \u043f\u044f\u0442\u044b\u0439 \u0438 \u0448\u0435\u0441\u0442\u043e\u0439 \u0431\u0430\u0439\u0442\u044b \u2014 \u044d\u0442\u043e \u043c\u0430\u0441\u043a\u0430. <br \/>  \u0421\u043b\u0435\u0434\u043e\u043c \u0438\u0434\u0451\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 (\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0435 125-\u044e \u0431\u0430\u0439\u0442\u0430\u043c\u0438).<\/p>\n<p>  <b>\u0412 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0441\u044f, \u0447\u0442\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0442 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0432\u0441\u0435\u0433\u0434\u0430 \u0438\u043c\u0435\u0435\u0442 \u043c\u0430\u0441\u043a\u0443, \u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043a \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0443 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0431\u0435\u0437 \u043c\u0430\u0441\u043a\u0438.<\/b><\/p>\n<p>  <i>\u0412\u043e\u0442 \u0447\u0442\u043e \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 Chromium \u0435\u0441\u043b\u0438 \u0435\u043c\u0443 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435:<\/i><br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/e25\/f1d\/c44\/e25f1dc44c974cc2acda9625fbc7bae9.png\"\/><\/p>\n<p>  <i>\u041e\u0434\u043d\u0430\u043a\u043e \u0435\u0441\u043b\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0443 FireFox, \u0442\u043e \u043e\u0448\u0438\u0431\u043e\u043a \u043d\u0435 \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u0442\u0441\u044f.<\/i> <\/p>\n<p>  \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u0440\u0438\u0441\u043b\u0430\u043b \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u00abHello geektimes\u00bb, \u0442\u043e\u0433\u0434\u0430 \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/cc2\/1ae\/9bb\/cc21ae9bb3fd4961a290cbe7aa6cc8d5.png\"\/><\/p>\n<p>  <b>\u041f\u0440\u0438\u043d\u044f\u0442\u043e<\/b> \u2014 21 \u0431\u0430\u0439\u0442.<br \/>  <b>Opcode: 0x01<\/b> \u2014 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u044d\u0442\u043e \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435.<br \/>  <b>Maska: 0x01<\/b> \u2014 \u043c\u0430\u0441\u043a\u0430 \u0435\u0441\u0442\u044c.<br \/>  <b>Payload_len: 15<\/b> \u2014 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f 15 \u0431\u0430\u0439\u0442.<\/p>\n<p>  \u0412 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f\u2026<\/p>\n<pre><code class=\"cpp\">... #define WS_TEXT_FRAME 0x01 ...  void * ws_func(void *client_arg)   {     ...     while(1)     {       ...        if(rec_b &gt; 0)  \/\/ \u0435\u0441\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438, \u0442\u043e ...                            {           char masking_key[4] = {0,}; \/\/ \u0441\u044e\u0434\u0430 \u043f\u043e\u043b\u043e\u0436\u0438\u043c \u043c\u0430\u0441\u043a\u0443          char opcode; \/\/ \u0441\u044e\u0434\u0430 \u0442\u0438\u043f \u0444\u0440\u0435\u0439\u043c\u0430          int payload_len; \/\/ \u0441\u044e\u0434\u0430 \u0434\u043b\u0438\u043d\u0443 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f (\u0442\u0435\u043b\u0430), \u0442\u043e \u0435\u0441\u0442\u044c \u0431\u0435\u0437 \u0441\u043b\u0443\u0436\u0435\u0431\u043d\u044b\u0445 \u0431\u0430\u0439\u0442\u043e\u0432            opcode = inbuf[0] & 0x0F;               printf(&quot;FIN: 0x%02x\\n&quot;, inbuf[0] & 0x01);             printf(&quot;RSV1: 0x%02x\\n&quot;, inbuf[0] & 0x02);             printf(&quot;RSV2: 0x%02x\\n&quot;, inbuf[0] & 0x03);             printf(&quot;RSV3: 0x%02x\\n&quot;, inbuf[0] & 0x04);             printf(&quot;Opcode: 0x%02x\\n&quot;, inbuf[0] & 0x0F);                                 payload_len = inbuf[1] & 0x7F;              printf(&quot;Maska: 0x%02x\\n&quot;, inbuf[1] & 0x80 ? 1:0);             printf(&quot;Payload_len: %d\\n&quot;, inbuf[1] & 0x7F);           masking_key[0] = inbuf[2];           masking_key[1] = inbuf[3];          masking_key[2] = inbuf[4];          masking_key[3] = inbuf[5];                          char payload[128] = {0,}; \/\/ \u0441\u044e\u0434\u0430 \u043f\u043e\u043b\u043e\u0436\u0438\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435           ...           if(opcode == WS_TEXT_FRAME) \/\/ \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d \u0442\u0435\u043a\u0441\u0442           {             int i = 6, pl = 0;             for(; pl &lt; payload_len; i++, pl++)              {                payload[pl] = inbuf[i]^masking_key[pl % 4]; \/\/ \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u043c\u0430\u0441\u043a\u0443 \u043a \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u043c\u0443 \u0442\u0435\u043a\u0441\u0442\u0443              }                                   printf(&quot;\\nReciv TEXT_FRAME from %d client, payload: %s\\n&quot;, client_fd, payload);             ... <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u0437\u0430\u043a\u0440\u044b\u0442\u044c \u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043a\u0443, \u0442\u043e \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u043e\u0448\u043b\u0451\u0442 \u043e\u043f\u043a\u043e\u0434 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u044f (0x08):<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/cee\/825\/eec\/cee825eecc7941d298c122366c00378a.png\"\/><\/p>\n<pre><code class=\"cpp\">... #define WS_CLOSING_FRAME 0x08 ...         if(opcode == WS_CLOSING_FRAME) \/\/ \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d \u043a\u043e\u0434 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u044f \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f           {             memset(reciv_r, 0, 48);             snprintf(reciv_r, 47, &quot;%s%d\\n&quot;, &quot;Ws_func recive opcod - 0x08, DIE clien - &quot;,  client_fd);             warning_access_log(reciv_r); \/\/ \u043f\u0438\u0448\u0435\u043c \u0441\u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0432 \u043b\u043e\u0433             if(close(client_fd) == -1) error_log(&quot;open file close client_fd ws 2.&quot;); \/\/ \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c             pthread_exit(NULL); \/\/ \u0443\u0431\u0438\u0432\u0430\u0435\u043c \u043f\u043etok           } ... <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u0441\u0435\u0440\u0432\u0435\u0440 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0443 <b>PING<\/b>\u2026<\/p>\n<pre><code class=\"cpp\">...                printf(&quot;\\nPING client - %d\\n&quot;, client_fd);                 char ping[] = {0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f}; \/\/ Ping - \u043d\u0435 \u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439, \u0442\u0435\u043b\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u043b\u043e\u0432\u043e Hello, \u044d\u0442\u0430 \u0436\u0435 \u0441\u043b\u043e\u0432\u043e \u0432\u0435\u0440\u043d\u0451\u0442\u0441\u044f \u0441 \u041f\u043e\u043d\u0433\u043e\u043c                                if(send(client_fd, ping, 7, 0) == -1)                 {                   warning_access_log(&quot;Error PING.&quot;);                    if(close(client_fd) == -1) error_log(&quot;open file close client_fd ws 3.&quot;); \/\/ \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c                   pthread_exit(NULL);                  } ... <\/code><\/pre>\n<p>  \u2026 \u0442\u043e\u0433\u0434\u0430 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u0432 \u043e\u0442\u0432\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442 <b>PONG<\/b>, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0441\u043e\u043e\u0431\u0449\u0438\u0432 \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u043e \u0441\u0432\u043e\u0451\u043c \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0438. \u0412\u043c\u0435\u0441\u0442\u0435 \u0441 PING\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043b\u044e\u0431\u0443\u044e \u0444\u0440\u0430\u0437\u0443, \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u0432\u0435\u0440\u043d\u0451\u0442 \u0435\u0451 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 PONG\u043e\u043c. <\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/da0\/000\/eec\/da0000eecc2646bb90437377fd816ee1.png\"\/><\/p>\n<p>  \u041d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u0441\u044b\u043b\u0430\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 PING\u043e\u043c \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u0444\u0440\u0430\u0437\u0443, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043b\u0430\u0442\u044c \u00ab\u043f\u0443\u0441\u0442\u043e\u0439\u00bb PING\u2026<\/p>\n<pre><code class=\"cpp\">...                char ping[] = {0x89, 0x00};                 if(send(client_fd, ping, 2, 0) == -1)                 {                   warning_access_log(&quot;Error PING.&quot;);                    if(close(client_fd) == -1) error_log(&quot;open file close client_fd ws 3.&quot;); \/\/ \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c                   pthread_exit(NULL);                  } ... <\/code><\/pre>\n<p>  <b>\u041e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c PING \u043c\u043e\u0436\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0435\u0440\u0432\u0435\u0440.<\/b><\/p>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0437\u0430\u043a\u0440\u044b\u0442\u044c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c, \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0435\u043c\u0443 \u043e\u043f\u043a\u043e\u0434 \u2014 <b>0x88<\/b><\/p>\n<pre><code class=\"cpp\">...                char close_client[] = {0x88, 0};                                if(send(client_fd, close_client, 2, 0) == -1)                 {                   warning_access_log(&quot;Error CLOSE.&quot;);                    if(close(client_fd) == -1) error_log(&quot;open file close client_fd ws 4.&quot;); \/\/ \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c                   pthread_exit(NULL);                  } ... <\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0443 \u043a\u0430\u043a\u043e\u0435-\u043b\u0438\u0431\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435, \u043d\u0443\u0436\u043d\u043e \u0432 \u043f\u0435\u0440\u0432\u044b\u0439 \u0431\u0430\u0439\u0442 \u0444\u0440\u0435\u0439\u043c\u0430 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u043f\u043a\u043e\u0434 \u0433\u043e\u0432\u043e\u0440\u044f\u0449\u0438\u0439 \u0447\u0442\u043e \u044d\u0442\u043e \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435, \u0430 \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u043c\u0430\u0441\u043a\u0443 0 \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0431\u0430\u0439\u0442 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0441\u0430\u043c\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435. <br \/>  \u0422\u043e \u0435\u0441\u0442\u044c \u0444\u0440\u0435\u0439\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cpp\">               char hello[] = {0x81, 0x05, 'H', 'e', 'l', 'l', 'o'};                              if(send(client_fd, hello, 7, 0) == -1)                 {                   if(close(client_fd) == -1) error_log(&quot;send hello.&quot;);                    pthread_exit(NULL);                  } <\/code><\/pre>\n<p>  \u041d\u0443 \u0438 \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u043a\u043e\u0434 \u0446\u0435\u043b\u0438\u043a\u043e\u043c\u2026<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;sys\/types.h&gt;  #include &lt;sys\/socket.h&gt; #include &lt;netinet\/in.h&gt; #include &lt;netdb.h&gt; #include &lt;arpa\/inet.h&gt; #include &lt;string.h&gt; #include &lt;sys\/stat.h&gt;  #include &lt;fcntl.h&gt;     #include &lt;time.h&gt; #include &lt;pthread.h&gt; #include &lt;semaphore.h&gt; #include &lt;signal.h&gt; #include &lt;sys\/sendfile.h&gt; #include &lt;openssl\/sha.h&gt;  char response[] = &quot;HTTP\/1.1 200 OK\\r\\n&quot; &quot;Content-Type: text\/html; charset=UTF-8\\r\\n\\r\\n&quot;;  char response_404[] = &quot;HTTP\/1.1 404 Not Found\\r\\n&quot; &quot;Content-Type: text\/html; charset=UTF-8\\r\\n\\r\\n&quot;;  char response_img[] = &quot;HTTP\/1.1 200 OK\\r\\n&quot; &quot;Content-Type: image\/png; charset=UTF-8\\r\\n\\r\\n&quot;;    char response_xicon[] = &quot;HTTP\/1.1 200 OK\\r\\n&quot; &quot;Content-Type: image\/x-icon; charset=UTF-8\\r\\n\\r\\n&quot;;  char response_css[] = &quot;HTTP\/1.1 200 OK\\r\\n&quot; &quot;Content-Type: text\/css; charset=UTF-8\\r\\n\\r\\n&quot;;  char response_js[] = &quot;HTTP\/1.1 200 OK\\r\\n&quot; &quot;Content-Type: text\/js; charset=UTF-8\\r\\n\\r\\n&quot;;  char response_ttf[] = &quot;HTTP\/1.1 200 OK\\r\\n&quot; &quot;Content-Type: font\/ttf ; charset=UTF-8\\r\\n\\r\\n&quot;;  char response_text[] = &quot;HTTP\/1.1 200 OK\\r\\n&quot; &quot;Content-Type: text\/text; charset=UTF-8\\r\\n\\r\\n&quot;;  char response_403[] = &quot;HTTP\/1.1 200 OK\\r\\n&quot; &quot;Content-Type: text\/html; charset=UTF-8\\r\\n\\r\\n&quot; &quot;&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;head&gt;&lt;title&gt;403&lt;\/title&gt;&quot; &quot;&lt;style&gt;body { background-color: #312f2f }&quot; &quot;h1 { font-size:4cm; text-align: center; color: #666;}&lt;\/style&gt;&lt;\/head&gt;&quot; &quot;&lt;body&gt;&lt;h1&gt;403&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;\\r\\n&quot;;  char GUIDKey[] = &quot;258EAFA5-E914-47DA-95CA-C5AB0DC85B11&quot;; \/\/36  char response_ws[] = &quot;HTTP\/1.1 101 Switching Protocols\\r\\n&quot; &quot;Upgrade: websocket\\r\\n&quot; &quot;Connection: Upgrade\\r\\n&quot; &quot;Sec-WebSocket-Accept: &quot;; \/\/97  unsigned char charset[]={&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/&quot;};  \/\/ opcode - \u0442\u0438\u043f \u0444\u0440\u0435\u0439\u043c\u0430 #define WS_TEXT_FRAME 0x01 #define WS_PING_FRAME 0x09 #define WS_PONG_FRAME 0x0A #define WS_CLOSING_FRAME 0x08 #define BUFSIZE 1024 #define FILESTR 32 #define ALLARRAY 64  char patch_to_dir[ALLARRAY] = {0,}; char fpfile[ALLARRAY] = {0,}; char buffer[BUFSIZE] = {0,}; int client_fd; int count_warning_log =0; struct stat stat_buf; sem_t sem;   int base64_encode(unsigned char sha_key_in[], unsigned char base64_key_out[], int len)  {    int idx, idx2, blks, left_over;     blks = (len \/ 3) * 3;    for(idx=0, idx2=0; idx &lt; blks; idx += 3, idx2 += 4)      {       base64_key_out[idx2] = charset[sha_key_in[idx] &gt;&gt; 2];       base64_key_out[idx2+1] = charset[((sha_key_in[idx] & 0x03) &lt;&lt; 4) + (sha_key_in[idx+1] &gt;&gt; 4)];       base64_key_out[idx2+2] = charset[((sha_key_in[idx+1] & 0x0f) &lt;&lt; 2) + (sha_key_in[idx+2] &gt;&gt; 6)];       base64_key_out[idx2+3] = charset[sha_key_in[idx+2] & 0x3F];     }     left_over = len % 3;     if(left_over == 1)      {       base64_key_out[idx2] = charset[sha_key_in[idx] &gt;&gt; 2];       base64_key_out[idx2+1] = charset[(sha_key_in[idx] & 0x03) &lt;&lt; 4];       base64_key_out[idx2+2] = '=';       base64_key_out[idx2+3] = '=';       idx2 += 4;     }     else if(left_over == 2)      {       base64_key_out[idx2] = charset[sha_key_in[idx] &gt;&gt; 2];       base64_key_out[idx2+1] = charset[((sha_key_in[idx] & 0x03) &lt;&lt; 4) + (sha_key_in[idx+1] &gt;&gt; 4)];       base64_key_out[idx2+2] = charset[(sha_key_in[idx+1] & 0x0F) &lt;&lt; 2];       base64_key_out[idx2+3] = '=';       idx2 += 4;     }     base64_key_out[idx2] = '\\0';    return(idx2);  }   void error_log(char *my_error)   {     time_t t;    time(&t);    FILE *f;    f = fopen(&quot;\/var\/log\/ErrorWsstd.log&quot;, &quot;a&quot;);     if(f == NULL) printf(&quot;Error open \/var\/log\/ErrorWsstd.log.\\n&quot;);    fprintf(f, &quot;%s&quot;, ctime( &t));    fprintf(f, &quot;Error %s\\n\\n&quot;, my_error);    printf(&quot;Error %s Write to \/var\/log\/ErrorWsstd.log.\\n&quot;, my_error);    fclose(f);    exit(0);  }   void warning_access_log(char *war_ac)   {      count_warning_log++;    if(count_warning_log &gt; 100)      {        system(&quot;gzip -f \/var\/log\/Access_warning.log&quot;);        count_warning_log = 0;        time_t t;        time(&t);        FILE *f;        f = fopen(&quot;\/var\/log\/Access_warning.log&quot;, &quot;w&quot;);         fprintf(f, &quot;%s&quot;, ctime( &t));        fprintf(f, &quot;%s\\n\\n&quot;, war_ac);        printf(&quot;_______________________________________\\nWrite to \/var\/log\/Access_warning.log...\\n%s\\n&quot;, war_ac);        fclose(f);      }      else      {        time_t t;        time(&t);        FILE *f;        f = fopen(&quot;\/var\/log\/Access_warning.log&quot;, &quot;a&quot;);         fprintf(f, &quot;%s&quot;, ctime( &t));        fprintf(f, &quot;%s\\n\\n&quot;, war_ac);        printf(&quot;_______________________________________\\nWrite to \/var\/log\/Access_warning.log...\\n%s\\n&quot;, war_ac);        fclose(f);      }  }   void read_in_file(char *name_file)   {     off_t offset = 0;    memset(&stat_buf, 0, sizeof(stat_buf));        memset(fpfile, 0, ALLARRAY);    snprintf(fpfile, (int)strlen(patch_to_dir) + (int)strlen(name_file) + 1, &quot;%s%s&quot;, patch_to_dir, name_file);    int file = open(fpfile, O_RDONLY);     if(file &lt; 0)      {       if(close(client_fd) == -1) error_log(&quot;open file close client_fd.&quot;);       warning_access_log(&quot;Not File.&quot;);      }      else     {       if(fstat(file, &stat_buf) != 0) error_log(&quot;fstat.&quot;);       if(sendfile(client_fd, file, &offset, stat_buf.st_size) == -1) warning_access_log(&quot;sendfile.&quot;);        if(close(file) == -1) error_log(&quot;close file.&quot;);       if(close(client_fd) == -1) warning_access_log(&quot;in function read_in_file() - close client_fd.&quot;);       warning_access_log(buffer);       printf(&quot;Trans %s\\n\\n&quot;, name_file);     }  }   \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ ws_func \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ void * ws_func(void *client_arg)   {     int client_fd = * (int *) client_arg;    sem_post(&sem);    warning_access_log(&quot;START_WS&quot;);    printf(&quot;\\nClient ID - %d\\n&quot;, client_fd);    char inbuf[132] = {0,};    char reciv_r[48] = {0,};     while(1)     {       memset(inbuf, 0, 132);        int rec_b = read(client_fd, inbuf, 131); \/\/ \u043e\u0436\u0438\u0434\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0438 \u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u043f\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0443       memset(reciv_r, 0, 48);       snprintf(reciv_r, 39, &quot;%s%d%s%d\\n&quot;, &quot;Ws_func recive &quot;, rec_b, &quot; bytes from clien &quot;,  client_fd);       warning_access_log(reciv_r); \/\/ \u043f\u0438\u0448\u0435\u043c \u0441\u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0432 \u043b\u043e\u0433        if(rec_b == 0 || rec_b == -1) \/\/ \u0435\u0441\u043b\u0438 \u043a\u043b\u0438\u0435\u043d\u0442 \u043e\u0442\u0432\u0430\u043b\u0438\u043b\u0441\u044f \u0438\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u043d\u0435\u0445\u043e\u0440\u043e\u0448\u043e, \u0442\u043e\u0433\u0434\u0430...        {          memset(reciv_r, 0, 48);          snprintf(reciv_r, 47, &quot;%s%d%s%d\\n&quot;, &quot;Ws_func read return - &quot;, rec_b, &quot;, DIE clien - &quot;,  client_fd);          warning_access_log(reciv_r); \/\/ \u043f\u0438\u0448\u0435\u043c \u0441\u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0432 \u043b\u043e\u0433          if(close(client_fd) == -1) error_log(&quot;open file close client_fd ws 1.&quot;); \/\/ \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c          pthread_exit(NULL);        }         if(rec_b &gt; 0)  \/\/ \u0435\u0441\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438, \u0442\u043e ...                            {           char masking_key[4] = {0,}; \/\/ \u0441\u044e\u0434\u0430 \u043f\u043e\u043b\u043e\u0436\u0438\u043c \u043c\u0430\u0441\u043a\u0443          char opcode; \/\/ \u0441\u044e\u0434\u0430 \u0442\u0438\u043f \u0444\u0440\u0435\u0439\u043c\u0430          int payload_len; \/\/ \u0441\u044e\u0434\u0430 \u0434\u043b\u0438\u043d\u0443 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f (\u0442\u0435\u043b\u0430), \u0442\u043e \u0435\u0441\u0442\u044c \u0431\u0435\u0437 \u0441\u043b\u0443\u0436\u0435\u0431\u043d\u044b\u0445 \u0431\u0430\u0439\u0442\u043e\u0432            opcode = inbuf[0] & 0x0F;               printf(&quot;FIN: 0x%02x\\n&quot;, inbuf[0] & 0x01);             printf(&quot;RSV1: 0x%02x\\n&quot;, inbuf[0] & 0x02);             printf(&quot;RSV2: 0x%02x\\n&quot;, inbuf[0] & 0x03);             printf(&quot;RSV3: 0x%02x\\n&quot;, inbuf[0] & 0x04);             printf(&quot;Opcode: 0x%02x\\n&quot;, inbuf[0] & 0x0F);                                 payload_len = inbuf[1] & 0x7F;              printf(&quot;Maska: 0x%02x\\n&quot;, inbuf[1] & 0x80 ? 1:0);             printf(&quot;Payload_len: %d\\n&quot;, inbuf[1] & 0x7F);           masking_key[0] = inbuf[2];          masking_key[1] = inbuf[3];          masking_key[2] = inbuf[4];          masking_key[3] = inbuf[5];                          char payload[128] = {0,}; \/\/ \u0441\u044e\u0434\u0430 \u043f\u043e\u043b\u043e\u0436\u0438\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435           if(opcode == WS_CLOSING_FRAME) \/\/ \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d \u043a\u043e\u0434 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u044f \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f           {             memset(reciv_r, 0, 48);             snprintf(reciv_r, 47, &quot;%s%d\\n&quot;, &quot;Ws_func recive opcod - 0x08, DIE clien - &quot;,  client_fd);             warning_access_log(reciv_r); \/\/ \u043f\u0438\u0448\u0435\u043c \u0441\u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0432 \u043b\u043e\u0433             if(close(client_fd) == -1) error_log(&quot;open file close client_fd ws 2.&quot;); \/\/ \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c             pthread_exit(NULL); \/\/ \u0443\u0431\u0438\u0432\u0430\u0435\u043c \u043f\u043etok           }            if(opcode == WS_PONG_FRAME) \/\/ \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d PONG (\u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439)           {             int i = 6, pl = 0;             for(; pl &lt; payload_len; i++, pl++)              {                payload[pl] = inbuf[i]^masking_key[pl % 4];               }                                   printf(&quot;\\nRecive PONG and text \\&quot;%s\\&quot;\\n&quot;, payload);           }            if(opcode == WS_TEXT_FRAME) \/\/ \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d \u0442\u0435\u043a\u0441\u0442           {             int i = 6, pl = 0;             for(; pl &lt; payload_len; i++, pl++)              {                payload[pl] = inbuf[i]^masking_key[pl % 4];               }                                   printf(&quot;\\nReciv TEXT_FRAME from %d client, payload: %s\\n&quot;, client_fd, payload);               if(payload[0] == 'p' && payload[1] == 'i' && payload[2] == 'n' && payload[3] == 'g') \/\/ \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d \u0442\u0435\u043a\u0441\u0442 &quot;ping&quot;                {                printf(&quot;\\nPING client - %d\\n&quot;, client_fd);                  char ping[] = {0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f}; \/\/ Ping - \u043d\u0435 \u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439, \u0442\u0435\u043b\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u043b\u043e\u0432\u043e Hello, \u044d\u0442\u0430 \u0436\u0435 \u0441\u043b\u043e\u0432\u043e \u0432\u0435\u0440\u043d\u0451\u0442\u0441\u044f \u0441 \u041f\u043e\u043d\u0433\u043e\u043c                                if(send(client_fd, ping, 7, 0) == -1)                 {                   warning_access_log(&quot;Error PING.&quot;);                    if(close(client_fd) == -1) error_log(&quot;open file close client_fd ws 3.&quot;); \/\/ \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c                   pthread_exit(NULL);                  }               }              if(payload[0] == 'c' && payload[1] == 'l' && payload[2] == 'o' && payload[3] == 's' && payload[4] == 'e') \/\/ \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d \u0442\u0435\u043a\u0441\u0442 &quot;close&quot;              {                printf(&quot;\\nClose client - %d\\n&quot;, client_fd);                  char close_client[] = {0x88, 0};                             if(send(client_fd, close_client, 2, 0) == -1)                 {                   warning_access_log(&quot;Error CLOSE.&quot;);                    if(close(client_fd) == -1) error_log(&quot;open file close client_fd ws 4.&quot;); \/\/ \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c                   pthread_exit(NULL);                  }               }                if(payload[0] == 'h' && payload[1] == 'i') \/\/ \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d \u0442\u0435\u043a\u0441\u0442 &quot;hi&quot;              {                char messag[] = &quot;Hi client - &quot;;                int message_size = (int) strlen(messag);                char out_data[128] = {0,};                memcpy(out_data + 2, messag, message_size); \/\/ \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 &quot;out_data&quot; \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441\u043e \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0430\u0439\u0442\u0430 (\u043f\u0435\u0440\u0432\u044b\u0435 \u0434\u0432\u0430 \u0431\u0430\u0439\u0442\u0430 \u0434\u043b\u044f \u043e\u043f\u043a\u043e\u0434\u0430 \u0438 \u0434\u043b\u0438\u043d\u044b \u0442\u0435\u043b\u0430)                char nom_client[5] = {0,};                sprintf(nom_client, &quot;%d&quot;, client_fd); \/\/ \u043d\u043e\u043c\u0435\u0440 \u043a\u043b\u0438\u0435\u043d\u0442\u0430                int nom_client_size = (int) strlen(nom_client);                memcpy(out_data + 2 + message_size, nom_client, nom_client_size); \/\/ \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u043d\u043e\u043c\u0435\u0440 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 &quot;out_data&quot; \u0441\u043b\u0435\u0434\u043e\u043c \u0437\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435\u043c                 message_size += nom_client_size; \/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043e\u0431\u0449\u0443\u044e \u0434\u043b\u0438\u043d\u0443 \u0442\u0435\u043b\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f                 out_data[0] = 0x81;                out_data[1] = (char)message_size;                 printf(&quot;\\nSize out Msg: %d\\n&quot;, message_size);                 if(send(client_fd, out_data, message_size + 2, 0) == -1)                  {                   warning_access_log(&quot;Error Hi.&quot;);                    if(close(client_fd) == -1) error_log(&quot;open file close client_fd ws 5.&quot;); \/\/ \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c                    pthread_exit(NULL);                  }              }           }        }         }   }     int main(int argc, char *argv[])   {     if(argc != 3) error_log(&quot;not argumets.&quot;);         unsigned int PORTW = strtoul(argv[1], NULL, 0); \/\/ \u043f\u043e\u0440\u0442 \u0434\u043b\u044f web-\u0441\u0435\u0440\u0432\u0435\u0440\u0430 80   strncpy(patch_to_dir, argv[2], 63); \/\/ \u043f\u0443\u0442\u044c \u043a \u0444\u0430\u0439\u043b\u0443 index.html   warning_access_log(&quot;START&quot;);   pthread_t ws_thread;       \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/    WEB    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/   int one = 1;   struct sockaddr_in svr_addr, cli_addr;   socklen_t sin_len = sizeof(cli_addr);     int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);   if (sock &lt; 0) error_log(&quot;not socket.&quot;);     setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));     svr_addr.sin_family = AF_INET;   svr_addr.sin_addr.s_addr = INADDR_ANY;   svr_addr.sin_port = htons(PORTW);    if(bind(sock, (struct sockaddr *) &svr_addr, sizeof(svr_addr)) == -1)     {      close(sock);      error_log(&quot;bind.&quot;);    }     if(listen(sock, 5) == -1)     {      close(sock);      error_log(&quot;listen.&quot;);    }     signal(SIGPIPE, SIG_IGN);    char str_from_buf[FILESTR] = {0,};   char result_file[FILESTR] = {0,};           for(;;)     {     client_fd = accept(sock, (struct sockaddr *) &cli_addr, &sin_len);       if(client_fd == -1) continue;      memset(buffer, 0, BUFSIZE);     memset(str_from_buf, 0, FILESTR);     memset(result_file, 0, FILESTR);     char *p = NULL;      if(read(client_fd, buffer, BUFSIZE - 1) == -1) warning_access_log(&quot;Error in main - read_client_fd.&quot;);      int i = 0;     for(; i &lt; FILESTR; i++)      {        str_from_buf[i] = buffer[i];        if(str_from_buf[i] == '\\n') break;        if(i &gt; 31)         {           str_from_buf[i] = '\\0';           break;           }      }       if((strstr(str_from_buf, &quot;GET \/ &quot;)) != NULL)       {        if(send(client_fd, response, (int)strlen(response), MSG_NOSIGNAL) == -1) warning_access_log(&quot;send response.&quot;);        read_in_file(&quot;index.html&quot;);      }       \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ WS \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/     else if((strstr(str_from_buf, &quot;GET \/ws &quot;)) != NULL)       {        warning_access_log(buffer);         if((p = strstr(buffer, &quot;Sec-WebSocket-Key:&quot;)) != NULL)         {                                                             char resultstr[64] = {0,};           int i = 0, it = 0;           for(i = 19; it &lt; 24; i++, it++)            {              resultstr[it] = p[i];            }            strcat(resultstr, GUIDKey);            \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/sha1\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/           unsigned char temp[SHA_DIGEST_LENGTH] = {0,};           char buf[SHA_DIGEST_LENGTH*2] = {0,};            SHA1((unsigned char *)resultstr, strlen(resultstr), temp);            for(i=0; i &lt; SHA_DIGEST_LENGTH; i++)             {              sprintf((char*)&(buf[i*2]), &quot;%02x&quot;, temp[i]);            }            \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/Base64\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/            unsigned char key_out[64] = {0,};           base64_encode(temp, key_out, sizeof(temp));             sem_init(&sem, 0, 0);           char resp[131] = {0,};           snprintf(resp, 130, &quot;%s%s%s&quot;, response_ws, key_out, &quot;\\r\\n\\r\\n&quot;);           if(send(client_fd, resp, sizeof(char) * strlen(resp), MSG_NOSIGNAL) == -1) warning_access_log(&quot;send response_ws.&quot;);                \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ START WS \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/           if(pthread_create(&ws_thread, NULL, &ws_func, &client_fd) != 0) error_log(&quot;creating WS.&quot;);           pthread_detach(ws_thread);           sem_wait(&sem);         }      }       else if((p = strstr(str_from_buf, &quot;.png&quot;)) != NULL)       {        int index = p - str_from_buf;        int i = 0;        int otbor = 0;        for(; i &lt; index + 3; i++)         {           result_file[i] = str_from_buf[i];                if(result_file[i] == '\/')             {              otbor = i;            }         }         memset(result_file, 0, FILESTR);        strncpy(result_file, str_from_buf + otbor - 3, index -1); \/\/  otbor + 1        if(send(client_fd, response_img, (int)strlen(response_img), MSG_NOSIGNAL) == -1) warning_access_log(&quot;Error send response_img.&quot;);        read_in_file(result_file);      }      else if((p = strstr(str_from_buf, &quot;.css&quot;)) != NULL)       {        int index = p - str_from_buf;        int i = 0;        int otbor = 0;        for(; i &lt; index + 3; i++)         {           result_file[i] = str_from_buf[i];                if(result_file[i] == '\/')             {              otbor = i;            }         }         memset(result_file, 0, FILESTR);        strncpy(result_file, str_from_buf + otbor + 1, index -1);        if(send(client_fd, response_css, (int)strlen(response_css), MSG_NOSIGNAL) == -1) warning_access_log(&quot;Error send response_css.&quot;);        read_in_file(result_file);      }       else if((strstr(str_from_buf, &quot;jquery.js&quot;)) != NULL)       {        if(send(client_fd, response_js, (int)strlen(response_js), MSG_NOSIGNAL) == -1) warning_access_log(&quot;Error send response_js.&quot;);        read_in_file(&quot;jquery.js&quot;);      }      else if((strstr(str_from_buf, &quot;favicon.ico&quot;)) != NULL)       {        if(send(client_fd, response_xicon, (int)strlen(response_xicon), MSG_NOSIGNAL) == -1) warning_access_log(&quot;Error send favicon.ico.&quot;);        read_in_file(&quot;favicon.ico&quot;);      }      else       {        if(send(client_fd, response_403, sizeof(response_403), MSG_NOSIGNAL) == -1) warning_access_log(&quot;Error send response_403.&quot;);        if(close(client_fd) == -1) warning_access_log(&quot;Error close client_fd 403.&quot;);        warning_access_log(buffer);      }     }  } \/\/END main  \/\/ gcc -Wall -Wextra -Werror websocket.c -o websocket -pthread -lcrypto \/\/ sudo .\/websocket 80 \/home\/dima\/c-websocket\/ <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0418 <a href=\"http:\/\/istarik.ru\/file\/websocket\">\u0431\u0438\u043d\u0430\u0440\u043d\u0438\u043a<\/a> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/009\/46f\/fa1\/00946ffa1e4249f6a2e782585eac1d64.png\"\/>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0441 \u0434\u0432\u0443\u043c\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u2014 \u043f\u043e\u0440\u0442 (80) \u0438 \u043f\u0443\u0442\u044c \u043a \u043f\u0430\u043f\u043a\u0435 \u0441 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043e\u0439 \u0438 \u0444\u0430\u0439\u043b\u043e\u043c index.html (\u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u0441\u0442\u0430\u0442\u044c\u0438).  <\/p>\n<pre><code class=\"cpp\">sudo chmod +x .\/websocket sudo .\/websocket 80 \/home\/dima\/c-websocket\/ <\/code><\/pre>\n<p>  <i>\u0416\u0435\u043b\u0430\u044e\u0449\u0438\u0435 \u043c\u043e\u0433\u0443\u0442 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0442\u044c \u043f\u0440\u043e\u0433\u0443 \u0434\u043b\u044f \u0440\u043e\u0443\u0442\u0435\u0440\u0430 \u0438\u043b\u0438 \u0435\u0449\u0451 \u0447\u0435\u0433\u043e-\u043d\u0438\u0431\u0443\u0434\u044c.<\/i><\/p>\n<p>  \u041f.\u0421. \u041a\u043e\u0434 \u043d\u0435 \u00ab\u043f\u0440\u0438\u0447\u0451\u0441\u0430\u043d\u00bb, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043c\u043d\u043e\u0433\u043e \u043d\u0435\u043d\u0443\u0436\u043d\u043e\u0439 \u043e\u0442\u043b\u0430\u0434\u043e\u0447\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0438 \u0432\u0441\u044f\u043a\u0438\u0445 \u043b\u043e\u0433\u043e\u0432, \u0442\u0430\u043a \u0447\u0442\u043e \u043d\u0435 \u043e\u0431\u0435\u0441\u0441\u0443\u0434\u044c\u0442\u0435.<br \/> \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:\/\/geektimes.ru\/post\/283298\/\"> https:\/\/geektimes.ru\/post\/283298\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/8ca\/a13\/15a\/8caa1315a2f64ff9a3b37f80c9365a94.png\"\/><\/p>\n<p>  \u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u0442\u0435\u043c, \u043a\u043e\u043c\u0443 \u0437\u0430\u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 WebSocket-\u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430 \u0421\u0418.<\/p>\n<p>  <i>\u041d\u0435 \u0437\u043d\u0430\u043b \u043a\u0430\u043a\u0443\u044e \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c \u041a\u0414\u041f\u0412&#8230;<\/i>  <\/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-281967","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/281967","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=281967"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/281967\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=281967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=281967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=281967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}