zoukankan      html  css  js  c++  java
  • [ 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: $!
    " 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: $!
    " 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
    ; 写得不太好,重写中。。
    
  • 相关阅读:
    二叉搜索树
    自己课题的任务
    查找
    排序
    python快速教程
    塌下心来,慢慢积累~
    生命只有一次,你可以用它来做一点伟大的事情
    Python 编程规范-----转载
    Some regret....
    Discussing the scenery in the program of 863 with Doctor Zhang!
  • 原文地址:https://www.cnblogs.com/yeungchie/p/14423224.html
Copyright © 2011-2022 走看看