473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Client server sockets script

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
3 2155
ak1dnar
1,584 Expert 1GB
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
5,821 Expert 4TB
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
khu84
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

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

Similar topics

4
by: Mike Dole | last post by:
I'm working on a client - server application based on the 'How to Sockets Server and How to Sockets Client' code from the Visual Basic ..NET Resource Kit. Since I want to be able to send 'big...
4
by: milkyway | last post by:
Hello everyone, I have a few values and variables that I want to post to a server (without using a SUBMIT button). Is there a way to post data from within javascript - do sockets or connections...
15
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
5
by: John | last post by:
I have a simple script that runs a server where one client can connect. I would like to make it so that many clients can connect to one server on the same port. Where can I find how to do this? ...
0
by: goroth | last post by:
I am trying to create a telnet client that will connect to several of my network devices on campus and change settings on the devices. So far I can connect with the code below, but I can't seem to...
0
by: alikim | last post by:
Hi, I'm trying to create a network game where the server part is a cgi script and the client is a flash embedded into an html page. Data exchange is performed via sockets. Now I'm using the...
0
by: muraley | last post by:
Hi, This client-server script does bi-directional communication. Setup i used: The server script on windows 2003 server and the client on a linux machine. Since i faced issues in getting the...
3
by: Joh | last post by:
I'd like to test a server application and a client application from the same computer. Currently if I let the client try to connect to my IPv4 adress provided by my router in this case 192.168.1.2...
5
by: Nash | last post by:
Hi, I am using asynchronous client/server communication. Whenever a client is getting connected to the server the correspoinding socket is added to the list. I want to validate whether the client...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.