# Create an application client to communicate with a serveur, via socket IP #!/usr/bin/perl -w use strict; use IO::Socket::INET; use bytes; my ($host, $port, $kidpid, $socket, $line, $cmdid); #unless (@ARGV == 2) # {die "usage: $0 host (ex: 47.162.12.24) port (ex: 5004)"} #($host, $port) = @ARGV; $host = "47.164.133.226"; $port = 34000; #$port = 5004; # Create a connexion tcp/ip on host and port required $socket = new IO::Socket::INET( PeerAddr => $host, PeerPort => $port, Proto => "tcp", Type=>SOCK_STREAM, Blocking=>1 ) or die "Can't connect to port $port on $host: $!"; # Don't wait for end of line $socket->autoflush(1); print "[Connected to $host: $port]\n"; # cut the app on two twi process # die "Can't fork: $!" unless defined($kidpid = fork()); # try other if (not defined ($kidpid = fork())) { die "Can't fork: $!"; } # the if block is in father process #elsif ($kidpid) while (1) { if ($kidpid) { # copy socket in std output: listen to the server while (defined ($line = <$socket>)) # if (defined ($line = <$socket>)) { print "Opera> $line"; $socket->flush(); } print "ok"; } # the else block is in kid process else { # copy std input on the socket: write to server while (defined ($line = )) # if (defined ($line = )) { if ($line eq "exit\n" or $line eq "quit\n" or $line eq "q\n") { print "End of client\n"; waitpid($kidpid, $kidpid); kill ("TERM", $kidpid); close ($socket); return; } else { #serveur test if ($port == 5004) { print $socket "$line"; } # Opera else { # header: # type = 12 (sur 1 octet) # transaction id (sur 2 octets) # longueur de la commande (sur 2 octets) # commande (1 caractere par octet) my $type = 12; $cmdid++; my $len = length($line); my @list = (); @list[0] = $type&0xFF; @list[1] = $cmdid&0xFF00; @list[2] = $cmdid&0x00FF; @list[3] = $len&0xFF00; @list[4] = $len&0x00FF; my $str = pack ("C5", @list); my $tmp = pack ("Z$len", $line); $str .=$tmp; print $socket $str; #print $socket "\n"; } $socket->flush(); } last; } } } print "End of client\n"; waitpid($kidpid, $kidpid); kill ("TERM", $kidpid); close ($socket);