Ask a Question related to PHP Development, Design and Development.
-
websafe #1
php tcp server - sockets - how to handle more clients at once ?
Hello
I've modified the tcp-server example from [url]http://www.php.net/sockets[/url]
The server waits for a connection, then for \r\n and disconnect
the remote host. My question is, how to modify this code,
to make the server available for more than one remote host
at once? I mean, that more clients could connect at the
same time? Im new with socket programming.
Thanks for any help.
#!/bin/php-cmd -q
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$address = '192.168.1.1';
$port = 99;
echo "Creating...\n";
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
echo "socket_create() failed: reason: " . socket_strerror($sock) . "\n";
}
echo "Binding...\n";
if (($ret = socket_bind($sock, $address, $port)) < 0) {
echo "socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
}
echo "Listening...\n";
if (($ret = socket_listen($sock, 5)) < 0) {
echo "socket_listen() failed: reason: " . socket_strerror($ret) . "\n";
}
do {
echo "Waiting for a connection...\n";
if (($msgsock = socket_accept($sock)) < 0) {
echo "socket_accept() failed: reason: " .
socket_strerror($msgsock) . "\n";
}
socket_getpeername($msgsock,$ip, $port);
echo "Incoming connection from {$ip}:{$port}\n";
$msg = "+OK {$ip}:{$port}\r\n";
socket_write($msgsock, $msg, strlen($msg));
$buf = socket_read($msgsock, 8192, PHP_NORMAL_READ);
echo $buf;
$msg = "+OK Received: $buf\r\n";
socket_write($msgsock, $msg, strlen($msg));
socket_close($msgsock);
echo "Connection closed...\n";
} while(true);
socket_close($sock);
?>
websafe Guest
-
Handle disconnected clients?
Hello, I have a WebMethod that returns a large array of bytes (actually some files, but i don't think it matters). How can i know on the... -
Retrieving clients ip adress on the server
Hello, I have looked a lot but not found how to do this: I have a Web Services server and from this I want to see what IP adress the Client... -
AIX DHCP Server and W2K Clients: help!
Hi, I am trying to set up a "simple" AIX DHCP server directly connected to W2K/NT Clients (no routers). Everything is properly connected, but the... -
WTT: NT Server w/ 10 Clients
Have Windows NT 4.0 Server with 10 Client licenses. Fully registerable. Wanted Dreamweaver/Flash/Fireworks package. Newest/Registerable only. ... -
NT-Server and 2 XP-Clients no shares??
Hallo all, i'm close to going crazy trying to link two XP Pro clients to an NT 4 server, so that the two clients can communicate with each other!!... -
Majid #2
Re: php tcp server - sockets - how to handle more clients at once ?
i'm like you,this code will accept more than one client,but it can't receive data from them,i'm trying to solve that,hope this one will help
<?php
//Server
set_time_limit(0);
$host='127.0.0.1';
$port=9997;
$socket=socket_create(AF_INET,SOCK_STREAM,0);
socket_bind($socket,$host,$port);
socket_listen($socket);
$client=array($socket);
while (true) {
//$read=$client;
$cliid=0;
socket_select($client,$write=NULL,$except=NULL,$tv _sec=NULL);
if(in_array($socket,$client)) {
for($i=1;$i<5;$i++)
{
if(!isset($client[$i]))
{
$client[$i]['socket']=socket_accept($socket);
$cliid+=1;
socket_getpeername($client[$i]['socket'], $ip);
echo "Client #".$cliid." With IP {$ip} Connected\n";
$msg="\nWelcome to the PHP Test Server. \n" .
"To quit, type 'quit'.\n";
socket_write($client[$i]['socket'],$msg);
} //inside if
} //for
break;
}
} //while
socket_close($socket); //for accepting
?>Majid Guest



Reply With Quote

