#!/usr/bin/perl #tcpclient.pl use strict; use IO::Socket::INET; use Digest::MD5 qw(md5 md5_hex md5_base64); # flush after every write $| = 1; my ($socket,$client_socket,$status,$data,@info,$file,$md_self,$x,$i,$a1,@a1,@a2); # creating object interface of IO::Socket::INET modules which internally creates # socket, binds and connects to the TCP server running on the specific port. $socket = new IO::Socket::INET ( PeerHost => '127.0.0.1', PeerPort => '5000', Proto => 'tcp', ) or die "ERROR in Socket Creation : $!\n"; print "TCP Connection Success.\n"; # read the socket data sent by server. $data = <$socket>; # we can also read from socket through recv() in IO::Socket::INET # $socket->recv($data,1024); print "Received from Server : ",$data; chomp($data); if($data eq 1) { $file=createRandom(); $md_self= md5_hex($file); open(FL,'>disk1.txt'); print FL $file; close FL; open(FL,'disk1.txt'); $_=; close FL; if(md5_hex($_) eq $md_self) { $status="$file $md_self"; #send the file address and its MD5: } sleep(20); } else { chomp($data); $x=0; @a1=split(" {}",$data); for $i(0..($#a1)) { @a2=(); @a2=split(' ',$a1[$i]); if(md5_hex($a2[0]) eq $a2[1]) { $x++; } } if ($x eq ($#a1+1)) { $file=createRandom(); $md_self= md5_hex($file); open(FL,'>disk1.txt'); print FL $file; close FL; open(FL,'disk1.txt'); $_=; close FL; if(md5_hex($_) eq $md_self) { $status=$file.' '.$md_self; #send the file address and its MD5. } } else { $status="Could'nt verfiy"; } } # write on the socket to server. print $socket "$status\n" ; # we can also send the data through IO::Socket::INET module, # $socket->send($data); #sleep (10); $socket->close(); #### #To create random characters #### sub createRandom { my ($randChars, $number, $output, $new, $i); # create string in loop (each char is a loop iteration) for ($i = 0; $i < 5; $i++) { # get a random number for each digit $number = int(rand(36)); # convert to a number or a letter depending on value if ($number < 10) { # number -> ASCII number $output = $number + 48; } else { # letter -> ASCII letter $output = $number + 87; } # make the ASCII number a character and append to string $new = chr($output); $randChars = "$randChars$new"; } return ($randChars); }