PHP Socket Programming Tutorial

Advertisements

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

2 Responses

  1. cool says:

    I want to apply same on browser .. please help

  2. Femina F says:

    <?php
    error_reporting(E_ALL);
    set_time_limit(0);
    echo "TCP/IP Connection\n”;

    $port = 1935;
    $ip = “127.0.0.1”;

    /*
    +——————————-
    * @socketconectionprocess
    +——————————-
    * @socket_create
    * @socket_connect
    * @socket_write
    * @socket_read
    * @socket_close
    +——————————–
    */

    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if ($socket < 0) {
    echo "socket_create() failed. reason: " . socket_strerror($socket) . "\n";
    }else {
    echo "OK.\n";
    }

    echo "Try to connect '$ip' Port '$port'…\n";
    $result = socket_connect($socket, $ip, $port);
    if ($result < 0) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
    }else {
    echo "Connect OK\n";
    }

    $in = "Testing\r\n";
    $out = '';

    if(!socket_write($socket, $in, strlen($in))) {
    echo "socket_write() failed. reason: " . socket_strerror($socket) . "\n";
    }else {
    echo "Send Message to Server Successfully!\n";
    echo "Send Information:$in “;
    }

    while($out = socket_read($socket, 8192)) {
    echo “Receive Server Return Message Successfully!\n”;
    echo “Received Message:”,$out;
    }

    echo “Turn Off Socket…\n”;
    socket_close($socket);
    echo “Turn Off OK\n”;
    ?>

Leave a Reply

Your email address will not be published. Required fields are marked *