• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

socket 同时只能有一个客户端连接一个端口并往下面发送数据

php 搞代码 4年前 (2022-01-23) 24次浏览 已收录 0个评论

求助 socket 同时只能有一个客户端连接一个端口并往上面发送数据
同时只能有一个客户端连接一个端口(如8090)并往上面发送数据,当有第二个客户端连接时这个8090端口时 就连接不上了
有的朋友说服务端就accept一次,那如何accept多次啊!下面贴上我的代码

PHP code

<!---ecms Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$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 = $dat<span>%本文来源gaodai#ma#com搞*代#码9网#</span><strong>搞gaodaima代码</strong>a;   //文件驱动模式       /*$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");  }

——解决方案——————–
用非阻塞模式,具体百度


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:socket 同时只能有一个客户端连接一个端口并往下面发送数据
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址