douhezi2285 2019-01-18 08:04
浏览 202

PHP Socket陷入接受

I have a php socket server and a javascript websocket, but websocket stuck in connecting to socket. There is no error but websocket stay connecting.

sock.php

set_time_limit(0);

$ip   = '127.0.0.1';


$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$ret  = socket_bind($sock, $ip);
$ret  = socket_listen($sock);

do 
{
 $ref = socket_accept($sock);

 /*
   Write message
 */
 $msg ="Success receive from client
";

 socket_write($ref, $msg, strlen($msg));

 /*
   Read message
 */
 $buf = socket_read($ref , 1024);

 echo "Received message: $buf
";

 socket_close($ref);

}while (true);

socket_close($sock);

?>

javascript websocket:

var sock = new WebSocket('ws://localhost/sock.php');
sock.onopen(function()
{
 console.log('socket connected.');
});
  • 写回答

1条回答 默认 最新

  • dongwei1855 2019-01-18 16:17
    关注

    You must specify the correct port in socket_bind() or it will listen on a random port. You check this with netstat on Linux and Windows. As you try to connect to ws://localhost that should be port 80. However binding to port 80 is usually not allowed, unless you are an admin user. Best to bind to another port, for example 8080, and connect to ws://localhost:8080.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错