使用php在2个套接字客户端之间交换数据

时间:2015-08-22 15:26:08

标签: php sockets websocket serversocket

我正在开发一个基于套接字的新应用程序,用于与我的客户进行交互,我希望使用套接字编程来实现这一点。

我们说server socket正在127.0.0.1端口的8888上运行。当客户端连接到我的服务器&如果他试图向管理员发送消息,管理员已从另一个套接字客户端连接。

但是我遇到了在管理员和管理员之间交换数据的问题。客户端套接字。但是,服务器套接字正在接受来自admin&amp ;;的数据。客户也是。当我试图将客户端数据发送给管理员时,它不会被传递。

server.php

<?
    set_time_limit(0);

    // Set the ip and port we will listen on
    $address = '127.0.0.1';
    $port = 8888;

    // Create a TCP Stream socket
    $sock = socket_create(AF_INET, SOCK_STREAM, 0) or die('Could not bind to address'); // 0 for  SQL_TCP

    // Bind the socket to an address/port
    socket_bind($sock, 0, $port) or die('Could not bind to address');  //0 for localhost

    // Start listening for connections
    socket_listen($sock);

    $admin = null;
    $clients = array();

    while(true) {
        /* Accept incoming  requests and handle them as child processes */
        $client = socket_accept($sock);

        if(socket_getpeername($client, $address, $port)) {
            echo "Client $address : $port is now connected to us. \n";
        }

        // Read the input  from the client – 2MB bytes
        $input = socket_read($client, 2097152);
        $data = json_decode($input);

        print_r($data);

        if($data->connection === "admin"){
            echo "Admin connection\n";
            $admin = $client;
        }else if($data->connection === "client"){
            echo "Got data from client\n";
            $message = array('client_id' => $data->id, 'message' => $data->message);

            $clients[$data->id] = $client;

            if($admin !== null){
                echo "Sending data to admin\n";
                socket_write($admin, json_encode($message), strlen(json_encode($message)));
            }else{
                echo "Admin not available, sending response to client\n";
                $message = 'Admin not available!';
                socket_write($client, $message, strlen($message));
            }
        }

        print_r($clients);

        // socket_write($client, $response);
        // socket_close($client);
    }

    // Close the master sockets
    socket_close($sock);
?>

admin.php

<?
    set_time_limit(0);

    $server = '128.199.XXX.XXX';
    $port = '8888';

    if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0))) {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);

        die("Couldn't create socket: [$errorcode] $errormsg \n");
    }

    echo "Socket created \n";

    //Connect socket to remote server
    if(!socket_connect($sock, $server, $port)) {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);

        die("Could not connect: [$errorcode] $errormsg \n");
    }

    echo "Connection established \n";

    $message = array('connection' => 'admin');

    //Send the message to the server
    if(!socket_send($sock, json_encode($message), strlen(json_encode($message)), 0)) {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);

        die("Could not send data: [$errorcode] $errormsg \n");
    }

    echo "Message send successfully \n";

    if(socket_recv($sock, $buf, 10, MSG_WAITALL) === FALSE) {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);

        die("Could not receive data: [$errorcode] $errormsg \n");
    }

    print_r(json_decode($buf));

?>

client.php

<?
    set_time_limit(0);

    $server = '128.199.XXX.XXX';
    $port = '8888';

    if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0))) {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);

        die("Couldn't create socket: [$errorcode] $errormsg \n");
    }

    echo "Socket created \n";

    //Connect socket to remote server
    if(!socket_connect($sock, $server, $port)) {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);

        die("Could not connect: [$errorcode] $errormsg \n");
    }

    echo "Connection established \n";

    $message = array('connection' => 'client', 'id' => time(), 'message' => 'Hi');

    //Send the message to the server
    if(!socket_send($sock, json_encode($message), strlen(json_encode($message)), 0)) {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);

        die("Could not send data: [$errorcode] $errormsg \n");
    }

    echo "Message send successfully \n";

    if(socket_recv($sock, $buf, 10, MSG_WAITALL) === FALSE) {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);

        die("Could not receive data: [$errorcode] $errormsg \n");
    }

    print_r(json_decode($buf));
?>

以前有人可以这样做吗?租约帮助我实现它。

提前致谢: - )

0 个答案:

没有答案