• <i id='ZzKsw'><tr id='ZzKsw'><dt id='ZzKsw'><q id='ZzKsw'><span id='ZzKsw'><b id='ZzKsw'><form id='ZzKsw'><ins id='ZzKsw'></ins><ul id='ZzKsw'></ul><sub id='ZzKsw'></sub></form><legend id='ZzKsw'></legend><bdo id='ZzKsw'><pre id='ZzKsw'><center id='ZzKsw'></center></pre></bdo></b><th id='ZzKsw'></th></span></q></dt></tr></i><div id='ZzKsw'><tfoot id='ZzKsw'></tfoot><dl id='ZzKsw'><fieldset id='ZzKsw'></fieldset></dl></div>
      <bdo id='ZzKsw'></bdo><ul id='ZzKsw'></ul>

    <small id='ZzKsw'></small><noframes id='ZzKsw'>

    <tfoot id='ZzKsw'></tfoot>

    1. <legend id='ZzKsw'><style id='ZzKsw'><dir id='ZzKsw'><q id='ZzKsw'></q></dir></style></legend>

      1. 超简单的 HTTP 套接字服务器,用 PHP 编写,行为异常

        时间:2024-08-22
            <tbody id='4KZKn'></tbody>

          • <i id='4KZKn'><tr id='4KZKn'><dt id='4KZKn'><q id='4KZKn'><span id='4KZKn'><b id='4KZKn'><form id='4KZKn'><ins id='4KZKn'></ins><ul id='4KZKn'></ul><sub id='4KZKn'></sub></form><legend id='4KZKn'></legend><bdo id='4KZKn'><pre id='4KZKn'><center id='4KZKn'></center></pre></bdo></b><th id='4KZKn'></th></span></q></dt></tr></i><div id='4KZKn'><tfoot id='4KZKn'></tfoot><dl id='4KZKn'><fieldset id='4KZKn'></fieldset></dl></div>
            <legend id='4KZKn'><style id='4KZKn'><dir id='4KZKn'><q id='4KZKn'></q></dir></style></legend>
            1. <tfoot id='4KZKn'></tfoot>

                <small id='4KZKn'></small><noframes id='4KZKn'>

                  <bdo id='4KZKn'></bdo><ul id='4KZKn'></ul>
                • 本文介绍了超简单的 HTTP 套接字服务器,用 PHP 编写,行为异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  tldr;

                  1. PHP
                  2. 中非常小的流套接字服务器
                  3. 表现得很奇怪,因为它有时会成功处理 HTTP 请求,而有时会在同一个进程中失败
                  4. 在不同的浏览器中表现得很奇怪 - 几乎每次都在 Chrome 中失败,而在 IE11
                  1. very minimal stream socket server in PHP
                  2. acts strange since sometimes it successfully serves HTTP request and sometimes fails within the very same process
                  3. acts strange across different browsers - almost every time fails in Chrome and never in IE11

                  代码:

                  $server = stream_socket_server("tcp://0.0.0.0:4444", $errno, $errorMessage);
                  
                  if ($server === false) 
                      throw new UnexpectedValueException("Could not bind to socket: $errorMessage");
                  
                  $e = "
                  ";
                  $headers = array(
                      "HTTP/1.1 200 OK",
                      "Date: " . date('D') . ', ' . date('m') . ' '  . date('M') . ' ' . date('Y') . ' ' . date('H:i:s') . ' GMT' ,
                      'Server: MySpeedy',
                      'Connection: close',
                      'Content-Type: text/plain',
                      'Content-Length: 2'
                  );
                  
                  $headers = implode($e, $headers) . $e .  $e .'ok';
                  
                  for (;;) 
                  {
                      $client = stream_socket_accept($server);
                  
                      if ($client) 
                      {
                          echo 'Connection accepted from '.stream_socket_get_name($client, false) . $e;
                  
                          fwrite($client, $headers);
                          fclose($client);
                      }
                  }
                  

                  给我这个 http 响应(telnet 结果):

                  gives me this http response (telnet results):

                  HTTP/1.1 200 OK
                  Date: Fri, 11 Nov 2015 20:09:02 GMT
                  Server: MySpeedy
                  Connection: close
                  Content-Type: text/plain
                  Content-Length: 2
                  
                  ok
                  

                  这导致我得到这些结果:

                  And that leads me to these results:

                  • ERR_CONNECTION_RESET 在 Chrome 中,几乎每次(可能是 20-30 中的 1请求得到预期的响应)
                  • 连接被重置在 Firefox 中,大约 1 in 2-3请求
                  • 每次在 Internet Explorer 11 中都能得到正确的预期响应(是的,IE 是最好的).
                  • ERR_CONNECTION_RESET in Chrome, almost every time (maybe 1 in 20-30 requests get expected response)
                  • The connection was reset in Firefox, approximately 1 in 2-3 requests
                  • Correct, expected response in Internet Explorer 11 every time (yay, IE is the best in something).

                  我做错了什么?是 http 标头(我不能说我的格式是否错误)还是 socket loop 或..?

                  What am I doing wrong? Is it up to http headers (I couldn't say if I've formatted them incorrectly) or socket loop or..?

                  推荐答案

                  您不会从客户端读取 HTTP 请求,而是简单地发送响应并关闭连接.但是在仍有数据要读取时关闭套接字会导致连接重置发送回客户端,这就是您将在 Chrome 中看到的带有 ERR_CONNECTION_RESET 的内容.其他浏览器的行为可能会有所不同,如果浏览器可以在处理重置之前显示响应,这也是一个时间问题.

                  You don't read the HTTP request from the client but instead simply send your response and close the connection. But closing the socket while there are still data to read will cause a connection reset send back to the client and that's what you will see in Chrome with ERR_CONNECTION_RESET. Other browsers might behave differently and it is also a timing issue if the browser can display the response before handling the reset.

                  要修复它,请先从客户端读取完整的请求,然后再关闭套接字.

                  To fix it first read the full request from the client before you close the socket.

                  这篇关于超简单的 HTTP 套接字服务器,用 PHP 编写,行为异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:通过 curl 发送 xml 和 headers 下一篇:PNG图像输出的标题以确保它在浏览器中被缓存?

                  相关文章

                • <legend id='uHoPd'><style id='uHoPd'><dir id='uHoPd'><q id='uHoPd'></q></dir></style></legend>

                  <small id='uHoPd'></small><noframes id='uHoPd'>

                  <i id='uHoPd'><tr id='uHoPd'><dt id='uHoPd'><q id='uHoPd'><span id='uHoPd'><b id='uHoPd'><form id='uHoPd'><ins id='uHoPd'></ins><ul id='uHoPd'></ul><sub id='uHoPd'></sub></form><legend id='uHoPd'></legend><bdo id='uHoPd'><pre id='uHoPd'><center id='uHoPd'></center></pre></bdo></b><th id='uHoPd'></th></span></q></dt></tr></i><div id='uHoPd'><tfoot id='uHoPd'></tfoot><dl id='uHoPd'><fieldset id='uHoPd'></fieldset></dl></div>
                  <tfoot id='uHoPd'></tfoot>

                      <bdo id='uHoPd'></bdo><ul id='uHoPd'></ul>