Jump to content

Recommended Posts

Posted

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


Posted

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:

<?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:

<?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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...