Here is client server very simple code, seems to work with telnet but with with web client code gives blank output.
Following is the server code:-
[php]
<?
function createSocketServer($host='192.168.1.34',$port=2222 )
{
$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,1);
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);
}
?>
[/php]
Here comes the client code
[PHP]
<?php
// check if form was submitted
if($_POST['send']){
// open client connection to TCP server
if(!$fp=fsockopen('192.168.1.34',2222,$errstr,$err no,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>
[/PHP]
so can any one check for any fault in the code, it performs better at telnet prob seems with writing and getting data from client.
Any responses will be highly apericitated