Socket Server

From: Date: Thu, 30 Aug 2001 11:22:01 +0000
Subject: Socket Server
Groups: php.general 
Request: Send a blank email to php-general+get-65250@lists.php.net to get a copy of this message
Dear all, This is my quandry... I am trying to build a socket server to transfer simple information, more of a monitoring tool. But after about 14+ hours it just kicks out, without rhyme or reason. No errors, no nothing... I was curious if anyone could see big gaping holes in my code that would allow for this, it's being run on Linux via the command: /web/sockets/socketserver 2 > /web/sockets/logs/error.log & Same for the client piece.. and the code in each is as follows, the client is: #!/scs/pkg/bin/php <? set_time_limit (0); error_reporting (E_ALL); echo "TCP/IP Connection @ "; echo exec('date'); echo "\n"; $address = "mymain.server.net"; $port = 4001; do { exec('/usr/bin/uptime > /tmp/uptime'); exec('/bin/date >> /tmp/uptime'); exec('/usr/bin/top -bn1 > /tmp/proc'); exec('df -k > /tmp/dfk'); $sock = @socket_create(AF_INET,SOCK_STREAM,0) or die("Can't create socket.\n"); echo "Socket created, [$sock].\n"; socket_setopt($sock, SOL_SOCKET, SO_REUSEADDR, 1); echo "Attempting to connect to '$address' on port '$port'..."; $result = @socket_connect($sock,$address, $port) or die("connect() failed.\n Reason: " . socket_strerror($result) . "\n"); echo "Connection established.\n"; $in = file('/tmp/uptime'); $inb = file('/tmp/proc'); $inc = file('/tmp/dfk'); $out = ''; $clear = "new\n"; $ret = @socket_read($sock, 4096, PHP_NORMAL_READ); socket_write ($sock, $clear, strlen($clear)); sleep(5); if($ret == "Ready\n") { echo "Sending file...\n "; $num = 0; $numb = 0; $numc = 0; for($num; $num != count($in); $num++) { socket_write($sock, $in[$num], strlen($in[$num])); usleep(50000); } echo "OK...\t"; for($numc; $numc != count($inc); $numc++) { socket_write($sock, $inc[$numc], strlen($inc[$numc])); usleep(50000); } echo "OK...\n"; } else { echo "Sleeping for Retry\n"; socket_write ($sock, "quit\n", 5); @socket_close ($sock); sleep(10); continue; } echo "\nClosing socket... "; socket_write ($sock, "quit\n", 5); echo "\nOK. Socket Closed @ "; echo exec('date'); echo "\n\n"; @socket_close ($sock); sleep(70); $ret = ""; exec('rm -rf /tmp/uptime /tmp/dfk'); # /tmp/proc'); } while (true); ?> And the main server code is as follows: #!/scs/pkg/bin/php <? set_time_limit (0); $address = "mymain.server.net"; $port = 4001; $sock = @socket_create(AF_INET,SOCK_STREAM,0) or die("Can't create socket.\n"); echo "Socket created, [$sock].\n"; socket_setopt($sock, SOL_SOCKET, SO_REUSEADDR, 1); if(!@socket_bind($sock,$address,$port)) { @socket_close($sock); die("Can't bind to port $port, exiting.\n"); } echo "Socket binding successful on $address:$port @ "; echo exec('date'); echo "\n"; if (@socket_listen($sock,10) < 0) { @socket_close($sock); die("Can't setup socket for listening.\n Reason: " . socket_strerror($sock) . "\n"); } echo "Socket is now listening for connection requests...\n\n"; do { if(!$msgsock = @socket_accept($sock)) { echo "Can't accept connection.\n Reason: " . socket_strerror($msgsock) . "\n"; break; } @socket_getpeername($msgsock,$remote_host,$remote_port); echo "Accepted socket connection for $remote_host:$remote_port [$msgsock]\n"; do { @socket_write($msgsock,"Ready\n",6); if(!$ret = socket_read($msgsock,4096)) { echo "Error reading output, possible broken connection."; break; } if ($temp) { $ret .= $temp; $mylength = strlen($ret); $offset = strpos($ret, "\n"); if ($offset === false) { $temp = $ret; continue; } else { # String Slice $cmd = substr($ret, 0, $offset); if ($cmd != $ret) { $temp = substr($ret, $offset+1); } else { $temp = ""; } } } elseif ($ret == "\n") { $cmd .= $ret . "\n"; } else { $ret .= $temp; $mylength = strlen($ret); $offset = strpos($ret, "\n"); if ($offset === false) { $temp = $ret; continue; } else { # String Slice $cmd = substr($ret, 0, $offset); if ($cmd != $ret) { $temp = substr($ret, $offset+1); } else { $temp = ""; } } } $ret = $cmd; if (!$ret) { echo "Socket read error. \n Reason: " . socket_strerror($ret) . "\n"; break; } elseif (strlen(chop($ret)) > 0) { $talkback = "PHP: You said \"" . chop($ret) . "\"\r\n"; $response = @socket_write($msgsock,$talkback,strlen($talkback)); echo "$remote_host:$remote_port said: " . chop($ret) . "\r\n"; if(chop($ret) != "quit") { if(chop($ret) == "new") { exec("rm -rf /web/downloads/$remote_host/uptime"); $file = "new"; } elseif (chop($ret) == "proc") { exec("rm -rf /web/downloads/$remote_host/proc"); $file = "proc"; } elseif (chop($ret) == "dfk") { exec("rm -rf /web/downloads/$remote_host/dfk"); $file = "dfk"; } if ($file == "new" && chop($ret) != "new") { $file1 = fopen("/web/downloads/$remote_host/uptime","a+"); fputs($file1, "$ret \n"); fclose($file1); } elseif ($file == "proc" && chop($ret) != "proc") { $file2 = fopen("/web/downloads/$remote_host/proc","a+"); fputs($file2, "$ret \n"); fclose($file2); } elseif ($file == "dfk" && chop($ret) != "dfk") { $file3 = fopen("/web/downloads/$remote_host/dfk","a+"); fputs($file3, "$ret \n"); fclose($file3); } } } if (chop($ret) == "quit") { break; } } while (true); @socket_close($msgsock); echo "Closed socket connection, [$msgsock] @ "; echo exec('date'); echo "\n"; } while (true); @socket_close($sock); echo "Closed server socket.\n"; ?> Get 250 color business cards for FREE! http://businesscards.lycos.com/vp/fastpath/

« previous php.general (#65250) next »