Jump to content

[SOLVED] Repeating socket server


aximbigfan

Recommended Posts

Hi,

 

I have a small socket server I wrote in PHP. Unfortunately it isn't working as I want it to.

 

What I want it to do:

1. Receive data from the client

2. process the data

3. send it back

4. Reset for next client

 

#2, #3 and #4 work great! But #1 doesn't. The server doesn't even give the client a chance to send the data. What am I doing wrong?

 

Source:

<?php
//SocketServer
//A PHP app to RX/TX data via sockets
//Written by Chris S
$ver = 0;

//Options
$opt_address     = "192.168.15.111";
$opt_port        =  1337 ;
$opt_maxcients   =  10 ;

//Set time out to infinite
set_time_limit(0);

//Custom encoding function
function safe_encode($mode, $input)
{
if ($mode == "TX")
	return urlencode(json_encode($input));
else if ($mode == "RX")
	return json_decode(urldecode($input));
else
	return false;
}

//Check options and report
echo "Checking options... ";
if ($opt_address == NULL || $opt_port == NULL)
die("Error!\n");
else
echo "OK\n";

//Create socket and report
echo "Creating Socket... ";
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
if (!$socket)
die("Error!\n");
else
echo "OK\n";

//Bind the socket and report
echo "Binding socket... ";
if (!socket_bind($socket, $opt_address, $opt_port))
die("Error!\n");
else
echo "OK; Bound to port $opt_port@$opt_address\n";

//Start listening for connections and report
echo "Listening for connections... ";
if (!socket_listen($socket))
die("Error!\n");
else
echo "OK\n";

while (true)
{
//Accept new connection
$spawn = socket_accept($socket);
if ($spawn)
	echo "New connection accepted!\n";
else
	echo "Error accepting new connection.\n";

//Read sent data and report
echo "Reading sent data... ";
$input = socket_read($spawn, 1024);
if (!$input)
	echo "Error!\n";
else
	echo "OK\n";
//Decode input
$input = safe_encode("RX", $input);

/////////////////////////
$ouput = base64_encode($input);

//Send data to client and report
echo "Sending data to client... ";
//safe_encode data
$output = safe_encode("TX", $output);
$write_result = socket_write($spawn, $output, strlen($output));
	if (!$write_result)
	echo "Error!\n";
else
	echo "OK\n";

//Close connections
socket_close($spawn);
//socket_close($socket);
}

?>

 

Thanks!

 

Chris

Link to comment
Share on other sites

It works on my end too.  PHP5, ubuntu 7.05

 

mbeals@LAMPserver:~$ telnet 192.168.3.198 1337
Trying 192.168.3.198...
Connected to 192.168.3.198.
Escape character is '^]'.
test
null
Connection closed by foreign host.

 

mbeals@LAMPserver:/home/webroot/www$ sudo php -q socketTest.php
Checking options... OK
Creating Socket... OK
Binding socket... OK; Bound to port 1337@192.168.3.198
Listening for connections... OK
New connection accepted!
Reading sent data... OK
Sending data to client... OK

 

you might also want to look into forking the process when a new connection is made.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.