Will someone help me close this socket?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
laserbeamninja
Forum Newbie
Posts: 12
Joined: Mon Apr 18, 2005 3:58 pm

Will someone help me close this socket?

Post by laserbeamninja »

So I've opened a socket which basically echoes back to a given client exactly what is sent through the socket. The problem is, once this socket is opened, I don't know how to close it. Please help. Thanks.

Code: Select all

<?php 
// Set time limit to indefinite execution 
set_time_limit (0); 

// Set the ip and port we will listen on 
$address = 'ip'; 
$port = port; 
$max_clients = 10;

// Array that will hold client information 
$clients = Array(); 

// Create a TCP Stream socket 
$sock = socket_create(AF_INET, SOCK_STREAM, 0); 
// Bind the socket to an address/port 
socket_bind($sock, 0, $port) or die('Could not bind to address');
socket_listen($sock);



// Loop continuously 
while(true){
	// Setup clients listen socket for reading
	$read[0] = $sock;
	for ($i=0;$i<$max_clients;$i++){
		if ($client[$i]['sock']!=null) $read[$i + 1] = $client[$i]['sock'] ; 
	} 
	// Set up a blocking call to socket_select()
	$null = null;
	$ready = socket_select($read,$null,$null,$null); 
	/* if a new connection is being made add it to the client array */ 
	if (in_array($sock, $read)) { 
		for ($i = 0; $i < $max_clients; $i++) { 
			if ($client[$i]['sock'] == null) { 
				$client[$i]['sock'] = socket_accept($sock); 
				break; 
			} elseif ($i == $max_clients - 1) print ("too many clients");
		} 
		if (--$ready <= 0) continue; 
	} // end if in_array 
 
	// If a client is trying to write - handle it now 
	for ($i = 0; $i < $max_clients; $i++) { 
		if (in_array($client[$i]['sock'] , $read)){ 
			$input = socket_read($client[$i]['sock'] , 1024); 
			if ($input == null) { 
				// Zero length string meaning disconnected 
				unset($client[$i]); 
			} 
			$n = trim($input); 
			if ($input == 'exit') { 
				// requested disconnect 
				socket_close($client[$i]['sock']); 
			} elseif ($input) { 
				// strip white spaces and write back to user 
				$output = ereg_replace("[ \t\n\r]","",$input).chr(0);
				$this = "<go/>";
				socket_write($client[$i]['sock'],$this); 
			} 
		} else { 
			// Close the socket 
			socket_close($client[$i]['sock']); 
			unset($client[$i]); 
		}
	}
} // end while 
// Close the master sockets 
socket_close($sock);
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your code never breaks out of the while loop.
laserbeamninja
Forum Newbie
Posts: 12
Joined: Mon Apr 18, 2005 3:58 pm

Post by laserbeamninja »

Yes, I know.

It does break out however, eventually, just by the nature of the server. So it's not perpetual in the sense that it never stops. What I'm wondering is, is there a controlled way to clear a loop from the port. For the application of this project, it is essential that the socket does indeed stay open for very long periods of time, thus the perpetual loop. However, for the development of it, I need a way to clear the socket from outside the script that creates it. When I launch this script from the command line, the shell itself never recovers. It somehow becomes a task, albeit and invisible one, and one which does not use very much RAM, but that lives in the RAM and is associated with that port. The loop itself becomes the XML socket.
Post Reply