PHP+Socket聊天室(telnet命令调试)

2 篇文章 0 订阅
class socket
{
    private $_socket;
    private $_domain = AF_INET;
    private $_type = SOCK_STREAM;
    private $_protocol = SOL_TCP;
    private $_clients = [];
    private $_max_clients = 10;

    public function __construct()
    {
        if (!extension_loaded('sockets')) {
            die('the sockets extension is not loaded!');
        }
        if (($this->_socket = $this->create($this->_domain, $this->_type, $this->_protocol)) < 0) {
            $this->error($this->_socket);
        }
    }

    public function start($ip, $port)
    {
        if (($res = $this->set_option()) < 0) {
            $this->error($res);
        }
        if (($res = $this->bind($ip, $port)) < 0) {
            $this->error($res);
        }
        if (($res = $this->listen(5)) < 0) {
            $this->error($res);
        }
        #使用非阻塞模式
        socket_set_nonblock($this->_socket);
        echo "listen on port " . $port . "..." . PHP_EOL;
        $clients = array($this->_socket);
        while (true) {
            $this->_clients = $clients;
            $write = [];
            $except = [];
            if (socket_select($this->_clients, $write, $except, 0) < 1) {
                continue;
            }

            if (in_array($this->_socket, $this->_clients)) {
                $clients[] = $connection = $this->connect();
//                if (($clients + 1) > $this->_max_clients) {
//                    echo "Too many clients..." . PHP_EOL;
//                } else {
                    $this->send($connection, "welcome!\nthere are " . (count($clients) - 1) . " client here\n");
                    socket_getpeername($connection, $client_ip);
                    echo "new client connected:$client_ip\n";
                    $key = array_search($connection, $this->_clients);
                    unset($this->_clients[$key]);
//                }
            }

            foreach($this->_clients as $client){
                $receive = $this->receive($client);
                if($receive===false){
                    $key=array_search($client, $clients);
                    unset($clients[$key]);
                    echo "client disconnected.\n";
                    continue;
                }
                $receive = trim($receive);
                if(!empty($receive)){
                    foreach($clients as $send_socket){
                        if($send_socket==$this->_socket||$send_socket==$client){
                            continue;
                        }
                        $this->send($send_socket, $this->_socket .">".$client.">"."$receive\n");
                    }
                }
            }
        }
        $this->close();
    }

    public function bind($ip, $port)
    {
        socket_bind($this->_socket, $ip, $port);
    }

    public function listen($max_clients)
    {
        socket_listen($this->_socket, $max_clients);
    }

    public function connect()
    {
        return socket_accept($this->_socket);
    }

    public function send($connection, $msg)
    {
        socket_write($connection, $msg);
    }

    public function receive($client)
    {
        return socket_read($client, 819292, PHP_NORMAL_READ);
    }

    public function error($resource)
    {
        echo socket_strerror($resource) . PHP_EOL;
    }

    public function close()
    {
        socket_close($this->_socket);
    }

    public function create($domain = AF_INET, $type = SOCK_STREAM, $protocol = SOL_TCP)
    {
        return socket_create($domain, $type, $protocol);
    }

    public function set_option(){
        socket_set_option($this->_socket,SOL_SOCKET,SO_REUSEADDR,1);
    }
}


demo

ini_set("display_errors", 1);
include_once INC_PATH . "class/socket.class.php";
$socket = new socket();
$socket->start("192.168.2.113", 1371);


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值