perl and communications


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl and communications
# 1  
Old 05-20-2011
perl and communications

Any idea on how to set up a perl script on the client end to listen and respond to commands on a set port?

Ive tried this before in the past and thinking about trying it again. Just looking for a starting point.
# 2  
Old 05-20-2011
Check out the POE module.
# 3  
Old 05-21-2011
On my understanding of a client / server communication, it's the server side that is listening to a port.

Here are some very simple example easy to find with google.

The server side script
Code:

#!/usr/bin/perl -w
# server.pl
#--------------------

use strict;
use Socket;

# use port 7890 as default
my $port = shift || 7890;
my $proto = getprotobyname('tcp');

# create a socket, make it reusable
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) 
   or die "Can't open socket $!\n";
setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1) 
   or die "Can't set socket option to SO_REUSEADDR $!\n";

# bind to a port, then listen
bind( SOCKET, pack( 'Sn4x8', AF_INET, $port, "\0\0\0\0" ))
       or die "Can't bind to port $port! \n";
listen(SOCKET, 5) or die "listen: $!";
print "SERVER started on port $port\n";

# accepting a connection
my $client_addr;
while ($client_addr = accept(NET_SOCKET, SOCKET)) {
    # send them a message, close connection
    print NEW_SOCKET "Smile from the server";
    close NEW_SOCKET;
}

Client side script
Code:
#!/usr/bin/perl -w
# client.pl
#----------------

use strict;
use Socket;

# initialize host and port
my $host = shift || 'localhost';
my $port = shift || 7890;
my $server = "10.12.12.168";

# create the socket, connect to the port
socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2])
   or die "Can't create a socket $!\n";
connect( SOCKET, pack( 'Sn4x8', AF_INET, $port, $server ))
       or die "Can't connect to port $port! \n";

my $line;
while ($line = <SOCKET>) {
    print "$line\n";
}
close SOCKET or die "close: $!";

Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

In UNIX which following type of file is used for network communications

a) FIFO B) SOCKET C) DIRECTORY D) BLOCK PLEASE SUGGEST ME THE ANSWER Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules. Please review the rules, which you agreed to when you... (0 Replies)
Discussion started by: raghugowda
0 Replies

2. AIX

VIO and LPAR disk/fcs communications

:wall::wall::wall: 1. I have created an LPAR in the HMC. 2. I have allocated the storage from an Hitachi AMS2500 and assigned it to the host group. 3. I have zoned the LPAR and Storage on a Brocade 5100. (The zone sees the AMS) Next I activated the LPAR in the HMC, SMS mode for the mksysb... (3 Replies)
Discussion started by: Dallasguy7
3 Replies

3. Shell Programming and Scripting

Logging/Reading Interprocess Communications

Greetings, I'm posting this in the shell scripting forum because I'm hoping this can be done in BASH or PERL. If not, I'm still open to suggestions of other ways to do it: I've got an iPhone app that's sending some encrypted (SSL) traffic to a server and I'd like to be able to read the... (0 Replies)
Discussion started by: FiZiX
0 Replies

4. UNIX for Advanced & Expert Users

linux communications modes

what r the ways in which a linux application communicate with an external system like linux or windows? (1 Reply)
Discussion started by: shil
1 Replies
Login or Register to Ask a Question