473,587 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Client server sockets script

3 New Member
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 createSocketSer ver($host='192. 168.1.34',$port =2222)
{

$max_clients = 10;
$client=array() ;
if(!is_int($por t)||$port<1||$p ort>65535)
{
trigger_error(' Invalid TCP port number.',E_USER _ERROR);
}
set_time_limit( 0);
// create low level socket
if(!$socket=soc ket_create(AF_I NET,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_E RROR);
}
// begin listening connections
if(!socket_list en($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($sock et, $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($clie nt[$i]['socket'] , $read))
{
$input = socket_read($cl ient[$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($c lient[$i]['socket']);
}
else if($input)
{
// strip white spaces and write back to user
$output = ereg_replace("[ \t\n\r]","",$input).ch r(0);
socket_write($c lient[$i]['socket'],$output);
}
}
else
{
// Close the socket

if ($client[$i]['socket'] != null)
{
socket_close($c lient[$i]['socket']);
unset($client[$i]);
}
}
}
}
socket_close($s ocket);
}
?>
[/php]


Here comes the client code

[PHP]
<?php
// check if form was submitted
if($_POST['send']){
// open client connection to TCP server
if(!$fp=fsockop en('192.168.1.3 4',2222,$errstr ,$errno,30)){
trigger_error(' Error opening socket',E_USER_ ERROR);
}
$message=$_POST['message'];
// write message to socket server
fputs($fp,$mess age);
// get server response
$ret=fgets($fp, 102400);
// close socket connection
fclose($fp);
echo '<strong>serve r 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.dt d">
<html>
<head>
<title>TESTIN G 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 2169
ak1dnar
1,584 Recognized Expert Top Contributor
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 Recognized Expert Expert
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 New Member
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
4419
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 strings' instead of 'one liners' I check the streams for terminators. I'm having problems with the connection, I've been looking and debugging for 2...
4
335
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 have to be open for this to work? Any help, hints or advice is appreciated :-) Kindest Regards.
15
4464
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 computations, the only data transfered is user mouse/kbd input. It works synchronously, but somehow, when I play in client window, both client and ...
5
1516
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? Thanks, --j
0
3842
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 get the correct return data from the device. Sometimes I get about 100 bytes and other times I get 3000 bytes. I even put a thread sleep but that...
0
1216
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 following scheme: a user starts the game by running "start.cgi" in his browser. Firstly, this program returns a location to the flash client....
0
2400
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 socket work, i'm posting this. Server Side script: #!/usr/bin/perl -w # serIO.pl
3
1595
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 or my "external" IP provided by my ISP the tcplistener in the Server class wont accept the connection. Can I do this in another way and make it...
5
2797
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 is really a valid one, for that every client will send some command to server for validation. But the thing is if the client is invalid server will...
0
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8349
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.