關於網路那些事...

網路行銷,SEO,網路趨勢,教學文章,網頁設計,生活時事

PHP Socket - simple case tutorial

Open PHP Socket Extension

First, make sure php.ini php_socket extension is opened
remove ; and restart http server
php.ini

...
extension=php_sockets.dll
...

Socket Server Side

Writing your servier side code as fellowing:
php_socket_server.php

<?php
header('content-type:text/html;charset=utf-8');
set_time_limit(0);

$ip = '127.0.0.1';

$port = 8085;

$recive_times = 0;//0 is no limited

//socket_create
if(($sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) < 0) {
        echo "socket_create() Fails Reason is:".socket_strerror($sock)."\n";
}
//socket_bind
if(($ret = socket_bind($sock,$ip,$port)) < 0) {
        echo "socket_bind()  Fails Reason is:".socket_strerror($ret)."\n";
}
//socket_listen
if(($ret = socket_listen($sock,4)) < 0) {
        echo "socket_listen()  Fails Reason is:".socket_strerror($ret)."\n";
}

//socket_accept
$count = 0;

do {
        if (($msgsock = socket_accept($sock)) < 0) {
                echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
                break;
        } else {
                //Transfer msg to client side
                $msg ="Hello! I am server side";
                // sleep(2);
                //socket_write
                socket_write($msgsock, $msg, strlen($msg));
                echo "Test Success\n";
                //socket_read
                $buf = socket_read($msgsock,8192);
                $talkback = "Recive Message from client:$buf\n";
                echo $talkback;
                if(++$count >= $recive_times && $recive_times!=0){
                    break;
                };
        }
        //socket_close
        socket_close($msgsock);
} while (true);
socket_close($sock);
?>
?>

Socket Client Side

Writing your Client side code as fellowing
php_socket_client.php

<?php
//TCP/IP Connection;

header('content-type:text/html;charset=utf-8');
error_reporting(E_ALL);
set_time_limit(0);

$port = 8085;

$ip = "127.0.0.1";

$in = 'Hello!I am client';//input, transfer msg to server side

$out = '';//output, recive service msg

//socket_create
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if ($socket < 0) {
    echo "---Failed: socket_create() failed! Reason: " . socket_strerror($socket) . "<br>";
}

//socket_connect
$result = socket_connect($socket, $ip, $port);

if ($result < 0) {
    echo "---Failed: socket_connect() failed! Reason: " . socket_strerror($result) . "<br>";
}

//socket_write
if(!socket_write($socket, $in, strlen($in))) {
    echo "---Failed: socket_write() failed! Reason: " . socket_strerror($socket) . "\n";
}

while($out = socket_read($socket, 8192)) {

    echo "<h1>Recive Success! Get Msg:".$out."</h1>";

}

//Close
socket_close($socket);
?>

Server Initializes

php/bin/php  htdocs/php_socket_server.php

or

/.../htdocs/
$php php_socket_server.php

client request

php/bin/php  htdocs/php_socket_client.php

or you can test client side from browsers

Reference:
http://php.net/manual/en/book.sockets.php
http://inspiregate.com/programming/php/127-php-socket-teaching.html


如果你喜歡我們的文章內容,請在這裡按個讚



最新文章推薦