aximbigfan Posted August 6, 2008 Share Posted August 6, 2008 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 https://forums.phpfreaks.com/topic/118393-solved-repeating-socket-server/ Share on other sites More sharing options...
btherl Posted August 6, 2008 Share Posted August 6, 2008 It works fine for me here, using linux and "PHP 4.4.4-8+etch4 (cli) (built: Jun 30 2007 21:02:54)" What OS and php version are you using? Link to comment https://forums.phpfreaks.com/topic/118393-solved-repeating-socket-server/#findComment-609367 Share on other sites More sharing options...
aximbigfan Posted August 6, 2008 Author Share Posted August 6, 2008 Hi, I am using Debian with PHP5 CLI. Although it appears to work, it opens the connection, and spits out data without waiting for input. Try opening it with telnet, and you will see what I mean. Thanks, Chris Link to comment https://forums.phpfreaks.com/topic/118393-solved-repeating-socket-server/#findComment-609610 Share on other sites More sharing options...
mbeals Posted August 6, 2008 Share Posted August 6, 2008 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 https://forums.phpfreaks.com/topic/118393-solved-repeating-socket-server/#findComment-609650 Share on other sites More sharing options...
aximbigfan Posted August 6, 2008 Author Share Posted August 6, 2008 That's what I mean, it is sending "null", it is suppose to send the base64_encode of the string. It is not catching the input. Chris Link to comment https://forums.phpfreaks.com/topic/118393-solved-repeating-socket-server/#findComment-610252 Share on other sites More sharing options...
aximbigfan Posted August 6, 2008 Author Share Posted August 6, 2008 Odd, I commented out the line with base64 encode, and now it works!!! Chris Link to comment https://forums.phpfreaks.com/topic/118393-solved-repeating-socket-server/#findComment-610260 Share on other sites More sharing options...
btherl Posted August 7, 2008 Share Posted August 7, 2008 Oh .. take a close look at this line: $ouput = base64_encode($input); Step 1 was working fine, that's what I tested .. I did not test steps 2, 3 and 4 beacuse you said those were ok. Link to comment https://forums.phpfreaks.com/topic/118393-solved-repeating-socket-server/#findComment-610283 Share on other sites More sharing options...
aximbigfan Posted August 9, 2008 Author Share Posted August 9, 2008 output Thanks for noticing that! Chris Link to comment https://forums.phpfreaks.com/topic/118393-solved-repeating-socket-server/#findComment-612205 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.