Hi all!
I have previously written some server side code to first take some request from a user , process it and give the output to the user. It was running fine from one client but when other person from other place have accessed it, it stoped working. Then on research I came to know that you have to actually enable a code to give access to multiple users. For that I have written a client server script which performs bit better from telnet , but when i try to use it from my client it just starts waiting.
If some one can check the code for me it will be great.
SErver function is as follows.
function createSocketServer($host='192.168.1.34',$port=9508)
{
$max_clients = 10;
$client=array();
if(!is_int($port)||$port<1||$port>65535)
{
trigger_error('Invalid TCP port number.',E_USER_ERROR);
}
set_time_limit(0);
// create low level socket
if(!$socket=socket_create(AF_INET,SOCK_STREAM,0))
{
trigger_error('Error creating new socket.',E_USER_ERROR);
}
// bind socket to TCP port
if(!socket_bind($socket,$host,$port))
{
trigger_error('Error binding socket to TCP port.',E_USER_ERROR);
}
// begin listening connections
if(!socket_listen($socket))
{
trigger_error('Error listening socket connections.',E_USER_ERROR);
}
while(true)
{
$read[0] = $socket;
for ($i = 0; $i < $max_clients; $i++)
{
if ($client[$i]['socket'] != null)
{
$read[$i + 1] = $client[$i]['socket'] ;
}
}
// Set up a blocking call to socket_select()
$ready = socket_select($read,$a=null,$b=null,$c=null);
if (in_array($socket, $read))
{
for ($i = 0; $i < $max_clients; $i++)
{
if ($client[$i]['socket'] == null)
{
$client[$i]['socket'] = socket_accept($socket);
break;
}
else if($i == $max_clients - 1)
print ("too many clients");
}
if (--$ready <= 0)
continue;
} // end if in_array//NA Ends
// If a client is trying to write - handle it now
for ($i = 0; $i < $max_clients; $i++) // for each client
{
if (in_array($client[$i]['socket'] , $read))
{
$input = socket_read($client[$i]['socket'] , 1024);
if ($input == null)
{
// Zero length string meaning disconnected
unset($client[$i]);
}
$n = trim($input);
//if ($input == 'exit')
if ($n == 'exit')
{
// requested disconnect
socket_close($client[$i]['socket']);
}
else if($input)
{
// strip white spaces and write back to user
$output = ereg_replace("[ \t\n\r]","",$input).chr(0);
socket_write($client[$i]['socket'],$output);
}
}
else
{
// Close the socket
if ($client[$i]['socket'] != null)
{
socket_close($client[$i]['socket']);
unset($client[$i]);
}
}
}
}
socket_close($socket);
}
And client is as under
<?php
// check if form was submitted
if($_POST['send']){
// open client connection to TCP server
if(!$fp=fsockopen('192.168.1.34',9508,$errstr,$errno,30)){
trigger_error('Error opening socket',E_USER_ERROR);
}
$message=$_POST['message'];
// write message to socket server
fputs($fp,$message);
// get server response
$ret=fgets($fp,102400);
// close socket connection
fclose($fp);
echo '<strong>server output is following:- <br>'.$ret.'</strong>';
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>TESTING TCP SOCKET SERVER</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-
8859-1" />
</head>
<body>
<h1>Enter the domain name</h1>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type="text" name="message" size="30" /><br />
<input type="submit" name="send" value="Check UP" />
</form>
</body>
</html>
For now server have to just get an input "string" and post it back to client but not working fine... can any one help