简体   繁体   English

PHP:连接:保持活动状态读取套接字数据

[英]PHP: Connection: keep-alive problem reading socket data

Trying to write data to a socket and read the response. 尝试将数据写入套接字并读取响应。

$packet = "GET /get-database HTTP/1.1\r\n";
$packet .= "Host: 192.168.3.136:3689\r\n";
//$packet .= "Accept-Encoding: gzip\r\n";
$packet .= "Viewer-Only-Client: 1\r\n";
$packet .= "Connection: keep-alive\r\n\r\n";

socket_write($socket, $packet, strlen($packet));

do{
    $buf = "";
    $buf = socket_read($socket, 4096);
    $data .= $buf;
}while($buf != "");

echo "$data\r\n\r\n";

If I set the Connection to close then it works and I'm able to read the response. 如果我将“连接”设置为“ close则它可以正常工作,并且能够读取响应。 The problem with that is, that after I read the data, I need to write back to the socket. 这样做的问题是,在读取数据后,我需要写回套接字。 The response contains an id that I need to send back for verification. 响应包含一个ID,我需要将其发送回以进行验证。 If I write to the server on two separate sockets, it rejects the verification post back. 如果我在两个单独的套接字上写服务器,它将拒绝验证回发。 So I can only assume, that I need to post on same "open connection" or "session". 因此,我只能假设,我需要在相同的“开放连接”或“会话”上发布信息。

Any thoughts? 有什么想法吗?

I would like to figure out why I can't read from the socket with Connection: keep-alive 我想弄清楚为什么我无法使用Connection: keep-alive从套接字读取内容Connection: keep-alive

####### EDIT #######编辑

There has been a little development on this. 在此方面已有一些进展。 I'm trying to make this very simple so I can pinpoint the problem: 我正在尝试使其变得非常简单,以便我可以查明问题所在:

Right now my code looks like this: 现在,我的代码如下所示:

HTTP/1.1 200 OK
Date: Fri, 05 Mar 2010 22:05:47 GMT
RIPT-Server: iTunesLib/3.0.2 (Mac OS X)
Content-Type: application/x-dmap-tagged
Content-Length: 32

What happens is, as soon as I do my fputs, I get a response header from the server that looks like this: 发生的事情是,一旦执行了fput,我就会从服务器获得一个响应标头,如下所示:

 HTTP/1.1 200 OK Date: Fri, 05 Mar 2010 22:05:47 GMT RIPT-Server: iTunesLib/3.0.2 (Mac OS X) Content-Type: application/x-dmap-tagged Content-Length: 32 

And then my cursor just sits there. 然后我的光标就坐在那儿。 After anywhere from 15 seconds to a minute, I sometimes get the content, but I am still stuck in the while loop. 从15秒到一分钟的任何时间之后, 有时我都可以获得内容,但是我仍然陷入while循环中。

Does anyone know, if after the server has sent the response header, if I should be sending something back to it to let it know that I am ready for the content? 有谁知道,如果在服务器发送响应头之后,我是否应该向它发送回一些东西,以使其知道我已经准备好接收内容了?

Again, I don't think this is the case, since when I look in the packets on the network, I can see the entire response. 再一次,我认为情况并非如此,因为当我查看网络上的数据包时,可以看到整个响应。 That and the fact that I do sometimes get the content of the response. 我有时确实会得到答复的内容。 It's really like PHP can't handle this or I am just way off base. 真的就像PHP无法处理这个问题,或者我离基础很远。

Still need help..... :( 仍然需要帮助..... :(

Working Code 工作守则
 $fp = pfsockopen("192.168.3.136", "3689"); $header = "GET /login?id=5648349 HTTP/1.1\\r\\n"; $header .= "Connection: keep-alive\\r\\n\\r\\n"; fputs($fp, $header); $headers = array(); while(true){ $line = fgets($fp, 8192); if($line == "\\r\\n"){ break; } $line_parts = explode(': ',$line); echo $line_parts[1]."\\r\\n"; $headers[$line_parts[0]] = $line_parts[1]; } $content = fread($fp,intval($headers['Content-Length'])); echo $content; 

Now, I'll have to be wary of the "\\r\\n" test as I'm sure it's possible that some responses might only send a "\\n" after the header. 现在,我必须对“ \\ r \\ n”测试保持警惕,因为我确信某些响应可能仅在标头之后发送“ \\ n”。

Did you try setting the socket to nonblocking mode? 您是否尝试将套接字设置为非阻止模式?

socket_set_nonblock($socket);

EDIT1: Ok. EDIT1:好的。 Just a hunch... try this... 只是预感...尝试一下...

$fp = pfsockopen("192.168.3.136", "3689");
$content = "GET /login?id=a90347 HTTP/1.1\r\n";
$content .= "Connection: keep-alive\r\n\r\n";

fputs($fp, $content);

$headers = array();

do
{
  $line = fgets($fp, 8192);
  $line_parts = ecplode(': ',$line);
  $headers[$line_parts[0]] = $line_parts[1];
} while($line != '');

$content = fread($fp,intval($headers['Content-Length']));

echo $content;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM