View Single Post
(#6 (permalink))
Old
grace1004 grace1004 is offline
Senior Member
grace1004 is on a distinguished road
 
Posts: 762
Join Date: Dec 2010
Default 05-10-2012, 06:18 PM

PHP Code:
<?php
error_reporting
(E_ALL);

echo 
"<h2>TCP/IP Connection</h2>\n";

/* Get the port for the WWW service. */
$service_port getservbyname('www''tcp');

/* Get the IP address for the target host. */
$address gethostbyname('mydomain.com');

/* Create a TCP/IP socket. */
$socket socket_create(AF_INETSOCK_STREAMSOL_TCP);
if (
$socket === false) {
    echo 
"socket_create() failed: reason: " socket_strerror(socket_last_error()) . "\n";
} else {
    echo 
"OK.\n";
}

echo 
"Attempting to connect to '$address' on port '$service_port'...";
$result socket_connect($socket$address$service_port);
if (
$result === false) {
    echo 
"socket_connect() failed.\nReason: ($result) " socket_strerror(socket_last_error($socket)) . "\n";
} else {
    echo 
"OK.\n";
}

$in "HEAD / HTTP/1.1\r\n";
$in .= "Host: mydomain.com\r\n";
$in .= "Connection: Close\r\n\r\n";
$out '';

echo 
"Sending HTTP HEAD request...";
socket_write($socket$instrlen($in));
echo 
"OK.\n";

echo 
"Reading response:\n\n";
while (
$out socket_read($socket2048)) {
    echo 
$out;
}

echo 
"Closing socket...";
socket_close($socket);
echo 
"OK.\n\n";
?>
After running example #2 code (http://php.net/manual/en/sockets.examples.php), I got the following response:

Quote:
TCP/IP Connection
OK. Attempting to connect to '31.170.160.169' on port '80'...OK. Sending HTTP HEAD request...OK.
Reading response: HTTP/1.1 200 OK Date: Thu, 10 May 2012 17:56:23 GMT Server: Apache X-Powered-By:
PHP/5.2.17 Connection: close Content-Type: text/html Closing socket...OK.
Isn't this what you want?

To test example #1 code, you need to have your own server for executing php script on shell mode.

Last edited by grace1004; 05-10-2012 at 06:24 PM.
Reply With Quote