g3n1u5
g3n1u5

Reputation: 47

PHP Sockets loading information into browser

Hello guys I am trying to load some information into the browser. I have this code:

<?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");
    }

?>

On the server side, but I dont know how I can made it visible in the browser. Can you help me? I am trying to make a simple chat system just for the sake of understandint the basics and I hope to translate into some more complex data manupulation for games, and live statistics and so on...

Regards!

Upvotes: 0

Views: 1944

Answers (1)

Guillermo
Guillermo

Reputation: 791

You can use JavaScript and WebSockets to send requests to your PHP server program and receive the responses. This is a good tutorial using PHP and WebSockets for a chat.

Upvotes: 2

Related Questions