On the
last page, I displayed the PHP script for the
sockets. Here I will briefly go over what you see. As I
said earlier, all the comments needed to understand it
are located in the zip file I have provided for
download. So lets check this stuff out a bit!
- #!/usr/bin/php
-q
This line states that this script is a PHP shell
script and not a PHP web script. A -q denotes that all
HTTP headers will be suppressed.
- error_reporting(E_ALL);
-
- set_time_limit(0);
-
- ob_implicit_flush();
-
- $address
=
'192.168.0.16';
- $port
=
9999;
-
- function
send_Message($allclient,
$socket,
$buf)
{
- foreach($allclient
as
$client)
{
- socket_write($client,
"$socket wrote: $buf");
- }
- }
-
-
-
- //---- Start Socket
creation for PHP 5 Socket Server
-------------------------------------
-
- if
(($master
=
socket_create(AF_INET,
SOCK_STREAM,
SOL_TCP))
<
0)
{
- echo
"socket_create() failed,
reason: " .
socket_strerror($master)
. "\n";
- }
-
- socket_set_option($master,
SOL_SOCKET,SO_REUSEADDR,
1);
-
- if
(($ret
=
socket_bind($master,
$address,
$port))
<
0)
{
- echo
"socket_bind() failed,
reason: " .
socket_strerror($ret)
. "\n";
- }
-
- if
(($ret
=
socket_listen($master,
5))
<
0)
{
- echo
"socket_listen() failed,
reason: " .
socket_strerror($ret)
. "\n";
- }
-
- $read_sockets
=
array($master);
Just setting up and declaring any variables that we
will use throughout the script. A master socket has been
created so that others can attempt to connect. As you
can see, the socket is bound to your IP and desired
Port. In the last step, a listen method is applied to the socket so
that anything attempting to connect is heard and then
dealt with.
Anytime you need to have Flash
connect to any port, you must have the port equal to or
higher than 1024. This is a default precaution Flash
takes to ensure connections are not crossing with other
ports such as 21, 24, or 80.
- while
(true)
{
- $changed_sockets
=
$read_sockets;
-
- $num_changed_sockets
=
socket_select($changed_sockets,
$write
=
NULL,
$except
=
NULL,
NULL);
-
- foreach($changed_sockets
as
$socket)
{
-
- if
($socket
==
$master)
{
-
- if
(($client
=
socket_accept($master))
<
0)
{
- echo
"socket_accept()
failed: reason: " .
socket_strerror($msgsock)
. "\n";
- continue;
- }
else
{
- array_push($read_sockets,
$client);
- }
- }
else
{
-
- $bytes
=
socket_recv($socket,
$buffer,
2048,
0);
-
- if
($bytes
==
0)
{
- $index
=
array_search($socket,
$read_sockets);
- unset($read_sockets[$index]);
-
socket_close($socket);
- }else{
- $allclients
=
$read_sockets;
-
array_shift($allclients);
-
-
send_Message($allclients,
$socket,
$buffer);
- }
- }
-
- }
- }
This piece makes a continuous loop to obtain any
connections made to the master socket and process them
accordingly. If a socket sends data to the server it
will send that data package out to everyone else
connected. If a socket sends a disconnect signal, the
socket is then closed out completely and removed from
the master array of sockets.
Just remember, all the comments are included with
this PHP file in the ZIP at the end of this tutorial!!
Now it's time to write a Batch file to run our PHP
Shell script to make our chat room work. The
next
section will show you the Batch file we will write
and show you what is happening.