swoole 简单聊天室

服务端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
 * Created by PhpStorm.
 * User: LeoKim
 * Date: 2017/5/12
 * Time: 23:05
 */
 
class Server{
    private $serv;
    private $test;
 
    public function __construct()
    {
        $this->serv = new swoole_server("0.0.0.0", 9502);
        $this->serv->set(
            array(
                'worker_num' => 1,
            )
        );
 
        $this->serv->on('Start'array($this'onStart'));
        $this->serv->on('Connect'array($this'onConnect'));
        $this->serv->on('Receive'array($this'onReceive'));
        $this->serv->on('Close'array($this'onClose'));
 
        $this->serv->start();
    }
 
    public function onStart( $serv ) {
        echo "Start\n";
    }
 
    public function onConnect( $serv$fd$from_id ) {
        echo "Client {$fd} connect\n";
    }
 
    public function onClose( $serv$fd$from_id ) {
        echo "Client {$fd} close connection\n";
    }
 
    public function onReceive( swoole_server $serv$fd$from_id$data){
        echo "Get Message From Client {$fd}:{$data}\n";
        foreach($serv->connections as $client){
            if($fd != $client)
            $serv->send($client$data);
        }
    }
}
 
$server new Server();

客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
/**
 * Created by PhpStorm.
 * User: LeoKim
 * Date: 2017/5/12
 * Time: 23:13
 */
 
$socket = stream_socket_client("tcp://127.0.0.1:9502"$errno$errstr, 30);
 
function OnRead()
{
    global $socket;
    $buffer = stream_socket_recvfrom($socket, 1024);
    if(!$buffer)
    {
        echo "Server clised\n";
    }
    echo "\nRECV: {$buffer}\n";
    fwrite(STDOUT, "Enter Msg:");
}
 
function onWrite()
{
    global $socket;
    echo "on Write\n";
}
 
function onInput()
{
    global $socket;
    $msg = trim(fgets(STDIN));
 
    if($msg == 'exit'){
        swoole_event_exit();
        exit();
    }
 
    swoole_event_write($socket$msg);
    fwrite(STDOUT, "Enter Msg:");
}
 
swoole_event_add($socket'onRead''onWrite');
 
swoole_event_add(STDIN, 'onInput');
 
fwrite(STDOUT, "Enter Msg:");

image.png

image.png

image.png

vsftpd设置被动模式

完整配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
listen=yes
listen_port=21
max_clients=100
max_per_ip=10
local_max_rate=5120000
anonymous_enable=no
local_enable=yes
write_enable=no
chroot_local_user=yes
chroot_list_enable=yes
chroot_list_file=/etc/vsftpd/chroot_list
guest_enable=yes
guest_username=kingsoft
virtual_use_local_privs=yes
user_config_dir=/etc/vsftpd/user_config
pasv_enable=yes
pasv_min_port=4500
pasv_max_port=5000
tcp_wrappers=yes
xferlog_enable=yes
xferlog_file=/var/log/ftp/vsftpd.log
idle_session_timeout=600
data_connection_timeout=120
accept_timeout=60
connect_timeout=60
connect_from_port_20=no
local_umask=022
pam_service_name=vsftpd.vu
pasv_address=本机ip
pasv_addr_resolve=yes

主动模式:

1
2
3
Port_enable=YES               开启主动模式
Connect_from_port_20=YES      当主动模式开启的时候 是否启用默认的20端口监听
Ftp_date_port=%portnumber%    上一选项使用NO参数是 指定数据传输端口

被动模式

1
2
3
4
5
6
7
8
9
被动模式
PASV_enable=YES   开启被动模式
PASV_min_port=%number% 被动模式最低端口
PASV_max_port=%number% 被动模式最高端口
 
iptables中开放这段端口
service iptables start 打开防火墙
iptables -I INPUT  -p tcp  --dport 10020:10040  -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 21 -j ACCEPT

在被动模式,服务器做了NAT,例如云主机,这时候我们用特定的IP访问机器,其实还转了一层。FTP客户端访问机器可能会没响应。具体情况为登录成功,但是list目录和文件的时候卡住。

1
2
vsftpd   22411   nobody    0u  IPv4  68905      0t0  TCP 10.140.41.65:ftp->10.10.10.98:43380 (ESTABLISHED)
vsftpd   22411   nobody    1u  IPv4  68905      0t0  TCP 10.140.41.65:ftp->10.10.10.98:43380 (ESTABLISHED)

这时候可以看到机器的真正IP。

1
2
pasv_address=本机ip【就是我们能访问的外网IP】
pasv_addr_resolve=yes

这样ftp客户端就可以解析IP,访问成功