zoukankan      html  css  js  c++  java
  • php Socket通信

     1 <?php
     2 error_reporting(0);
     3 $host = "0.0.0.0";
     4 $port = 1082;
     5 $maxUser = 10;
     6 
     7 set_time_limit(0);
     8 
     9 
    10 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("socket create failed: " . socket_strerror( socket_last_error() ) . "
    ");
    11 
    12 socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, true) or die("option set failed: " . socket_strerror( socket_last_error() ) . "
    ");
    13 socket_bind($socket, $host, $port) or die("socket_bind() failed: " . socket_strerror( socket_last_error() ) . "
    ");
    14 socket_listen( $socket, 3 ) or die("socket_listen() failed: " . socket_strerror( socket_last_error() ) . "
    ");
    15 
    16 echo "Starting Listen!
    ";
    17 do{
    18     if( $msgSocket = socket_accept($socket) === false ){
    19         echo "socket_accept() failed: " . socket_strerror( socket_last_error() ) . "
    ";
    20         break;
    21     }else{
    22         $msg = "hello world";
    23         socket_write($msgSocket, $msg, strlen( $msg ));
    24         
    25         echo "read client message
    ";
    26         $buf = socket_read($msgSocket, 8192);
    27         $talkback = "received message: $buf
    ";
    28         
    29         echo $talkback;
    30         
    31         if( false === socket_write($msgSocket, $talkback, strlen($talkback)) ){
    32             echo "socket_write() failed reason:" . socket_strerror(socket_last_error($sock)) ."
    ";
    33         }else{
    34             echo "send success
    ";
    35         }
    36         
    37         //socket_send($msgSocket, $buf, $len, $flags);
    38     }
    39     socket_close( $msgSocket );
    40     
    41 }while ( true );
    42 
    43 socket_close($socket);
    44 ?>
  • 相关阅读:
    ElasticSearch搜索
    Muddery框架
    https和http的区别
    multiprocessing模块创建进程
    进程理论基础
    单例模式
    原型模式
    建造者模式
    StrutsTypeConverter的使用
    UML用例图之泛化(generalization)、扩展(extend)和包含(include)关系--UML一波流系列讲解
  • 原文地址:https://www.cnblogs.com/rnckty/p/4998647.html
Copyright © 2011-2022 走看看