Forking sockets

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gkep
Forum Newbie
Posts: 1
Joined: Fri May 03, 2002 6:14 pm

Forking sockets

Post by gkep »

Hi everyone, I've been 'commissioned' to write an application for primary schools that filters bad language from inbound email. I figured I'd write a PHP script that acts as fake POP server and sits between the client and the real POP server, filtering emails on the way through.

I've managed to get the script up and running but I'm concerned about its robustness and whether it can handle multiple connections at the same time. Is this even possible with PHP or am I barking up the wrong tree here?

Here it is so far:

<?

// run with "php -q this.php"

// don't timeout
set_time_limit (0);

// this is the 'fake' POP server
$host = "127.0.0.1";
$port = 110;

// this is the 'real' pop server
$destHost="mail.sample.com";
$destPort = 110;

// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket
");

// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket
");

// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener
");

echo "Waiting for connections...
";

// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection
");

echo "Received connection request
";

// open a client connection
$destFp = fsockopen ($destHost, $destPort, $destErrno, $destErrstr);


// send first reply to client
$welcome = fgets ($destFp, 150);
socket_write($spawn, $welcome, strlen($welcome)) or die("Could not write client output
");
echo "Send to client: ". $welcome;

// keep looping and looking for client input
do
{
$tmpData = "";
while(($buf = socket_read($spawn, 512)) !== false) {
$tmpData .= $buf;
if(strpos($tmpData,"
")) break;
}
$input = $tmpData;
echo "Recv from client: ". $input;


// send client command to dest
echo "Send to dest: ". $input;
fputs ($destFp, $input);

// read response from dest
$destRecv = "";
$terminator = "
";
while(strpos($destRecv,$terminator)===false) {
$destRecv .= fgets ($destFp, 50);
if (strpos($destRecv, " follow.") > 0) $terminator = "
.
";
}
echo "Recv from dest: ". $destRecv;

// FILTER LANGUAGE HERE


// send to client
socket_write($spawn, $destRecv, strlen($destRecv)) or die("Could not write client output
");
echo "Send to client: ". $destRecv;

} while (true);

// close primary socket
socket_close($socket);
echo "Socket terminated
";
?>
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post by sam »

You do not want to do this with php... I would suggest moving over to C++ or Python or some other multithreaded language. PHP has very experamental thread support but it is not going to make your code any more robust.

Cheers Sam
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

listen to sam

Post by romeo »

sockets in general isn't php's strong point...
however I would love to see the complete code posted for anyone who may want something like that for thier personal servers :)
you did a good job :)
Post Reply