Bonjour,

Mes sockets clients arrive a ce connecter avec le socket serveur mais je rencontre un problème dans la fonction response(),
lorsque j'écris pour chaque sockets client, tous mes clients ce déconnecte alors qu'il doivent rester actifs et les sockets doivent se déconnecter seulement si il ne communique plus avec le serveur ou qu'il sont "timeout". j'ai essayer de debug la variable $res comme output cela me retourne un boolean false.

Merci d'avoir pris le temps de lire .

Merci, MrWalid.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
 
<?php
 
require __DIR__ . "/utils/Color.php";
require __DIR__ . "/utils/Logger.php";
 
class Server
{
 
    public $id = [];
    public $all = [];
 
    public $mastersock;
 
    public $n = 0;
 
    public $close = false;
    public $timeout = 0;
 
 
    public function __construct()
    {
        set_time_limit(0);
    }
 
    /**
     * @return array
     */
    public function getClients(): array
    {
        return $this->id;
    }
 
 
    /**
     * @param array $id
     */
    public function addClient(array $id)
    {
        $this->id[] = $id;
    }
 
    /**
     * @param int $id
     */
    public function disconnectClient(int $id)
    {
        if (isset($this->getClients()[$id])) {
            $socket = $this->getClients()[$id];
            socket_close($socket);
            $i = array_search($socket, $this->all);
            unset($this->all[$i]);
            unset($this->id[$id]);
            --$this->n;
        }
    }
 
    /**
     * @return array
     */
    public function getAll(): array
    {
        return $this->all;
    }
 
 
    /**
     * @param $socket
     */
    public function setAll($socket): void
    {
        $this->all[] = $socket;
    }
 
 
    /*
     * You need to run this function first for launch the Server and for communating with the clients.
     */
    public function connect()
    {
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
        socket_bind($socket, '127.0.0.1', 7000);
        socket_listen($socket, 5);
        socket_set_nonblock($socket);
 
 
        if ($socket) {
            Logger::info(Logger::getLocale() . Color::COLOR_GREEN . "You are now connected on Server.");
            Logger::updateTitle(count($this->getClients()));
            $this->setAll($socket);
            $this->insertClient($socket);
        } else {
            Logger::info(Logger::getLocale() . Color::COLOR_RED . "Impossible to connect to the Server.");
            socket_close($socket);
            socket_shutdown($socket);
        }
    }
 
 
    /**
     * @param $socket
     */
    public function insertClient($socket)
    {
        while (true) {
            usleep(500); //500 milliseconds.
 
            $r = $w = $e = $this->all;
            $result = socket_select($r, $w, $e, 0);
 
            if ($result > 0) {
                foreach ($r as $sock) {
                    if ($sock == $socket) {
                        $this->setAll($newsock = socket_accept($socket)); //add a client on the socket_list.
                        if ($socket) {
                            ++$this->n;
                            if (!isset($this->getClients()[$this->n])) {
                                socket_set_nonblock($newsock);
                                socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
                                $this->id[$this->n] = $newsock;
                                Logger::info(Logger::getLocale() . Color::COLOR_YELLOW . "Connected: Client: #" . $this->n);
                                Logger::info(Logger::getLocale() . Color::COLOR_GREEN . count($this->getClients()) . " Clients Online(s)");
                                Logger::updateTitle(count($this->getClients()));
 
                            }
                        }
                    } else {
                        $response = $this->response();
                        if (!empty($response)) { //waiting for a response from a client and communating information with other clients.
                            foreach ($response as $id => $buff) {
                                $this->sendToAll($buff);//send buff to all clients.
                            }
                        }
                    }
                }
            }
        }
    }
 
    /**
     * @param $data
     */
    public function sendToAll($data)
    {
        foreach ($this->getClients() as $id => $client) {
            @socket_write($client, $data, strlen($data));
        }
    }
 
    public function response()
    {
        $read = [];
        foreach ($this->getClients() as $id => $client) {
            socket_set_nonblock($client);
            $res = @socket_read($client, 1024);
            if (!$res) { //An empty or false bool information count as a disconnected client
                Logger::info(Logger::getLocale() . Color::COLOR_RED . "Disconnected: Client: #" . $id . " timeout.");
                $this->disconnectClient($id);
                Logger::info(Logger::getLocale() . Color::COLOR_GREEN . count($this->getClients()) . " Clients Online(s)");
                Logger::updateTitle(count($this->getClients()));
                continue;
            }
 
            if ($res) {
                Logger::info(Logger::getLocale() . Color::COLOR_GREEN . $res); //Server will receive information from the clients
            }
            $read[$id] = $res;
        }
        return $read;
    }
}
 
$server = new Server();
$server->connect();