同时只能有一个客户端连接一个端口(如8090)并往上面发送数据,当有第二个客户端连接时这个8090端口时 就连接不上了
有的朋友说服务端就accept一次,那如何accept多次啊!下面贴上我的代码$commonProtocol = getprotobyname("tcp");
$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);
@socket_bind($socket, '192.168.1.202', 8888);
@socket_listen($socket);
// Initialize the buffer
$buffer = "NO DATA";
while(true)
{
sleep(1);    
 // Accept any connections coming in on this socket $connection = @socket_accept($socket);
 printf("Socket connected\r\n");
 // Check to see if there is anything in the buffer
 if($buffer != "")
 {
  printf("Something is in the buffer...sending data...\r\n");
  @socket_write($connection, $buffer . "\r\n");
  printf("Wrote to socket\r\n");
 }
 else
 {
  printf("No Data in the buffer\r\n");
 }
 // Get the input
 while($data = @socket_read($connection, 14, PHP_NORMAL_READ))
 {
     
     if (!empty($data)){
         $buffer = $data;   //文件驱动模式 
      /*$f = fopen(dirname(__FILE__).'/file/'.date('YmdHis').'.txt','w');
    fwrite($f,$buffer); 
    fclose($f); */ 
 
   socket_set_nonblock($connection);
   socket_getpeername($connection,&$remoteIP,&$remotePort);
echo $remoteIP."\r\n";
echo $remotePort."\r\n";
 
 
 
 $data=str_split($buffer);
 
 print_r($data);
 
 foreach($data as $v){
     echo dechex(ord($v))."\t";
     }
 echo "\r\n";
 
     }
  
  
 }
 @socket_close($connection);
 printf("Closed the socket\r\n\r\n");
 
 
}

解决方案 »

  1.   

    不是就 服务端就accept一次 的问题
    你需要搞清楚 web 方式与 from 方式的区别
    你的程序监听 8888 端口。
    在 web 方式下,你的程序对于每一个用户都产生一个副本。于是就形成多个程序监听一个端口的局面,这是在网络通讯上是不允许的你需要在第一次收到用户 sock 请求后,令用户跳转(可通过发 loction 头)到另外的监听其他空闲端口的程序中
      

  2.   

    见手册的注释部分.
    http://php.net/manual/en/function.socket-accept.php关于socket编程,最好的是看c/unix的编程书里的socket部分.