首页 >后端开发 >php教程 > PHP socket 发到指定的地址,该如何解决

PHP socket 发到指定的地址,该如何解决

WBOY
WBOY原创
2016-06-13 13:28:071132浏览

PHP socket 发到指定的地址
有3个用户通过socket登录到了服务端,通过mysql记录下了三个用户的IP与端口分别为:
A: 192.168.1.100:13564 B:192.168.1.100:13565 C:192.168.1.100:13566

现在我想在客户A发一个指今(包含客户B的端口),服务端接受分析后从数据库取出客户B对应的地址发个TEST字符给B

我在服务端加入如下代码,出现错误,
PHP Warning: socket_connect(): unable to connect [0]: 提供了一个无效的参数。

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$conn = socket_connect ($socket, '192.168.1.100', 13565);  
$buffer = "ok";
$command = socket_sendto($socket,$buffer,strlen($buffer),0,'192.168.1.100','192.168.1.100');



可以在服务端用socket_sendto吗?这个怎么实现啊?


 
服务端:
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$addr = "192.168.1.121";
$port = 1338;

$remoteIP = "";
$remotePort = "";

$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if($socket 


客户端:
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
// Client  
 // 设置错误处理 
 error_reporting (E_ALL); 
 // 设置处理时间 
 set_time_limit (0); 
  
 $ip = "192.168.1.121";       // IP 地址 
 $port = 1338;            // 端口号 
  
 $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"; 
  
 $conn = socket_connect ($socket, $ip, $port);       // 建立SOCKET的连接 
 if ($conn) 
     echo "Success to connection![".$ip.":".$port."]\n"; 
 else 
     echo "socket_connect() failed:".socket_strerror ($conn)."\n"; 
  
 echo socket_read ($socket, 1024);    
  
 $stdin = fopen ('php://stdin', 'r'); 
 while (true) 
 { 
     $command = trim (fgets ($stdin, 1024)); 
     socket_write ($socket, $command, strlen ($command)); 
     $msg = trim (socket_read ($socket, 1024)); 
     echo $msg."\n"; 
     if ($msg == "Bye-Bye") 
         break; 
 } 
 fclose ($stdin); 
 socket_close ($socket);

 <div class="clear">
                 
              
              
        
            </div>
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn