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

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);

时间: 2024-10-12 19:10:35

PHP+Socket聊天室(telnet命令调试)的相关文章

Web Socket 聊天室

Web sockets test Web Socket 聊天室 按下连接按钮,会通过WebSocket发起一个到聊天浏览器的连接. 服务器地址: 用户名: 连接 发送 来自网上.............

python socket 聊天室

import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #绑定端口 s.bind(("127.0.0.1", 8888)) while True: data = s.recvfrom(1024) print(str(data[0].decode("gbk"))) send_data = input("请输入聊天内容") if "exit" in se

socket聊天室

1 #服务端 2 from socket import * 3 import json 4 def recvMsg(s): 5 while True: 6 #接收用户的信息 7 data,address = s.recvfrom(1024) 8 data = json.loads(data) 9 print(data,address) 10 11 if data['type'] == 'enter': 12 # 将用户进入聊天室的信息发给其它所有在线用户 13 sendToAll(('>>系统

【Unity3D自学记录】Unity3D网络之Socket聊天室初探

首先创建一个服务端程序,这个程序就用VS的控制台程序做即可了. 代码例如以下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; namespace SocketServer { class Program { const int Port = 20000; //设置连接port static void Main(strin

socket聊天室(服务端)(多线程)(TCP)

#include<string.h> #include<signal.h> #include<stdio.h> #include<sys/socket.h> #include<stdlib.h> #include<netdb.h> #include<pthread.h> #include<memory.h> #include<semaphore.h> int Thread_num=0,count=0

Socket聊天室-TcpListener,TcpClient

参考自:http://blog.csdn.net/liguo9860/article/details/6148614 服务端: 1 #region 属性 2 3 //请求的客户端连接 4 Socket clientsocket; 5 //当前连接集合 6 List<Client> clients; 7 //请求客户端线程 8 Thread clientservice; 9 //服务器监听线程 10 Thread threadListen; 11 //服务器监听连接 12 TcpListener

socket 聊天室实现

server #include <stdio.h> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #includ

socket 聊天室

服务端: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; using System.Thre

Swift - 使用socket进行通信(附聊天室样例)

在Swift开发中,如果我们需要保持客服端和服务器的长连接进行双向的数据通信,使用socket是一种很好的解决方案. 下面通过一个聊天室的样例来演示socket通信,这里我们使用了一个封装好的socket库(SwiftSocket). 功能如下: 1,程序包含服务端和客服端,这里为便于调试把服务端和客服端都做到一个应用中 2,程序启动时,自动初始化启动服务端,并在后台开启一个线程等待客服端连接 3,同时,客户端初始化完毕后会与服务端进行连接,同时也在后台开启一个线程等待接收服务端发送的消息 4,