0

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. 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. 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

####### 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:

$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);

while (!feof($fp)) {
    echo fgets($fp, 8192);
}

What happens is, as soon as I do my fputs, I get a response header from the server that 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

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.

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.

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.

4
  • Are you sure the connection is still active? even with "Connection: keep-alive"? Commented Mar 5, 2010 at 5:37
  • As far as I can tell. I don't know when the connection would die since it hangs on the "socket_read". Commented Mar 5, 2010 at 7:06
  • If your description is accurate then the remote system is NOT implementing HTTP - which is stateless. Commented Mar 5, 2010 at 14:22
  • "symcbean", this is where I feel like it's a PHP thing. Because if I open Wireshark, and watch the packets across the network, I can see where my packet goes out, and the remote system replies to my computer. So it handle the request correctly. My script just doesn't read the response back. Again, if I use Connection: close, it will read the response back, but closes the connection. Commented Mar 5, 2010 at 16:38

1 Answer 1

3

Did you try setting the socket to nonblocking mode?

socket_set_nonblock($socket);

EDIT1: Ok. 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;
Sign up to request clarification or add additional context in comments.

4 Comments

I did try, but that doesn't really seem to be the problem, as I don't particularly need to do anything until I receive the initial response. There has been s little more development :) Still stuck on this though. I don't know how to post code in a comment as it always seems to bunch together, so I'm going to edit my question.
@Senica: I was just guessing with nonblock... But I do know that posting code in the question is the proper way to go there.
Brilliant! Had to modify it just a tad. But you were right, fgets was not reading the content.
I'm glad that worked for you. If you try to read more content than is available PHP will block waiting for more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.