
November 29th, 2005, 04:51 PM
|
 |
Moderator
|
|
Join Date: Apr 2007
Location: San Diego, CA
Posts: 1,548
Time spent in forums: 2 Days 2 h 20 m 4 sec
Reputation Power: 3
|
|
|
RE: php running as a program?
you can run a php script on your server the same way you run a wsh or perl or batch file. Here is a tcp server script I stole from php developers cook book page 359.
SOLUTION
use php's lower level socket funtions along with the set_time_limit() function to create a simple server:
Code:
<?php
set_time_limit(0);
$addr = "127.0.01";
$port = 5000;
$sock = socket(AF_INET, SOCK_STREAM, 0);
if($sock < 0)
die(strerror($sock));
if(($ret = setsockopt($sock, SOL_SOCKET, SO_REUSEADDR< 1))<0)
print strerror($ret);
if(($ret =bind($sock, $addr, $port)) < 0)
die(strerror($ret));
if(($ret = listen($sock, 10)) < 0)
die(strerror($ret));
while (($csock = accept_connect($sock)) >= 0){
// .. manipulate $csock here.
}
close ($sock);
?>
I didn't check this for parse errors
To create a TCP server, you must first create a new socket witha domain of AF_INET and a type of SOCK_STREM. Then you bind() that socket to an addresss and port. Following that, you have to tell PHP to listen() on that socket, with a maximum number of conections specified by the second argument. Finally , ;you have to accept new conections to teh socket. For that, you can use PHP's accept_connect() funciton, which will accpet a new socket that can be both read from and listened to. When you close this socket, a new socket will be accepted.
again this is straight from PHP developer's cook book
copyright 2001 by sterling hughes
|