Blogger templates

Monday, 8 April 2013

How to create a tcp socket using perl


How to create a tcp socket using perl

The following program can be used to create a tcp connection to a host on a specified port.
The program attempts connection at a periodic interval, which is passed as 'execution_interval'.

#!/usr/bin/perl
use IO::Socket;
use POSIX qw/strftime/;
if (@ARGV != 4)
{
  usage();
  exit;             
}
my $hostname = $ARGV[0];
my $port = $ARGV[1];
my $timeout = $ARGV[2];
my $interval = $ARGV[3];

my $logdir = "/tmp/";
my $datestamp = strftime('%m-%d-%y_%H:%M:%S',localtime);
$mday = (localtime(time))[3];

my $logfile = $logdir."$hostname\_$port\_tcp_socket\_$datestamp.log";
open LOG, ">>","$logfile" or die $!;
LOG->autoflush(1);
while(1) {

    my $sock = new IO::Socket::INET (PeerAddr => $hostname,
                                     PeerPort => $port,
                                     Proto => 'tcp',
                                     Timeout => $timeout);
    $cur_datestamp = strftime('%m-%d-%y_%H:%M:%S',localtime);
    $cur_mday = (localtime(time))[3];  
    if(!$sock) {
        print LOG "\n$cur_datestamp : Error - Could not create socket to $hostname on $port: $!";
        sleep $interval;
        next;      
    }  
    if($cur_mday != $mday) {
        close(LOG);
        $logfile = $logdir."$hostname\_$port\_tcp_socket\_$datestamp.log";
        open LOG, ">>","$logfile" or die $!;
        LOG->autoflush(1);
        $mday = $cur_mday;
    }  
  
    print LOG "\n$cur_datestamp : Socket created to $hostname on $port";  
    close($sock);
    sleep $interval;
}

close(LOG);

sub usage
{
  print "Invalid parameters \nUsage: ./tcp_socket.pl \n";
}

No comments:

Post a Comment

 

Blogroll

Hello Friends myself Sahaj Ohri live at Jaipur city , This Website is mainly for Youth, You can explore a youth network here on www.techowner.blogspot.in As it is generally an information sharing platform when you can get information and also share information. If you are having some information.I am 19 years old guy from India who have interest in making blogs.I am a simple guy who likes to spent his time with Friends,watching movies,and reading various types of books. I loves to sharemy personal view with those people who have interest inmaking website, blog etc.Here I also give you some technical knowledge about making website attractive and some other technical support(knowledge) using WordPress as your CMS.Dear Friends, As an author, I’m trying my best to improve this platform day by day. If you are having any idea or suggestion in your mind about this website then you are free to contact me and share your thought. Contact : For Issues regarding advertisement, copyright issues or ideas sharing, you can contact us on the following details. sahaj.ohri3@gmail.com

About

Hello Friends myself Sahaj Ohri live at Jaipur city , This Website is mainly for Youth, You can explore a youth network here on www.techowner.blogspot.in As it is generally an information sharing platform when you can get information and also share information. If you are having some information.I am 19 years old guy from India who have interest in making blogs.I am a simple guy who likes to spent his time with Friends,watching movies,and reading various types of books. I loves to sharemy personal view with those people who have interest inmaking website, blog etc.Here I also give you some technical knowledge about making website attractive and some other technical support(knowledge) using WordPress as your CMS.Dear Friends, As an author, I’m trying my best to improve this platform day by day. If you are having any idea or suggestion in your mind about this website then you are free to contact me and share your thought. Contact : For Issues regarding advertisement, copyright issues or ideas sharing, you can contact us on the following details. sahaj.ohri3@gmail.com