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

Client server program

P: 23
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.
Feb 14 '08 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
this is all the help I am willing to give you:

http://perldoc.perl.org/perlipc.html
Feb 14 '08 #2

P: 89
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:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict; 
  3. use Socket; 
  4.  
  5. # use port 5700 as default 
  6. my $port = shift || 5700; 
  7. my $proto = getprotobyname('tcp'); 
  8.  
  9.  # create a socket, make it reusable 
  10.  socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; 
  11.  setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!"; 
  12.  
  13.  # grab a port on this machine 
  14.  my $paddr = sockaddr_in($port, INADDR_ANY); 
  15.  
  16. # bind to a port, then listen 
  17. bind(SERVER, $paddr) or die "bind: $!"; 
  18. listen(SERVER, SOMAXCONN) or die "listen: $!"; 
  19. print "SERVER started on port $port "; 
  20.  
  21. # accepting a connection 
  22. my $client_addr; 
  23. while ($client_addr = accept(CLIENT, SERVER)) 
  24. # find out who connected 
  25. my ($client_port, $client_ip) = sockaddr_in($client_addr); 
  26. my $client_ipnum = inet_ntoa($client_ip); 
  27. my $client_host = gethostbyaddr($client_ip, AF_INET); 
  28.  # print who has connected 
  29.  print " \n got a connection from: $client_host","[$client_ipnum] \n "; 
  30.  # send them a message, close connection 
  31. print CLIENT " \n Smile from the server \n"; 
  32. close CLIENT; 

Client:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict; 
  4. use Socket; 
  5.  
  6. # initialize host and port 
  7. my $host = shift || 'localhost'; 
  8. my $port = shift || 5700; 
  9.  
  10. my $proto = getprotobyname('tcp'); 
  11.  
  12. # get the port address 
  13. my $iaddr = inet_aton($host); 
  14. my $paddr = sockaddr_in($port, $iaddr); 
  15.  # create the socket, connect to the port 
  16.  socket(SOCKET, PF_INET, SOCK_STREAM, $proto) 
  17. a. or die "socket: $!"; 
  18. connect(SOCKET, $paddr) or die "connect: $!"; 
  19.  
  20. my $line; 
  21. while ($line = <SOCKET> ) 
  22. print $line; 
  23.  
  24. close SOCKET or die "close: $!";
Jun 26 '10 #3

Post your reply

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