473,395 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Client-server script in perl using IO::sockets

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
0 2391

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

Similar topics

4
by: Patrick Useldinger | last post by:
Has anybody got such an example? My server works fine, but my client does not :-( -- Real e-mail address is 'cHVAdm8ubHU=\n'.decode('base64') Visit my Homepage at http://www.homepages.lu/pu/
5
by: Fossie | last post by:
Hi folks, I am unable to solve the following problem, which should not be new, but I cannot find an appropriate solution anywhere: One python process ("daemon") waits for messages. Another...
2
by: roni | last post by:
hi. i want to build USER CONTROL that will be used in WEB PAGE. to build user control and use in web page,I KNOW . i did user control with textbox and lable for example and it works. the...
1
by: osaleh | last post by:
I am trying to execute a Socket.Select() statement on an arraylist of sockets. The problem is that I can only go up to 64 sockets at a time. I know that I have to manupilate the FD_SetSize to...
4
by: Cory Nemelka | last post by:
Can anyone tell my what is wrong with this snippet of code? //Creates a UdpClient for reading incoming data. UdpClient receivingUdpClient = new UdpClient(); //Creates an IPEndPoint to record...
3
by: Lloyd | last post by:
I have a question as to when remoting should be used as opposed to net.sockets.
1
by: Dave | last post by:
I'm have an app the opens a socket and I want the socket to stay open till the user clicks a button. I pass a string of "m_start" to start a listener, and "m_end" to the function listen_func to...
4
by: Sueffel | last post by:
Boy, someday I may get good at this stuff! Until then, I have a "TCP server" that accepts multiple connections. That all works wonderfully! YEAH!!!! I have a TCPClient that is the client...
3
by: Terry Olsen | last post by:
I'm wondering if a winsock class has been written that functions similar to the winsock.ocx from VB6.
4
by: keithseah | last post by:
Hi all, i've been having this problem and its kiiling me! i'm a newbie at this so i hope someone would be able to help me. picture link:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.