Hey there, i'm using this code:
I'm making a socket server, yes.
msock.class (ignore uneven indents):
<?php
class msock
{
var $socket;
var $accept;
var $readarray;
function create()
{
$this->socket = socket_create(AF_INET, SOCK_STREAM, 0);
if($this->socket != false)
{
return true;
}
}
function setreuseable()
{
$ret = socket_setopt($this->socket,SOL_SOCKET,SO_REUSEADDR,1);
return $ret;
}
function bind($address, $port)
{
$ret = socket_bind ($this->socket, $address, $port);
return $ret;
}
function listen()
{
$ret = socket_listen ($this->socket, 0);
return $ret;
}
function connect($address, $port)
{
if(!socket_connect($this->socket, $address, $port))
{
return 0;
}
else
{
return 1;
}
}
function accept()
{
$this->accept = socket_accept($this->socket);
return $this->accept;
}
function getaddr()
{
socket_getpeername($this->accept, $inet);
return $inet;
}
function write($msg)
{
socket_write($this->accept, $msg, strlen($msg));
}
function nwrite($msg)
{
socket_write($this->socket, $msg, strlen($msg));
}
function destroy()
{
socket_close($this->accept);
}
function read()
{
$readbuffer = socket_read($this->accept, 2048, PHP_NORMAL_READ);
return $readbuffer;
}
function set_socket_array()
{
$this->readarray = array($this->socket);
}
function msock_select()
{
$returnbuffer = socket_select($this->readarray, $a=NULL, $b=NULL, 1, 0);
return $returnbuffer;
}
}
?>
main.php:
<?php
// ##### Requires
require("class/msock.class");
// ##### End Requires
// Set time limit to indefinite execution
set_time_limit (0);
// Set the ip and port we will listen on
$zone_listen_address = '192.168.0.5';
$zone_listen_port = 9000;
$zonesock = new msock;
if(!$zonesock->create())
die('Could not create socket');
if(!$zonesock->setreuseable())
die('Could not set socket options');
if(!$zonesock->bind($zone_listen_address, $zone_listen_port))
die('Could not bind socket to port');
if(!$zonesock->listen())
die('Could not listen');
if(!$zonesock->accept())
die('Could not accept');
/* Prepare the read array */
$zonesock->set_socket_array();
while(1) {
echo "\n\ndamn we got a long way so far..\n\n";
$num_changed_sockets = $zonesock->msock_select();
echo "num_changed_sockets is: " .$num_changed_sockets. "|\n\n";
echo "\nlast socket error was: " .socket_strerror(socket_last_error()). "\n\n";
if ($num_changed_sockets === false) {
/* Error handling */
echo "\n\nwhat?? no changed sockets?\n\n";
} else if ($num_changed_sockets > 0) {
echo "\n\nHouston...?\n\n";
$incoming = $zonesock->read();
$incoming = trim($incoming);
echo "\n\n\n\nincoming: " . $incoming;
/* At least at one of the sockets something interesting happened */
}
}
$zonesock->destroy();
?>
What that SHOULD do is wait for a connection, accept it, then echo out whatever was sent.
However, it just loops like:
damn we got a long way so far..
num_changed_sockets is: 0|
last socket error was: Undefined error: 0
...and repeat forever...
the zend code: http://www.zend.com/zend/tut/tutorial-staub3.php gives a "only variables can be passed by reference" error.