2 zxc0324 zxc0324 于 2016.02.24 15:48 提问

websocket client执行socket_write 就断开连接

php 充当websocket客户端,模拟向服务器端发消息,但是一执行socket_write连接就丢失了。
是因为php是短连接吗?我在服务端有监控发送的消息,没有收到消息。

代码:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($socket, '********', ***);
echo $result."\n";
echo "Send Command..........\n";
$msg = 'test msg';
socket_write($socket, $msg, strlen($msg));
echo "OK\n";
echo "Reading Backinformatin:\n\n";
while ($out = socket_read($socket, 2048)) {
echo $out." out\n";
}
echo "Close socket........\n";
//socket_close($socket);
echo "OK,He He.\n\n";

2个回答

shanhexiaohe
shanhexiaohe   2016.02.24 16:35

if (socket_write($msgsock, $msg, strlen($msg)) === false)

{

echo "socket_write() failed: reason: " . socket_strerror(socket_last_error()) ."/n";

die;

}

看看能不能把错误日志打印出来看看。

qq_23395637
qq_23395637   2016.02.24 18:02

$errno = "";
$errstr = "";
$fp = fsockopen('192.168.1.37','1000',$errno,$errstr,30);
if(!$fp)
{
echo $errstr;
echo "connect fail";
die;
}
fputs($fp,"GET hehe&a=aaa&b==c22222c");
$str = "";
while(!feof($fp))
{
$date = fgets($fp, 1024);
$str .= $date;
}

fclose($fp);
echo $str;

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!