मैं एक साधारण क्लाइंट/सर्वर एप्लिकेशन बनाने की कोशिश कर रहा हूं और इस प्रकार मैं PHP में सॉकेट के साथ प्रयोग कर रहा हूं।PHP सॉकेट्स - एकाधिक कनेक्शन स्वीकार करें
अब मेरे पास सी # में एक साधारण क्लाइंट है जो सर्वर से अच्छी तरह से कनेक्ट होता है, लेकिन मैं केवल एक ही क्लाइंट को इस सर्वर से कनेक्ट कर सकता हूं (मुझे यह कोड नमूना ऑनलाइन मिला और परीक्षण उद्देश्यों के लिए थोड़ा सा tweaked)।
अजीब पर्याप्त मैं एक ही उदाहरण यहां के आधार पर एक ही सवाल पाया,: https://stackoverflow.com/questions/10318023/php-socket-connections-cant-handle-multiple-connection
मैं इसके बारे में हर हिस्से समझने की कोशिश की और मैं इसे कैसे विस्तार से काम करता है को देखने के लिए करीब हूँ, लेकिन किसी कारण से, जब मैं दूसरे क्लाइंट को कनेक्ट करता हूं, तो पहला डिस्कनेक्ट/क्रैश हो जाता है।
क्या कोई मुझे कुछ जंगली विचार या पॉइंटर दे सकता है जहां मुझे देखना चाहिए?
<?php
// Set time limit to indefinite execution
set_time_limit (0);
// Set the ip and port we will listen on
$address = '127.0.0.1';
$port = 9000;
$max_clients = 10;
// Array that will hold client information
$client = array();
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');
// Start listening for connections
socket_listen($sock);
// Loop continuously
while (true) {
// Setup clients listen socket for reading
$read[0] = $sock;
for ($i = 0; $i < $max_clients; $i++)
{
if (isset($client[$i]))
if ($client[$i]['sock'] != null)
$read[$i + 1] = $client[$i]['sock'] ;
}
// Set up a blocking call to socket_select()
$ready = socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL);
/* if a new connection is being made add it to the client array */
if (in_array($sock, $read)) {
for ($i = 0; $i < $max_clients; $i++)
{
if (!isset($client[$i])) {
$client[$i] = array();
$client[$i]['sock'] = socket_accept($sock);
echo("Accepting incomming connection...\n");
break;
}
elseif ($i == $max_clients - 1)
print ("too many clients");
}
if (--$ready <= 0)
continue;
} // end if in_array
// If a client is trying to write - handle it now
for ($i = 0; $i < $max_clients; $i++) // for each client
{
if (isset($client[$i]))
if (in_array($client[$i]['sock'] , $read))
{
$input = socket_read($client[$i]['sock'] , 1024);
if ($input == null) {
// Zero length string meaning disconnected
echo("Client disconnected\n");
unset($client[$i]);
}
$n = trim($input);
if ($n == 'exit') {
echo("Client requested disconnect\n");
// requested disconnect
socket_close($client[$i]['sock']);
}
if(substr($n,0,3) == 'say') {
//broadcast
echo("Broadcast received\n");
for ($j = 0; $j < $max_clients; $j++) // for each client
{
if (isset($client[$j]))
if ($client[$j]['sock']) {
socket_write($client[$j]['sock'], substr($n, 4, strlen($n)-4).chr(0));
}
}
} elseif ($input) {
echo("Returning stripped input\n");
// strip white spaces and write back to user
$output = ereg_replace("[ \t\n\r]","",$input).chr(0);
socket_write($client[$i]['sock'],$output);
}
} else {
// Close the socket
if (isset($client[$i]))
echo("Client disconnected\n");
if ($client[$i]['sock'] != null){
socket_close($client[$i]['sock']);
unset($client[$i]);
}
}
}
} // end while
// Close the master sockets
echo("Shutting down\n");
socket_close($sock);
?>
क्या आपके पास कभी इसके साथ कोई भाग्य है? –
देखें: [सॉकेटसेवर.क्लास.एफ़पी] (https://gist.github.com/navarr/459321) – kenorb