jawainc Posted August 2, 2008 Share Posted August 2, 2008 Hi i'm new to php. having some problems here is what i'm doing this is the server file index.php <?php error_reporting(E_ALL); /* Allow the script to hang around waiting for connections. */ set_time_limit(0); /* Turn on implicit output flushing so we see what we're getting * as it comes in. */ ob_implicit_flush(); $address = '127.0.0.1'; $port = 80; echo $address; if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; } if (socket_bind($sock, $address) === false) { echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; } if (socket_listen($sock, 5) === false) { echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; } do { if (($msgsock = socket_accept($sock)) === false) { echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; break; } /* Send instructions. */ $msg = "\nWelcome to the PHP Test Server. \n" . "To quit, type 'quit'. To shut down the server type 'shutdown'.\n"; socket_write($msgsock, $msg, strlen($msg)); do { if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) { echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n"; break 2; } if (!$buf = trim($buf)) { continue; } if ($buf == 'quit') { break; } if ($buf == 'shutdown') { socket_close($msgsock); break 2; } $talkback = "PHP: You said '$buf'.\n"; socket_write($msgsock, $talkback, strlen($talkback)); echo "$buf\n"; } while (true); socket_close($msgsock); } while (true); echo $address; echo $port; socket_close($sock); ?> and the client side file clinet.php <?php error_reporting(E_ALL); echo "<h2>TCP/IP Connection</h2>\n"; // Get the port for the WWW service. $service_port = getservbyname('http', 'tcp'); //echo $service_port; // Get the IP address for the target host. $address = gethostbyname('localhost'); // Create a TCP/IP socket. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false) { echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; } else { echo "socket created <br>OK<br />"; } 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 "socket connect<br>OK<br>"; } $in = " GET / HTTP:/1.1\r\n"; $in .= "Host: localhost/index.php\r\n"; $in .= "Connection: Close\r\n\r\n"; $out = ''; echo '****'.$in.'***<br>'; echo "Sending HTTP HEAD request...<br>"; socket_write($socket, $in, strlen($in)); echo "OK<br>"; echo "Reading response:\n\n<br>"; while ($out = socket_read($socket, 2048)) { echo '************<br>'.$out.'<br>*************<br>'; } echo "Closing socket..."; socket_close($socket); echo "OK.\n\n"; ?> when i run the file i got following error TCP/IP Connection socket created OK Attempting to connect to '127.0.0.1' on port '80'...socket connect OK **** GET / HTTP:/1.1 Host: localhost/index.php Connection: Close *** Sending HTTP HEAD request... OK Reading response: ************ HTTP/1.1 400 Bad Request Date: Sat, 02 Aug 2008 08:17:05 GMT Server: Apache/2.2.4 (Win32) DAV/2 mod_ssl/2.2.4 OpenSSL/0.9.8e mod_autoindex_color PHP/5.2.3 Content-Length: 374 Connection: close Content-Type: text/html; charset=iso-8859-1 Bad Request Your browser sent a request that this server could not understand. Apache/2.2.4 (Win32) DAV/2 mod_ssl/2.2.4 OpenSSL/0.9.8e mod_autoindex_color PHP/5.2.3 Server at localhost/index.php Port 80 ************* Closing socket...OK. any idea how to do it thanks. Link to comment https://forums.phpfreaks.com/topic/117805-need-help-in-socket-programming/ Share on other sites More sharing options...
448191 Posted August 2, 2008 Share Posted August 2, 2008 1) Your client script is connecting to Apache instead of your server script. Use a different pot or stop the Apache service. 2) Your HTTP HEAD is malformed. Try losing the colon; "GET / HTTP/1.1\r\n". Link to comment https://forums.phpfreaks.com/topic/117805-need-help-in-socket-programming/#findComment-605984 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.