日期:2014-05-17  浏览次数:20473 次

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

$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

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

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

$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if($socket < 0) {
        echo "Socket create:".$socket_strerror($socket)."\n";
        exit;
}
if ( ($ret = socket_bind($socket, $addr, $port)) < 0 ) {
    echo "socket bind:".socket_strerror()."\n";
    exit;
}
if ( ($ret = socket_listen($socket, 5)) < 0 ) {
    echo "socket listen:".socket_strerror()."\n";
    exit;
}

socket_set_nonblock($socket);

echo "Wainting for a connection:\n";

$clients = array($socket);
while(true) {
    $read = $clients;
    if(socket_select($read, $writes=NULL, $execs=NULL, 3) < 1){
        //echo ".";
        continue;
    }

    if(in_array($socket,$read)) {
        $clients[] = $newsock = socket_accept($socket);
        socket_set_nonblock($newsock);
        socket_getpeername($newsock,&$remoteIP,&$remotePort);        
        $key = array_search($socket,$read);
        unset($read[$key]);
        $msg = "Server: $addr\r\n".
                "Notice:Welcome the user from ".$remoteIP.":".$remotePort.".\r\n";
        echo $remoteIP.":".$remotePort." conected in.\r\n";
        socket_write($newsock,$msg);
    }

    foreach($read as $read_sock) {
        $data = @socket_read($read_sock,1024,PHP_BINARY_READ);
        if($data == false) {
            $key = array_search($read_sock,$clients);
            print_r($clients[$key]);
            unset($clients[$key]);
            socket_close($read_sock);
            echo "Client disconnected.\n";
            continue;
        }
        
        $command = strtoupper ($data);
        
        switch ($command)
        {
            case "LIST":
                $writer = print_r($clients,1);
                break;
            case "HELLO":
                $writer = "Hello Everybody!";
                break;
            case "QUIT":
                $writer = "Bye-Bye";
                break;
            case "SLEEP":
                sleep(30);
                $writer = "sleeped 10 seconds!";
                break;
            case "HELP":
                $writer = "HELLO\tQUIT\tHELP";
                break;
            default:
                $writer = "Error Command!\r\nType help for help message!";
        }

        socket_write($read_sock, $writer);
        //exit;
    }
}
socket_close($socket);



客户端:
PHP code

// 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"