Stanislaw T
Stanislaw T

Reputation: 420

how to count number of connections in PHP socket

I created a socket using this code:

//Create TCP/IP sream socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//reuseable port
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);

//bind socket to specified host
socket_bind($socket, $host, $port);

//listen to port
socket_listen($socket);

I need to periodically check how many connections are open on my socket in PHP. How do I do that?

Upvotes: 2

Views: 1252

Answers (1)

alzee
alzee

Reputation: 1396

I assume that somewhere in your code you are using socket_accept() in order to service the connections when they come in. In that code you can increment a counter indicating the number of current connections, and decrement it when clients disconnect.

You're using very low level methods here (the socket_* methods), and this interface does not maintain a list of connections on its own.

Alternatively you could use a higher level socket library such as Ratchet.

Upvotes: 1

Related Questions