socket 同時只能有一個用戶端串連一個連接埠並往上面發送資料

來源:互聯網
上載者:User
求助 socket 同時只能有一個用戶端串連一個連接埠並往上面發送資料
同時只能有一個用戶端串連一個連接埠(如8090)並往上面發送資料,當有第二個用戶端串連時這個8090連接埠時 就串連不上了
有的朋友說服務端就accept一次,那如何accept多次啊!下面貼上My Code

$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");


}




------解決方案--------------------
用非阻塞模式,具體百度
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.