php socket 实现长连接 php socket 怎么实现长连接啊当socket_write()之后连接就关闭了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 PHP_FUNCTION(socket_write){ zval *arg1; php_socket *php_sock; int retval, str_len; long length = 0; char *str; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &arg1, &str, &str_len, &length) == FAILURE) { return; } ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket); if (ZEND_NUM_ARGS() < 3) { length = str_len; }#ifndef PHP_WIN32 retval = write(php_sock->bsd_socket, str, MIN(length, str_len));#else retval = send(php_sock->bsd_socket, str, min(length, str_len), 0);#endif if (retval < 0) { PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno); RETURN_FALSE; } RETURN_LONG(retval);}这是socket_write的源代码, 就取到套接字描述符, 直接调了send/write写了数据, 没有关闭, 你源代码粘出来看看应用场景吧? $service_port=9999;$address="192.168.0.1"; $socket=socket_create(AF_INET,SOCK_STREAM,0);$result=socket_connect($socket,$address,$service_port);$InfoStr = '';socket_write($socket,$InfoStr,strlen($InfoStr));没写关闭 你没有伪死循环,至少你给的代码中没有那么 socket_write 后程序不就结束了么?自然连接也就关闭了 if(isset($info['liangshi']) && !empty($info['liangshi'])) { $nTp1 = "0"; $nTp2 = "0"; $nValue = $info['liangshi']; $InfoStr = pack("S",$nLen);//unsigned short 2 $InfoStr .= pack("C",$Ident);//unsigned char 1 $InfoStr .= pack("C",$Child);//unsigned char 1 $InfoStr .= pack("V",$PID);//__int64 8 $InfoStr .= pack("V",$hight); $InfoStr .= pack("C",$ntypes);//unsigned char 1 $InfoStr .= pack("V",$ID);//__int64 8 $InfoStr .= pack("V",$hight); $InfoStr .= pack("C",$nTp1);//unsigned char 1 $InfoStr .= pack("C",$nTp2);//unsigned char 1 $InfoStr .= pack("l",$nValue);//int 4 $InfoStr .= pack("l",0);//int 4 socket_write($socket,$InfoStr,strlen($InfoStr)); $InfoStr = ''; } if(isset($info['mutou']) && !empty($info['mutou'])) { $nTp1 = "1"; $nTp2 = "0"; $nValue = $info['mutou']; $InfoStr = pack("S",$nLen);//unsigned short 2 $InfoStr .= pack("C",$Ident);//unsigned char 1 $InfoStr .= pack("C",$Child);//unsigned char 1 $InfoStr .= pack("V",$PID);//__int64 8 $InfoStr .= pack("V",$hight); $InfoStr .= pack("C",$ntypes);//unsigned char 1 $InfoStr .= pack("V",$ID);//__int64 8 $InfoStr .= pack("V",$hight); $InfoStr .= pack("C",$nTp1);//unsigned char 1 $InfoStr .= pack("C",$nTp2);//unsigned char 1 $InfoStr .= pack("l",$nValue);//int 4 $InfoStr .= pack("l",0);//int 4 socket_write($socket,$InfoStr,strlen($InfoStr)); $InfoStr = ''; }然后在这个循环的最后写了个for($i = 0; $i<10000000000; $i++) { }测试了一下也还是不行 就打算你想无条件的连续执行 socket_write你自己看看你的代码,你给他第二次执行的机会了吗? 去看看服务端有没有主动关闭?用tcpdump把通信包打出来看看谁发的第一个FIN 贴一段代码if (isset($_POST['submit'])) { ignore_user_abort(true); set_time_limit(0); $address = '127.0.0.1'; $port = getport(); $port = 1000; $sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or exit('Error'); $ret = 0; while (!$ret) { $ret = @socket_bind($sock, $address, getport()); } $ret = socket_listen($sock, 4) or exit('Error'); ob_start(); ob_end_flush(); for ($i = 0; $i < 1024; $i++) { echo ' '; } while (true) { if (!($msgsock = socket_accept($sock))) { exit; } $buf = socket_read($msgsock, 8192); if ($buf) { $talkback = "<font color=red>收到的信息:$buf</font><p>"; echo $talkback; @ob_end_flush(); } usleep(1); flush(); $talkback = 'abc' . rand(); socket_write($msgsock, $talkback, strlen($talkback)); } socket_close($sock);} 用PHP语言展示产品图片怎么弄? 要滚动的 取mysql字段注释问题 /language/change/ja 关于字符串的一个算法请教 求一个无限分类树状????? php如何上传大文件? 用php开发b/s结构的软件产品,如何给客户分配序列号???请产品专家指点指点 怎么用PHP编写多邮箱登陆程序 PHP,,其实稳不稳定的呢? cURL请求第三方接口慢,导致阻塞 yii框架,条件查询问题 如何调用php类文件
PHP_FUNCTION(socket_write)
{
zval *arg1;
php_socket *php_sock;
int retval, str_len;
long length = 0;
char *str; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &arg1, &str, &str_len, &length) == FAILURE) {
return;
} ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket); if (ZEND_NUM_ARGS() < 3) {
length = str_len;
}#ifndef PHP_WIN32
retval = write(php_sock->bsd_socket, str, MIN(length, str_len));
#else
retval = send(php_sock->bsd_socket, str, min(length, str_len), 0);
#endif if (retval < 0) {
PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
RETURN_FALSE;
} RETURN_LONG(retval);
}这是socket_write的源代码, 就取到套接字描述符, 直接调了send/write写了数据, 没有关闭, 你源代码粘出来看看应用场景吧?
$address="192.168.0.1";
$socket=socket_create(AF_INET,SOCK_STREAM,0);
$result=socket_connect($socket,$address,$service_port);$InfoStr = '';socket_write($socket,$InfoStr,strlen($InfoStr));没写关闭
那么 socket_write 后程序不就结束了么?
自然连接也就关闭了
{
$nTp1 = "0";
$nTp2 = "0";
$nValue = $info['liangshi'];
$InfoStr = pack("S",$nLen);//unsigned short 2
$InfoStr .= pack("C",$Ident);//unsigned char 1
$InfoStr .= pack("C",$Child);//unsigned char 1
$InfoStr .= pack("V",$PID);//__int64 8
$InfoStr .= pack("V",$hight);
$InfoStr .= pack("C",$ntypes);//unsigned char 1
$InfoStr .= pack("V",$ID);//__int64 8
$InfoStr .= pack("V",$hight);
$InfoStr .= pack("C",$nTp1);//unsigned char 1
$InfoStr .= pack("C",$nTp2);//unsigned char 1
$InfoStr .= pack("l",$nValue);//int 4
$InfoStr .= pack("l",0);//int 4
socket_write($socket,$InfoStr,strlen($InfoStr));
$InfoStr = '';
}
if(isset($info['mutou']) && !empty($info['mutou']))
{
$nTp1 = "1";
$nTp2 = "0";
$nValue = $info['mutou'];
$InfoStr = pack("S",$nLen);//unsigned short 2
$InfoStr .= pack("C",$Ident);//unsigned char 1
$InfoStr .= pack("C",$Child);//unsigned char 1
$InfoStr .= pack("V",$PID);//__int64 8
$InfoStr .= pack("V",$hight);
$InfoStr .= pack("C",$ntypes);//unsigned char 1
$InfoStr .= pack("V",$ID);//__int64 8
$InfoStr .= pack("V",$hight);
$InfoStr .= pack("C",$nTp1);//unsigned char 1
$InfoStr .= pack("C",$nTp2);//unsigned char 1
$InfoStr .= pack("l",$nValue);//int 4
$InfoStr .= pack("l",0);//int 4
socket_write($socket,$InfoStr,strlen($InfoStr));
$InfoStr = '';
}然后在这个循环的最后写了个for($i = 0; $i<10000000000; $i++)
{
}测试了一下也还是不行
你自己看看你的代码,你给他第二次执行的机会了吗?
去看看服务端有没有主动关闭?
用tcpdump把通信包打出来看看谁发的第一个FIN
ignore_user_abort(true);
set_time_limit(0);
$address = '127.0.0.1';
$port = getport();
$port = 1000; $sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or exit('Error');
$ret = 0;
while (!$ret) {
$ret = @socket_bind($sock, $address, getport());
}
$ret = socket_listen($sock, 4) or exit('Error'); ob_start();
ob_end_flush();
for ($i = 0; $i < 1024; $i++) {
echo ' ';
} while (true) {
if (!($msgsock = socket_accept($sock))) {
exit;
} $buf = socket_read($msgsock, 8192);
if ($buf) {
$talkback = "<font color=red>收到的信息:$buf</font><p>";
echo $talkback;
@ob_end_flush();
}
usleep(1);
flush();
$talkback = 'abc' . rand();
socket_write($msgsock, $talkback, strlen($talkback));
}
socket_close($sock);
}