The Client with Socket : Client « Network « Perl

Home
Perl
1.Array
2.CGI
3.Class
4.Data Type
5.Database
6.File
7.GUI
8.Hash
9.Language Basics
10.Network
11.Regular Expression
12.Report
13.Statement
14.String
15.Subroutine
16.System Functions
17.Win32
18.XML
Perl » Network » Client 




The Client with Socket
    


#!/usr/local/bin/perl -Tw
require 5.6.0;
use Socket;
use FileHandle;
use strict;
my($remote, $port, @thataddr, $that,$them, $proto,@now,$hertime);

print "client\n";
$remote = shift || 'localhost' ;
$port = 99999 ;  # timeserver is at this port number
@thataddr=gethostbyname($remote);

$that = pack('Sna4x8', AF_INET, $port, $thataddr[4]);

$proto = getprotobyname('tcp');
if socket(SOCK, PF_INET, SOCK_STREAM, $proto ) ){
     print "Socket ok.\n";
}
else 
     die $!; 
}
if (connect(SOCK, $that)) {
     print "Connect ok.\n";
}
else {
     die $!;
}
SOCK->autoflush;
$hertime = <SOCK>;
close(SOCK);
print "Server machine time is: $hertime\n";
@now = localtime($hertime);
print "\tTime-$now[2]:$now[1] ","Date-",$now[4]+1,"/$now[3]/$now[5]\n";

   
    
    
    
  














Related examples in the same category
1.Time client
2.Two way client
3.The Client Program
4.A Daytime Client
5.Read the first line from a remote server
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.