Problems sending and receiving data between php and perl?

Posted by Chip Gà Con on Server Fault See other posts from Server Fault or by Chip Gà Con
Published on 2011-11-21T03:12:31Z Indexed on 2011/11/22 1:55 UTC
Read the original article Hit count: 534

Filed under:
|
|

I have a problem in sending and receiving data between php and perl socket:

-Problem: +php can not send all byte data to perl socket +Perl socket can not receiving all data from php . Here code php:

function save(){ 
unset($_SESSION['info']); 
unset($_SESSION['data']); 
global $config,$ip; 
$start=$_POST['config']; 
 $fp = fsockopen($_SESSION['ip'], $config['port'], $errno, $errstr, 30); 
            if(!$fp) 
                                { 
                                   $_SESSION['info']="Not connect "; 
                                transfer("Not   connect".$ip, "index.php?com=server&act=info"); 
                                } 
                                else 
                                { 
                                $_SESSION['info']="Save config - ".$ip; 

                                 fwrite($fp,$start); 


                                transfer("Sending data to ".$ip, "index.php?com=server&act=info");    
                                } 
 }

Here code perl socket:

#!/usr/bin/perl 
use strict; 
use warnings; 
use Carp; 
use POSIX qw( setsid ); 

use IO::Socket; 

$| = 1; 

my $socket = new IO::Socket::INET ( 
                              LocalHost => '192.168.150.3', 
                              LocalPort => '5000', 
                              Proto => 'tcp', 
                              Listen => 5, 
                              Reuse => 1 
                           ); 

die "Coudn't open socket" unless $socket; 

print "\nTCPServer Waiting for client on port 5000"; 
my $client_socket = ""; 

     while ($client_socket = $socket->accept()) 
     { 


    my $recieved_data =" "; 
    my $send_data=" "; 

    my $peer_address = $client_socket->peerhost(); 
    my $peer_port = $client_socket->peerport(); 

    print "\n I got a connection from ( $peer_address , $peer_port ) "; 

             print "\n SEND( TYPE q or Q to Quit):"; 
                $client_socket->recv($recieved_data,20000); 
#while (defined($recieved_data = <$client_socket>)) { 

                if ( $recieved_data eq 'q' or $recieved_data eq 'Q' ) 
                { 
                        close $client_socket; 
                        last; 
                } 
elsif ($recieved_data eq 'start' or $recieved_data eq 'START' ) 
               { 
                    $send_data = `/etc/init.d/squid start`; 
               } 
                elsif ($recieved_data eq 'restart' or $recieved_data eq 'RESTART' ) 
               { 

                $send_data =  `/etc/init.d/squid restart`; 
               } 
              elsif ($recieved_data eq 'stop' or $recieved_data eq 'STOP' ) 
              { 
            $send_data =  `/etc/init.d/squid stop`; 
              } 
              elsif ($recieved_data eq 'hostname' or $recieved_data eq 'HOSTNAME' ) 
              { 
                    $send_data= `hostname`; 
              } 
              elsif ($recieved_data eq 'view-config' or $recieved_data eq 'VIEW-CONFIG' ) 
  { 

                 $send_data = `cat /etc/squid/squid.conf` ; 

              } 

                else 

                { 

 #           print  $recieved_data; 

                open OUTPUT_FILE, '> /root/data' or die("can not open file"); 

        print OUTPUT_FILE $recieved_data; 
 close OUTPUT_FILE 
                } 
 #} 
                if ($send_data eq 'q' or $send_data eq 'Q') 
                { 

                    $client_socket->send ($send_data); 
                    close $client_socket; 
                    last; 
                    } 
 else 
                { 
                    $client_socket->send($send_data); 
                } 

    }

© Server Fault or respective owner

Related posts about php

Related posts about perl