Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to socket programming by bluntboy31

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-19 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found