[ Perl ] Perl 创建 Socket 实现与 Skill 的通信

https://www.cnblogs.com/yeungchie/

Perl

服务端

  • ycSocketServer.pl
#!/bin/env perl
#-----------------------------------
#  Program   :  ycSocketServer.pl  *
#  Language  :  Perl               *
#  Author    :  YEUNGCHIE          *
#  Version   :  2021.02.27         *
#-----------------------------------
use v5.10;
use warnings;
use strict;
use Getopt::Long;
use IO::Socket;

GetOptions (
    'm|mode:s' => \my $mode, # UNIX or INET
    'f|file:s' => \my $file,
    'h|host:s' => \my $host,
    'p|port:s' => \my $port,
    #
);
$mode = "UNIX"                  unless defined $mode;
$file = "$ENV{PWD}/.stream.skt" unless defined $file;
$host = "127.0.0.1"             unless defined $host;
$port = "99797"                 unless defined $port;

&server;

sub server {
    my $socket;
    given($mode){
        when("UNIX"){
            unlink $file if ( -S $file );
            $socket = IO::Socket::UNIX->new(
                Type   => SOCK_STREAM(),
                Local  => $file,
                Listen => 1,
                Reuse  => 1,
            );
        }
        when("INET"){
            $socket = IO::Socket::INET->new(
                LocalHost => $host,
                LocalPort => $port,
                Proto     => "tcp",
                Listen    => 1,
                Reuse     => 1,
            );
        }
    }# given
    die "Could not create socket: $!\n" unless $socket;
    
    #
    my $data = $socket->accept();
    while( <$data> ){ print; }
    close $socket;
    unlink $file if $mode eq "UNIX";
}# server

客户端

  • ycServerClient.pl
#!/bin/env perl
#-----------------------------------
#  Program   :  ycSocketClient.pl  *
#  Language  :  Perl               *
#  Author    :  YEUNGCHIE          *
#  Version   :  2021.02.27         *
#-----------------------------------
use v5.10;
use warnings;
use strict;
use Getopt::Long;
use IO::Socket;

GetOptions (
    'm|mode:s'       => \my $mode, # UNIX or INET
    'f|file:s'       => \my $file,
    'h|host:s'       => \my $host,
    'p|port:s'       => \my $port,
    'i|input|data:s' => \my $data,
);
$mode = "UNIX"                  unless defined $mode;
$file = "$ENV{PWD}/.stream.skt" unless defined $file;
$host = "127.0.0.1"             unless defined $host;
$port = "99797"                 unless defined $port;

&client;

sub client {
    my $socket;
    given($mode){
        when("UNIX"){
            #
            $socket = IO::Socket::UNIX->new(
                Type => SOCK_STREAM(),
                Peer => $file,
                #
                #
            );
        }
        when("INET"){
            $socket = IO::Socket::INET->new(
                PeerHost => $host,
                PeerPort => $port,
                Proto    => "tcp",
                #
                #
            );
        }
    }# given
    die "Could not create socket: $!\n" unless $socket;
    
    $data = shift @ARGV || <STDIN> unless defined $data;
    chomp $data;
    $data = "nil" if $data eq "";
    print $socket "$data";
    close $socket;
}# client

Skill

调用 Socket

  • ycStartSocket.il
; 写得不太好,重写中。。
posted @ 2021-02-20 20:41  YEUNGCHIE  阅读(432)  评论(1编辑  收藏  举报