PHP也做socket编程

server.php

<?php



    $addr = "127.0.0.1";

    $port = 1000;



    $remoteIP = "";

    $remotePort = "";



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

    if($socket < 0) {

    echo "Socket create:".$socket_strerror($socket)."\n";

    exit;

    }

    if ( ($ret = socket_bind($socket, $addr, $port)) < 0 ) {

    echo "socket bind:".socket_strerror()."\n";

    exit;

    }

    if ( ($ret = socket_listen($socket, 5)) < 0 ) {

    echo "socket listen:".socket_strerror()."\n";

    exit;

    }



    socket_set_nonblock($socket);



    echo "Wainting fro a connection:\n";



    $clients = array($socket);

    while(true) {

    $read = $clients;

    if(socket_select($read, $writes=NULL, $execs=NULL, 3) < 1){

    //echo ".";

    continue;

    }

    if(in_array($socket,$read)) {

    $clients[] = $newsock = socket_accept($socket);

    socket_set_nonblock($newsock);

    socket_getpeername($newsock,&$remoteIP,&$remotePort);

    $key = array_search($socket,$read);

    unset($read[$key]);

    $msg = "Server: $addr\r\n".

    "Notice:Welcome the user from ".$remoteIP.":".$remotePort.".\r\n";

    echo $remoteIP.":".$remotePort." conected in.\r\n";

    socket_write($newsock,$msg);

    }



    foreach($read as $read_sock) {

    $data = @socket_read($read_sock,1024,PHP_BINARY_READ);

    if($data == false) {

    $key = array_search($read_sock,$clients);

    print_r($clients[$key]);

    unset($clients[$key]);

    socket_close($read_sock);

    echo "Client disconnected.\n";

    continue;

    }



    $command = strtoupper ($data);



    switch ($command)

    {

    case "LIST":

    $writer = print_r($clients,1);

    break;

    case "HELLO":

    $writer = "Hello Everybody!";

    break;

    case "QUIT":

    $writer = "Bye-Bye";

    break;

    case "SLEEP":

    sleep(30);

    $writer = "sleeped 10 seconds!";

    break;

    case "HELP":

    $writer = "HELLO\tQUIT\tHELP";

    break;

    default:

    $writer = "Error Command!\r\nType help for help message!";

    }



    socket_write($read_sock, $writer);

    //exit;

    }

    }

    socket_close($socket);



    ?>

client.php

<?php

    // Client

    // 设置错误处理

    error_reporting (E_ALL);

    // 设置处理时间

    set_time_limit (0);



    $ip = "127.0.0.1"; // IP 地址

    $port = 1000; // 端口号



    $socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); // 创建一个SOCKET

    if ($socket)

    echo "socket_create() successed!\n";

    else

    echo "socket_create() failed:".socket_strerror ($socket)."\n";



    $conn = socket_connect ($socket, $ip, $port); // 建立SOCKET的连接

    if ($conn)

    echo "Success to connection![".$ip.":".$port."]\n";

    else

    echo "socket_connect() failed:".socket_strerror ($conn)."\n";



    echo socket_read ($socket, 1024);



    $stdin = fopen ('php://stdin', 'r');

    while (true)

    {

    $command = trim (fgets ($stdin, 1024));

    socket_write ($socket, $command, strlen ($command));

    $msg = trim (socket_read ($socket, 1024));

    echo $msg."\n";

    if ($msg == "Bye-Bye")

    break;

    }

    fclose ($stdin);

    socket_close ($socket);

    ?>

上一篇: PHP XML转数组函数   下一篇: 对日开发中PG,PL,SE,PM都是什么意思

提交疑问

回顶部