Re: Sockets

From: Date: Mon, 04 Feb 2002 07:32:19 +0000
Subject: Re: Sockets
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-83364@lists.php.net to get a copy of this message
A quick note... If you are not aware of cURL (curl.haxx.se), then you may want to look into it. If you are, then please disregard this post. -Jason Garber At 11:06 PM 2/3/2002 -0800, Evan Nemerson wrote:
Anyone know if there is a way yet to see if a socket is still connected to a host? I want to use a socket to send "GET / HTTP/1.0\r\n\r\n" over a socket, and retrieve everything the server sends. That part works great, but I can't figure out when the remote host disconnects. I have the CVS version of php. Here is the function so far. The problem is at the end. function getdata ($host, $port, $data) {
        /* well, the below comment would be true if i could get it working! */
        /* This function sends $data to $host:$port, then returns the response
        * until connection is severed. Great for HTTP, but won't usually work
        * too well in protocols where data needs to be analyzed, and replied
        * to appropriatly, such as POP v3 */
        // Create a socket
        $so = socket_create (AF_INET, SOCK_STREAM, getprotobyname("TCP"));
        if ( !$so )
        {
                exit("Could not create socket.\n");
        }
        // Connect...
        $ec = socket_connect ($so, $host, $port);
        if ( $ec < 0 )
        {
                exit ("ERROR $ec: ".socket_strerror($ec));
        }
        /* Write $data to socket. The manual doesn't say what it returns, but I'll
        * assume (even though it makes an ass out of you and me) that it is the same
        * as socket_connect() because it wouldn't be logical to return a
descriptor. */
        $ec = socket_write ( $so, $data, ( strlen($data) ));
        if ( $ec < 0 )
        {
                exit ("ERROR $ec: ".socket_strerror($ec));
        }
        else
        {
                /* PROBLEM IS HERE- what do I put instead of while ( $x == 0 )??? */
                $x = 0;
                while ( $x == 0 )
                {
                        $buffer = socket_read ( $so, 1, PHP_BINARY_READ);
                        $string .= $buffer;
                }
        }
        // And (hopefully) return $string, for your viewing pleasure.
        return $string;
} -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#83364) next »