Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am doing a project related to php socket. My target is that I will show all client connected to server and show them in html file. However, if i put socket_accept() function in while loop, it won't break when no client connect. Now, I want to while loop only accept all client in 10 second and then export result? How to break socket_accept in php?
<pre><?php

error_reporting(E_ALL);	
/* Allow the script to hang around waiting for connections. */
set_time_limit(10);

	/* Turn on implicit output flushing so we see what we're getting
	* as it comes in. */
	ob_implicit_flush();
	
	$address = "103.1.239.148";
	$port = 10000;
	// Maximum device
	$maxdevice = 5;
	
	// Create Socket
	$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	if ($sock === false) {
		echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
	}
	
	if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) { 
		echo socket_strerror(socket_last_error($sock)); 
		exit; 
	}
	
	// Bind Socket to port
	if (socket_bind($sock, $address, $port) === false) {
		echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
	}
	
	// Start listening for connection
	if (socket_listen($sock, $maxdevice) === false) {
		echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
	}
	
	// Handling connection from client
	$i=0; // Initial count variable 
	while (true)
	{
		$msgsock[++$i] = socket_accept($sock); // msgsock is a client connect to webserver
		if ($msgsock[$i] === false) {
			echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
			break;
		}
		$senddata="L";
		socket_write($msgsock[$i], $senddata, strlen($senddata)) or die("Couldn't write to client");
		$receive=socket_read($msgsock[$i],2048,PHP_BINARY_READ) or die ("Couldn't read from client");
		if ($receive !="")
		{
			include ("list.html");
		}
		if ($i==$maxdevice)
			break;
		socket_close($msgsock[$i]);  // Close connect of client
	} 
	socket_close($sock);  // Close socket of server
	?>


What I have tried:

I have tried set_time_limit() but it doesn't work fine.
Posted
Updated 10-Apr-17 21:37pm

Wrong approach. If you stop accepting for 10 seconds, clients may run into time outs.

Think about what you want.

With your actual code there may be also only one client connected at the same time.
So "show all client connected to server" makes no sense.

Because your communication is very short and then closed, the list would contain a single client for a very short time. If you really want this then add the client to the list when accept() returns and remove it when closing the socket.

If you want to track a list of connections, create a list and add clients when they connect and are not already listed. This may be optionally limited to a "last seen" timeout. Then store also the connect time, remove clients from list after some time, and update list entries for clients that are already in the list.
 
Share this answer
 
Thanks, I have done it by using socket_select function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900