Commit 2e88523c by PotatoGim

perl 시리얼 통신 예제 추가

parent 5d4e94b0
#!/usr/bin/env perl
#=============================================================================
# FILE: tty_client.pl
# USAGE: ./tty_client.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# DATE: 2015년 01월 07일 10시 04분 10초
# VERSION: 1.0
# LICENSE: Artistic License
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
my $domain = 2; # Internet domain
my $type = 1; # Sequenced, reliable, two-way connection, byte streams
my $proto = 6; # Transmission Control Protocol (TCP)
socket (my $sock, $domain, $type, $proto) || die "socket: $!";
my $host = pack('C4', 10, 0, 0, 100); # localhost = 127.0.0.1
my $port = 1024;
my $addr = pack('S n a4 x8', $domain, $port, $host);
bind ($sock, $addr) || die "bind: $!";
printf "Client %s:%s\n", join('.', unpack('C4', $host)), $port;
$host = pack('C4', 10, 0, 0, 101); # localhost = 127.0.0.1
$port = 8888;
$addr = pack('S n a4 x8', $domain, $port, $host);
connect ($sock, $addr) || die "connect: $!";
printf "Server %s:%s\n", join('.', unpack('C4', $host)), $port;
select($sock);
$| = 1;
select(STDOUT);
while (my $m = <$sock>) {
print STDOUT $m;
$m = <STDIN>;
print $sock $m;
}
close ($sock);
1;
__END__
=encoding utf8
=head1 NAME
tty_client.pl -
=head1 SYNOPSIS
=head1 DESCRIPTION
=over
=item B<Item 1>
=item B<Item 2>
=item B<Item 3>
=back
=head1 AUTHOR
PotatoGim <potatogim@potatogim.net>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
=head1 SEE ALSO
=cut
#!/usr/bin/env perl
#=============================================================================
# FILE: tty_server.pl
# USAGE: ./tty_server.pl
# OPTIONS:
# BRIEF:
# AUTHOR: PotatoGim <potatogim@potatogim.net>
# DATE: 2015년 01월 07일 10시 09분 37초
# VERSION: 1.0
# LICENSE: Artistic License
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
my $domain = 2; # Internet domain
my $type = 1; # Sequenced, reliable, two-way connection, byte streams
my $proto = 6; # Transmission Control Protocol (TCP)
socket (my $lsock, $domain, $type,$proto)) || die "socket: $!";
my $host = pack('C4', 0, 0, 0, 0); # Local wildcard host id: 0.0.0.0
my $port = 8888;
my $addr = pack('S n a4 x8', $domain, $port, $host);
bind ($lsock, $addr) or die "bind: $!";
my $qsize = 5; # Queue up to 5 connections
listen ($lsock, $qsize) or die "listen: $!";
printf "Server host %s:%s\n", join('.', unpack('C4', $host)), $port;
$addr = accept(my $sock, $lsock);
($domain, $port, $host) = unpack('S n a4 x8', $addr);
print STDOUT "Client %s:%s", join('.', unpack('C4', $host)), $port;
select ($sock);
$| = 1;
select (STDOUT);
print $sock "Welcome to Reverse Echo Server.\r\n";
while (my $m = <$sock>) {
$m =~ s/\n|\r//g;
last if ($m eq ".");
$m = reverse($m);
print $sock "$m\r\n";
}
close ($sock);
1;
__END__
=encoding utf8
=head1 NAME
tty_server.pl -
=head1 SYNOPSIS
=head1 DESCRIPTION
=over
=item B<Item 1>
=item B<Item 2>
=item B<Item 3>
=back
=head1 AUTHOR
PotatoGim <potatogim@potatogim.net>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
=head1 SEE ALSO
=cut
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment