1 Replies - 1186 Views - Last Post: 25 April 2010 - 03:32 AM Rate Topic: -----

#1 yanjchan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-March 09

PHP Socket Server "Undefined offset" and wrong resource type e

Posted 25 April 2010 - 12:26 AM


You might have seen my previous post about my PHP socket server attempt, which I resolved.

This is a continuation of the same script, but I have come upon a new error.

If I have a client leave the server, the socket server goes crazy and starts looping extremely rapidly after firing off a series of errors about attempting to close the socket.

If another client tries to join after this, then the "Undefined offset" error starts recurring again and again.

The code is pasted / commented below:

#!/usr/local/bin/php -q



$address = "";
$port = 31415;

// Create the master socket, which will accept new clients.
if (($socMaster = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false)
	echo "Failed to create socket.\n";
	echo socket_strerror(socket_last_error()) . "\n";

// Set the master socket to reuse addresses, even if the port is currently waiting for another connection to complete (?)
socket_set_option($socMaster, SOL_SOCKET,SO_REUSEADDR, 1);

// Bind the master socket to the port specified in $port, so it will manage connections through here
if (socket_bind($socMaster, $address, $port) === false)
	echo "Failed to bind to port $port.\n";
	echo socket_strerror(socket_last_error()) . "\n";

// Start listening for connections to this port
if (socket_listen($socMaster) === false)
	echo "Failed to start listening on socket.\n";
	echo socket_strerror(socket_last_error()) . "\n";

// Set this socket to be non-blocking, which means that it will not wait for new connections.

// Initialize the array of 'clients' to include the master socket
$arrClients = array(array('sock' => $socMaster));

// This loop will run as long as the script continues to run
while (true)
	// Transfer the sockets held in $arrClients to the $arrChangedSockets array
	foreach ($arrClients as $thisClient)
		$arrChangedSockets[] = $thisClient['sock'];

	// Use socket_select to 'block', or wait, until a state of a socket is changed (is readable, or has quit)
	$numChangedSockets = socket_select($arrChangedSockets, $write = NULL, $except = NULL, $tv_sec = NULL);

	// A for loop to loop through all of the clients
	for ($i = 0; $i < count($arrClients); $i++)
		// Here, we check to see if the client currently being processed is in the array of changed sockets
		// If it is, then we should read from it (or do otherwise)
		if (in_array($arrClients[$i]['sock'], $arrChangedSockets))
			// Assign the temporary variable $thisSocket to hold the socket object for the current client
			$thisSocket = $arrClients[$i]['sock'];

			// Perform special actions if $thisSocket is actually the master socket
			if ($thisSocket == $socMaster)
				// Check if there is a new client trying to connect
				$socNewClient = socket_accept($socMaster);

				// If there was an error (socket_accept returns null on error)
				if ($socNewClient === null)
					echo "Failed to accept socket.\n";
					echo socket_strerror(socket_last_error()) . "\n";
				// If there actually was a new client
				else if ($socNewClient !== false)
					// 'Push' the new client array element to the array of clients, and assign 'sock'
					// to be the socket connection of the new client
					array_push($arrClients, array('sock' => $socNewClient));
			// $thisSocket is not the socket master, so we're going to check for various things to perform
				// Receive bytes from $thisSocket (the current client socket)
				$bytReceived = socket_recv($thisSocket, $strBuffer, 2048, 0);

				// If nothing was received at all yet the state has changed, this means that the client has closed the connection
				if ($bytReceived == null)
					// Remove the client from the array
					// Close the socket connection
				// If something was received, then there was a message
					// Loop through each of the clients
					foreach ($arrClients as $thisClient)
						// As long as the client is not the master client
						if ($thisClient['sock'] != $socMaster)
							// Write the message to the socket
							socket_write($thisClient['sock'], $strBuffer, strlen($strBuffer));




Thanks in advance for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Socket Server "Undefined offset" and wrong resource type e

#2 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: PHP Socket Server "Undefined offset" and wrong resource type e

Posted 25 April 2010 - 03:32 AM

Can you please post your errors (with line numbers)?

Basically undefined offset is saying that the array index no longer exists, you can check that it does using isset(..).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1