Help - Search - Members - Calendar
Full Version: PHP ServerSocket...HowTO?
MSFN Forums > Coding, Scripting and Servers > Web Development (HTML, Java, PHP, ASP, XML, etc.)

   
Google Internet Forums Unattended CD/DVD Guide
YoussefGamil
hi,
I have searched the web for PHP ServerSocket..
but, all I get is how PHP create the ServerSocket!!!

How to create PHP ServerSocket which can read and write? (by exmaples PLZ).
Thnx
egrath
Hi,

i've never used Socket's in a PHP Script before, but a quick look into the PHP Manual gave me a plenty amount of information about this.

Just look in the PHP Manual (downloadable from PHP.net in CHM Format, if you don't have it already) and look for "Socket Functions"

Short excerpt:

Socket creation:
CODE
<?php
$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)<br />\n";
} else {
while ($conn = stream_socket_accept($socket)) {
  fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
  fclose($conn);
}
fclose($socket);
}
?>


How to write to a socket and retrieve data:
CODE
<?php
$fp = stream_socket_client("tcp://www.example.com:80", $errno, $errstr, 30);
if (!$fp) {
  echo "$errstr ($errno)<br />\n";
} else {
  fwrite($fp, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
  while (!feof($fp)) {
      echo fgets($fp, 1024);
  }
  fclose($fp);
}
?>


Hope that helps,

Egon
YoussefGamil
Thanks after all..
but, It doesn't work!!!!
Google Internet Forums Unattended CD/DVD Guide
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.