Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP套接字将信息加载到浏览器中_Php_Html_Sockets_Browser_Visualization - Fatal编程技术网

PHP套接字将信息加载到浏览器中

PHP套接字将信息加载到浏览器中,php,html,sockets,browser,visualization,Php,Html,Sockets,Browser,Visualization,大家好,我正在尝试将一些信息加载到浏览器中。我有以下代码: <?php // allow the daemon to run without being timed out set_time_limit(0); $ip = "192.168.0.101"; $port = 123; // set the protocols if( !$socket = socket_create(AF_INET,SOCK_STREAM,0) ){

大家好,我正在尝试将一些信息加载到浏览器中。我有以下代码:

<?php
    // allow the daemon to run without being timed out
    set_time_limit(0);

    $ip = "192.168.0.101";
    $port = 123;

    // set the protocols
    if( !$socket = socket_create(AF_INET,SOCK_STREAM,0) ){
        showError();    
    }

    echo "The socket's protocol info was set \n";

    // bind the socket
    if( !socket_bind($socket,$ip,$port) ){
        showError();
    }
    echo "The socket has been bound to a specific port now ! \n";

    // start listening on this port
    if( !socket_listen($socket) ){
        showError();
    }
    echo "Now listening for connections @ @ @ \n";

    $client = socket_accept($socket);
    echo "new connection with client established !! \n";            
            do {
            // welcome the user
                if(!$clientMsg = socket_read($client, 2048, PHP_NORMAL_READ)) {
                        echo "Error occured while receiving message!\n";
                }

                        if(!$clientMsg = trim($clientMsg)) {
                        continue;
                    }

                    $msg = "Thank you for your message!\n";
                    socket_write($client, $msg, strlen($msg));                    

                    echo "====================================================\n";
                    echo "Message was received successfully!\n";
                    echo $clientMsg."\n";
                    echo "====================================================\n";

                    if ($clientMsg == 'close') {
                        //socket_close($client);
                        break 2;
                    }
        } while (true);
        /*
        // check for any message sent by the user
        do{
            if( ! $clientMssg = socket_read($client,2048,PHP_NORMAL_READ) ){
                showError();
            }

            // say something back
            $messageForUser = "Thanks for your input. Will think about it.";
            socket_write($client,$messageForUser,strlen($messageForUser));

            // was it actually words?
            if( !$clientMssg = trim($clientMssg) ){
                continue;
            }
            if( $clientMssg == 'close' ){
                // close their connection as requested
                socket_close($client);
                echo "\n\n-------------------------------- \n" .
                    "The user has left the connection\n";
                // break out of the loop
                break 2;    
            }

        }while(true); */  
    // end the socket
    echo "Ending the socket \n";
    socket_close($socket);

    // show error details
    function showError( $theSocket = null ){
        $errorcode = socket_last_error($theSocket);
     $errormsg = socket_strerror($errorcode);

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

?>

您可以使用JavaScript和WebSocket向PHP服务器程序发送请求并接收响应。是使用PHP和WebSocket聊天的好教程。

“使其可见”。使什么可见?这是PHP代码。它已经在服务器上运行了…您的意思是要从浏览器向服务器端程序发送消息,然后显示服务器发送的响应吗?我的意思是要从(示例)中输入,并在(示例)中的index.php中显示聊天。没有必要给我完整的代码,但可能只有实现这一点所需的函数,将信息从server.php发送到浏览器。我读过关于如何使用使用使用套接字的javascript库实现这一点的文章,但我想在php中了解一下。