为什么服务器端收不到客户端的信息呢?
程序代码:
<?php error_reporting(E_ALL); set_time_limit(0); echo "<h2>TCP/IP Connection</h2>\n"; $service_port = 20122; $address = "127.0.0.1"; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket < 0) { echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n"; } else { echo "OK.<BR />"; } echo "试图连接 '$address' 端口 '$service_port'...<br>"; $result = socket_connect($socket, $address, $service_port); if ($result < 0) { echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result); } else { echo "连接OK<br>"; } $out = ''; $out1=''; $in = "hello kitty!"; if(!socket_write($socket, $in, strlen($in))) { echo "socket_write() failed: reason: " . socket_strerror($socket) . "\n"; } else { echo "发送到服务器信息成功!<br>"; echo "发送的内容为:<font color='pink'>$in</font> <br />"; } $out = socket_read($socket, 1024); echo "接收服务器回传信息成功!<br>"; echo "接受的内容为:".$out."<br />"; //socket_close($result); echo "关闭SOCKET...<br>"; socket_close($socket); echo "关闭OK<br>"; ?>这个是客户端的页面
程序代码:
<?php //确保在连接客户端时不会超时 error_reporting (e_all); // 设置运行时间 set_time_limit (0); // 起用缓冲 ob_implicit_flush (); //设置IP和端口号 $address='127.0.0.1'; $port=20122; //调试的时候,可以多换端口来测试程序! echo "start\n<br />"; $protocol = getprotobyname("tcp"); //创建一个SOCKET $sock=socket_create(AF_INET,SOCK_STREAM,$protocol); if($sock<0) { echo "socket_create() 失败的原因是:".socket_strerror($sock)."<br>"; } //绑定到socket端口 if(($ret=socket_bind($sock,$address,$port))<0) //if(!@socket_bind($sock,$address,$port)) { echo "socket_bind() 失败的原因是:".socket_strerror($ret)."<br>"; } //开始监听 if(($ret=socket_listen($sock))<0) //if(!@socket_listen($sock)) //无效参数??? { echo "socket_listen() 失败的原因是:".socket_strerror($ret)."<br>"; } if (($msgsock = socket_accept($sock)) < 0) { echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n"; break; } //发到客户端 //$msg ="<font color=olive>you have send a message to client!</font><br />"; //socket_write($msgsock, $msg, strlen($msg)); $buf=$_POST['buf']; socket_write($msgsock, $buf, strlen($buf)); echo "读取客户端发来的信息<br>"; $bufmsg = socket_read($msgsock,1024); if($bufmsg==0){ echo "the string is empty<br />"; $bufmsg="not recieved<br />"; //break; } $talkback = "收到的信息:".$bufmsg."<br />"; echo $talkback."<br />"; echo "<br />message is :".$buf; socket_close($msgsock); socket_close($sock); ?> <form action="server_01.php" method="post"> 请选则发送方式:<br><input type=text name="buf"> <input type=submit name="submit" value="发送"> </form>这个是server端的代码
客户端能接受到服务器端的信息,但服务器接受不到客户端的信息
哪位高手指点一下,谢谢了!