bluntboy31 has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to send shell command down the udp socket I have created between my client and server, have server execute the command and send back the result? Below is my client code:
! /usr/bin/perl -w # client 129.82.12.187(planetlab-1.cs.colostate.edu) use strict; use Socket; #port assignments use constant UDP_PORT => 5001; use constant REMOTE_HOST => '127.0.0.1';#this should be ip of server use constant MAX_RECV_LEN => 65536; #local variable declarations my $transport_service = getprotobyname('udp');# gets port assignment f +or the OS usually 17 my $remote_host = gethostbyname(REMOTE_HOST); my $remote_port = UDP_PORT; my $destination = sockaddr_in( $remote_port, $remote_host); #binding or creating the socket socket(UDP_SOCK, PF_INET, SOCK_DGRAM, $transport_service); $cmd = 'qx(ls)' print "$destination ,$cmd \n"; # = qx(ls); #send( UDP_SOCK, $cmd, 0, $destination ); #print "Uptime is: $info\n"; <code> server side: #! /usr/bin/perl -w # server 129.82.12.188(planetlab-2.cs.colostate.edu) use strict; use Socket; #port assignments use constant UDP_PORT => 5001; use constant MAX_RECV_LEN => 65535;#packet length in bytes use constant LOCAL_INETNAME => '127.0.0.1';#HOST SERVER RUNS ON #local variable declarations my $transport_service = getprotobyname('udp'); #my $local_host = gethostbyname(LOCAL_INETNAME); my $local_port = shift || UDP_PORT; my $local_address = sockaddr_in( $local_port, INADDR_ANY );#use list s +ince perl can be ued for other languages socket( UDP_SOCK, PF_INET, SOCK_DGRAM, $transport_service ); bind( UDP_SOCK, $local_address );#this creates the actual socket my $client_data;#this will contain what is sent to the server from cli +ent #this is the nested loop to wait for and save data from remote client while( 1 ) { my $from_who = recv( UDP_SOCK, $client_data, MAX_RECV_LEN, 0 ); if ( $from_who ) { my ( $the_port, $the_ip ) = sockaddr_in( $from_who ); my $remote_name = gethostbyaddr( $the_ip, AF_INET ) || inet_nt +oa( $the_ip ); warn "Received from $remote_name: ", length( $client_data ), ' -> ', substr( $client_data, 0, 39 ), "\n"; system($client_data); sleep(3); warn "Sending back to client ... \n"; send( UDP_SOCK, $client_data, 0, $from_who ) or warn "udp_s5: send to socket failed.\n"; } else { warn "Problem with recv: $!\n";# this indicates not getting da +ta from client } } # write the data contents to output file
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: socket programming
by VinsWorldcom (Prior) on Mar 06, 2013 at 11:49 UTC | |
Re: socket programming
by aitap (Curate) on Mar 06, 2013 at 20:03 UTC | |
Re: socket programming
by kielstirling (Scribe) on Mar 06, 2013 at 01:46 UTC | |
by bluntboy31 (Initiate) on Mar 06, 2013 at 02:10 UTC | |
by BrowserUk (Patriarch) on Mar 06, 2013 at 02:13 UTC | |
Re: socket programming
by kielstirling (Scribe) on Mar 06, 2013 at 03:27 UTC |
Back to
Seekers of Perl Wisdom