<?php

function http_open($hostip, $getpath, $myhost){
require("./total.inc");

  error_reporting(E_ALL);

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

  //  $hostip = "192.168.0.2";
  //  $getpath = "/kanshi.php";
  //  $myhost = "www.example.com";

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

  /* Get the IP address for the target host. */
  $address = gethostbyname($hostip);

  /* Create a TCP/IP socket. */
  $socket = socket_create(AF_INET, SOCK_STREAM, 0);
  if ($socket < 0) {
      echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
  } else {
//      echo "OK.\n";
  }

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

  $in = "GET " . $getpath . " HTTP/1.0\r\n";
  $in .= "Host: " . $hostip . "\r\n";
  $in .= "Connection: Close\r\n\r\n";
  $out = '';

print("$hostip -->  $in\n");

  echo "Sending HTTP GET request...";
  socket_write($socket, $in, strlen ($in));
//  echo "OK.\n";

  echo "Reading response:\n\n";
  while ($out = socket_read($socket, 2048)) {
// echo $out;
  }

  echo "Closing socket...";
  socket_close($socket);
//  echo "OK.\n\n";

  echo "</pre>\n";
  }

?> 


