Client server program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karthikeyanck
    New Member
    • Oct 2007
    • 23

    Client server program

    I 've to write a client server program for my assignment at college......

    The server listens at some port in one machine and the client is running on some other machine......

    I 'll 've to execute a command from the client so that the command in executed on the server. Please guide me. A small hint for the start is appreciated.
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    this is all the help I am willing to give you:

    Comment

    • rohitbasu77
      New Member
      • Feb 2008
      • 89

      #3
      The bellow program has got two parts:
      A server and a client
      The server send message to the client and the client print that message.

      Server:

      Code:
      #!/usr/bin/perl
      use strict; 
      use Socket; 
      
      # use port 5700 as default 
      my $port = shift || 5700; 
      my $proto = getprotobyname('tcp'); 
      
       # create a socket, make it reusable 
       socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; 
       setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!"; 
      
       # grab a port on this machine 
       my $paddr = sockaddr_in($port, INADDR_ANY); 
      
      # bind to a port, then listen 
      bind(SERVER, $paddr) or die "bind: $!"; 
      listen(SERVER, SOMAXCONN) or die "listen: $!"; 
      print "SERVER started on port $port "; 
      
      # accepting a connection 
      my $client_addr; 
      while ($client_addr = accept(CLIENT, SERVER)) 
      { 
      # find out who connected 
      my ($client_port, $client_ip) = sockaddr_in($client_addr); 
      my $client_ipnum = inet_ntoa($client_ip); 
      my $client_host = gethostbyaddr($client_ip, AF_INET); 
       # print who has connected 
       print " \n got a connection from: $client_host","[$client_ipnum] \n "; 
       # send them a message, close connection 
      print CLIENT " \n Smile from the server \n"; 
      close CLIENT; 
      }

      Client:

      Code:
      #!/usr/bin/perl
      
      use strict; 
      use Socket; 
      
      # initialize host and port 
      my $host = shift || 'localhost'; 
      my $port = shift || 5700; 
      
      my $proto = getprotobyname('tcp'); 
      
      # get the port address 
      my $iaddr = inet_aton($host); 
      my $paddr = sockaddr_in($port, $iaddr); 
       # create the socket, connect to the port 
       socket(SOCKET, PF_INET, SOCK_STREAM, $proto) 
      a. or die "socket: $!"; 
      connect(SOCKET, $paddr) or die "connect: $!"; 
      
      my $line; 
      while ($line = <SOCKET> ) 
      { 
      print $line; 
      
      } 
      close SOCKET or die "close: $!";
      Last edited by eWish; Jun 26 '10, 07:01 PM. Reason: Added Code Tags

      Comment

      Working...