求助:PHP+SOCKET 由于套接字没有连接并且没有提供地址,发送或接收数据的请求没有被接受。
本帖最后由 subendong 于 2011-06-03 16:39:41 编辑 由于之前连续发布了两篇帖子,无一有回,所以先向版主申请删除之前两篇类似的帖子,不然留着无用。就像垃圾一样。
然后希望大神,能根据我提供的代码,稍作修改,使得代码能够正确运行,谢谢!
具体如下:
Warning: socket_read(): unable to read from socket [0]: 由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。
我建了两个文件xx.php是在服务器端执行的文件。x.php是在客户端执行的文件。
xx.php
<?php
// 设置一些基本的变量
global $lat,$lnt;
$host = "192.168.1.110";
$port = 8080;
// 设置超时时间
set_time_limit(0);
// 创建一个Socket
$commonProtocol = getprotobyname("tcp");
$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);
//绑定Socket到端口
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$ret = socket_listen($socket, 5);
if($ret)
{
while (true)
{
$spawn = socket_accept($socket);
if(!$spawn) break;
$output = "congratulations,you have connected server:" . $host . "!";
socket_write($spawn, $output, strlen ($output)) or die("error\n");
$input = socket_read($socket, 100000);
socket_write($spawn, $input, strlen ($input)) or die("error\n");
socket_close($spawn);
}
}
socket_close();
?>
<?php
// 设置一些基本的变量
$host = "192.168.1.110";
$port = 8080;
// 设置超时时间
set_time_limit(0);
// 创建一个Socket
$commonProtocol = getprotobyname("tcp");
$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);
if(!$socket) echo "can't creat socket";
//绑定Socket到端口
$result = socket_connect($socket,$host,$port);
if($result)
{
$output="abc";
socket_write($socket, $output, strlen ($output));
$input = socket_read($socket, 1024);
}
echo $input;
?>
// Client
// 设置错误处理
error_reporting (E_ALL);
// 设置处理时间
set_time_limit (0);
$ip = ""; // IP 地址
$port = 1000; // 端口号
$socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); // 创建一个SOCKET
if ($socket)
echo "socket_create() 成功!<br>\n";
else
echo "socket_create() 失败:".socket_strerror ($socket)."<br>\n";
$conn = socket_connect ($socket, $ip, $port); // 建立SOCKET的连接
if ($conn)
echo "成功连接到[".$ip.":".$port."]<br>\n";
else
echo "socket_connect() 失败:".socket_strerror ($conn)."<br>\n";
echo socket_read ($socket, 1024)."<br>";
$stdin = fopen ('php://stdin', 'r');
$ar = array("HELLO","HELP","test","QUIT");
$i = 0;
while (true)
{
// $command = trim (fgets ($stdin, 1024));
// socket_write ($socket, $command, strlen ($command));
echo "发送 $ar[$i]<br>";
socket_write ($socket, $ar[$i], strlen($ar[$i]));
$i++;
$msg = trim (socket_read ($socket, 1024));
echo "收到:$msg<br>\n";
if ($msg == "Bye-Bye")
break;
}
fclose ($stdin);
socket_close ($socket);
// Server
// 设置错误处理
error_reporting (E_ALL);
// 设置运行时间
set_time_limit (0);
// 起用缓冲
ob_implicit_flush ();
$ip = ""; // IP地址
$port = 1000; // 端口号
$socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); // 创建一个SOCKET
if ($socket)
echo "socket_create() successed!\n";
else
echo "socket_create() failed:".socket_strerror ($socket)."\n";
$bind = socket_bind ($socket, $ip, $port); // 绑定一个SOCKET
if ($bind)
echo "socket_bind() successed!\n";
else
echo "socket_bind() failed:".socket_strerror ($bind)."\n";
$listen = socket_listen ($socket); // 间听SOCKET
if ($listen)
echo "socket_listen() successed!\n";
else
echo "socket_listen() failed:".socket_strerror ($listen)."\n";
while (true)
{
$msg = socket_accept ($socket); // 接受一个SOCKET
if (!$msg)
{
echo "socket_accept() failed:".socket_strerror ($msg)."\n";
break;
}
$welcome = "服务端收到:Welcome to PHP Server!\n";
socket_write ($msg, $welcome, strlen ($welcome));
while (true)
{
$command = strtoupper (trim (socket_read ($msg, 1024)));
if (!$command)
break;
switch ($command)
{
case "HELLO":
$writer = "Hello Everybody!";
break;
case "QUIT":
$writer = "Bye-Bye";
break;
case "HELP":
$writer = "HELLO\tQUIT\tHELP";
break;
default:
$writer = "Error Command!";
}
socket_write ($msg, $writer, strlen ($writer));
if ($command == "QUIT")
break;
}
socket_close ($msg);
if ($command == "QUIT")
break;
}
socket_close ($socket); // 关闭SOCKET