LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 939|回复: 2

php socket 问题,求教!!

[复制链接]
发表于 2004-8-20 16:43:37 | 显示全部楼层 |阅读模式
下列代码无返回内容显示,也没有报错!
究竟问题在哪?问了好多人都没结果!
我的配置:
Apache HTTP Server Version 1.3
PHP Version 4.1.2
Redhat 7.3
按手册上说的配置应该没问题!
代码有人说socket_read()的不应为0,我改成1024,仍没显示!
<?

/*get the post "select * from music"*/
$sql = "select * from music";

error_reporting(E_ALL);

/* Get the port for the WWW service. */
$service_port = 5000;

/* Get the IP address for the target host. */
$address = '192.168.0.1';

/* Timeout set. */
//$timeout = 15;

/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0) {
    echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
} else {
    echo "\n";
}

$result = socket_connect($socket, $address, $service_port);
if ($result < 0) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
} else {
    echo "\n";
}

socket_write($socket, $sql, strlen($sql));

while ($out = socket_read($socket, 0,PHP_BINARY_READ)) {
    echo $out;
}

socket_close($socket);

?>

渴望,能解决!!

相关socket_read()说明文档:
http://cn2.php.net/socket_read
 楼主| 发表于 2004-8-23 09:14:57 | 显示全部楼层

没人会吗?

没人会吗?
发表于 2004-8-31 14:15:24 | 显示全部楼层
你应该学会用调试自己的程序。
相信你也读过文档了。
试着在你的代码中加上,socket_last_error()来检查出了什么错。
还有在你的这段代码中是以二进制的方式读取的的数据。
是不是这个原因呢?(我对PHP不熟,但是经验告诉我可能是这儿有问题。)

还有你显然是没将文档看透切。
<?php

   ...  //initialising communication

   $string = "ready to get/send data\0";
   socket_write($socket, $string);

   //now you can read from...
   $line = trim(socket_read($socket, MAXLINE));

   ...  // do some stuff, finaly close connection
?>
这是例子代码,和你的比一下吧。你会发现有很多问题。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表