By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,277 Members | 1,505 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,277 IT Pros & Developers. It's quick & easy.

Client server sockets script

P: 3
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
Sep 27 '07 #1
Share this Question
Share on Google+
3 Replies


ak1dnar
Expert 100+
P: 1,584
You've posted this thread in the php articles section, rather than the forum. Please use the php forum, for technical questions in future. Thanks!
Sep 27 '07 #2

pbmods
Expert 5K+
P: 5,821
Heya, Khu. Welcome to TSDN!

If you're getting a blank page, your script is probably generating an error. Check out this article to find out what is going on.
Sep 28 '07 #3

P: 3
Heya, Khu. Welcome to TSDN!

If you're getting a blank page, your script is probably generating an error. Check out this article to find out what is going on.

The page loads well but it did not show any data that server may have passed it back... My error reporting is on, on the development system so its definately not the reason problem is with fetching back the data... Can you or some one else can look further into it.
Sep 28 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.