By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
461,994 Members | 494 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 461,994 IT Pros & Developers. It's quick & easy.

Client-server script in perl using IO::sockets

P: 1
Hi,

This client-server script does bi-directional communication.
Setup i used: The server script on windows 2003 server and the client on a linux machine.
Since i faced issues in getting the socket work, i'm posting this.

Server Side script:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. # serIO.pl
  3. # server using IO::Socket
  4. #---------------------
  5. use strict;
  6. use IO::Socket;
  7.  
  8. my $port = 7890;
  9.  
  10. my $sock = new IO::Socket::INET(
  11. #    LocalHost    => 'localhost',
  12.     LocalPort    => $port,
  13.     Proto        => 'tcp',
  14.     Listen        => SOMAXCONN,
  15.     Reuse        => 1
  16. ) or die "no socket :$!";
  17.  
  18. my ($new_sock, $c_addr, $buf);
  19.  
  20. print "Server started on port :$port\n";
  21.  
  22. $sock->autoflush(1);
  23. while (($new_sock, $c_addr) = $sock->accept()) {
  24.     print "Socket connection accepted.\n";
  25.  
  26.     my ($client_port, $c_ip) = sockaddr_in($c_addr);
  27.     my $client_ipnum = inet_ntoa($c_ip);
  28.     my $client_host =gethostbyaddr($c_ip, AF_INET);
  29.     print "got a connection from: $client_host"," [$client_ipnum] ";
  30.     print "\n";
  31.  
  32.     print $new_sock "Welcome .....\n";
  33.  
  34.     my $pid = fork();
  35.     print "Cannot fork : $!\n" unless (defined($pid));
  36.  
  37.     if ($pid == 0) {
  38.         while (defined ($buf = <$new_sock>)) {
  39.             print $buf;
  40.             print "\n";
  41.             select(STDOUT); $| = 1;
  42.             my $output = `$buf`;
  43.             chomp($output);
  44.             $output = $output . "END_OF_CMD";
  45.             print "OUTPUT ---> $output\n";
  46.  
  47.             print $new_sock "$output\n";
  48.             $new_sock->flush();
  49.         }
  50.  
  51.         exit(0);
  52.     }
  53. }
  54.  

Client side script:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. # cliIO.pl
  3. # a simple client using IO:Socket
  4. #----------------
  5.  
  6. use strict;
  7. use IO::Socket;
  8.  
  9. my $host = shift || 'localhost';
  10. my $port = shift || 7890;
  11. my $sock = new IO::Socket::INET(
  12.     PeerAddr    => $host,
  13.     PeerPort    => $port,
  14.     Proto        => 'tcp',
  15. ) or die "no socket :$!";
  16.  
  17. $sock->autoflush(1);
  18.  
  19. my $ack = <$sock>;
  20. print "Server says: $ack\n";
  21.  
  22. print $sock "dir\n";
  23. fn();
  24. print $sock "help\n";
  25. fn();
  26. .
  27. .
  28. .
  29.  
  30. close $sock;
  31.  
  32. sub fn {
  33.     my $end_request = 1;
  34.     while($end_request) {
  35.         my $out = <$sock>;
  36.         chomp($out);
  37.         if ($out =~ /END_OF_CMD/) {
  38.             $out =~ s/END_OF_CMD//;
  39.             print "OUTPUT ----> $out\n";
  40.             $end_request = 0;
  41.  
  42.         } else {
  43.             print "OUTPUT ----> $out\n";
  44.         }
  45.     }
  46. }
  47.  
Thanks.
Jun 1 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.