473,396 Members | 2,139 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,396 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]
<?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]

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]

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
0 1706

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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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...
0
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...

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.