亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? perlipc.pod

?? ARM上的如果你對底層感興趣
?? POD
?? 第 1 頁 / 共 4 頁
字號:

Here's the code:

    #!/usr/bin/perl -w
    use strict;
    use IO::Socket;
    my ($host, $port, $kidpid, $handle, $line);

    unless (@ARGV == 2) { die "usage: $0 host port" }
    ($host, $port) = @ARGV;

    # create a tcp connection to the specified host and port
    $handle = IO::Socket::INET->new(Proto     => "tcp",
				    PeerAddr  => $host,
				    PeerPort  => $port)
	   or die "can't connect to port $port on $host: $!";

    $handle->autoflush(1);		# so output gets there right away
    print STDERR "[Connected to $host:$port]\n";

    # split the program into two processes, identical twins
    die "can't fork: $!" unless defined($kidpid = fork());

    # the if{} block runs only in the parent process
    if ($kidpid) {
	# copy the socket to standard output
	while (defined ($line = <$handle>)) {
	    print STDOUT $line;
	}
	kill("TERM", $kidpid);  		# send SIGTERM to child
    }
    # the else{} block runs only in the child process
    else {
	# copy standard input to the socket
	while (defined ($line = <STDIN>)) {
	    print $handle $line;
	}
    }

The C<kill> function in the parent's C<if> block is there to send a
signal to our child process (current running in the C<else> block)
as soon as the remote server has closed its end of the connection.

If the remote server sends data a byte at time, and you need that
data immediately without waiting for a newline (which might not happen),
you may wish to replace the C<while> loop in the parent with the
following:

    my $byte;
    while (sysread($handle, $byte, 1) == 1) {
	print STDOUT $byte;
    }

Making a system call for each byte you want to read is not very efficient
(to put it mildly) but is the simplest to explain and works reasonably
well.

=head1 TCP Servers with IO::Socket

As always, setting up a server is little bit more involved than running a client.
The model is that the server creates a special kind of socket that
does nothing but listen on a particular port for incoming connections.
It does this by calling the C<IO::Socket::INET-E<gt>new()> method with
slightly different arguments than the client did.

=over

=item Proto

This is which protocol to use.  Like our clients, we'll
still specify C<"tcp"> here.

=item LocalPort

We specify a local
port in the C<LocalPort> argument, which we didn't do for the client.
This is service name or port number for which you want to be the
server. (Under Unix, ports under 1024 are restricted to the
superuser.)  In our sample, we'll use port 9000, but you can use
any port that's not currently in use on your system.  If you try
to use one already in used, you'll get an "Address already in use"
message. Under Unix, the C<netstat -a> command will show
which services current have servers.

=item Listen

The C<Listen> parameter is set to the maximum number of
pending connections we can accept until we turn away incoming clients.
Think of it as a call-waiting queue for your telephone.
The low-level Socket module has a special symbol for the system maximum, which
is SOMAXCONN.

=item Reuse

The C<Reuse> parameter is needed so that we restart our server
manually without waiting a few minutes to allow system buffers to
clear out.

=back

Once the generic server socket has been created using the parameters
listed above, the server then waits for a new client to connect
to it.  The server blocks in the C<accept> method, which eventually an
bidirectional connection to the remote client.  (Make sure to autoflush
this handle to circumvent buffering.)

To add to user-friendliness, our server prompts the user for commands.
Most servers don't do this.  Because of the prompt without a newline,
you'll have to use the C<sysread> variant of the interactive client above.

This server accepts one of five different commands, sending output
back to the client.  Note that unlike most network servers, this one
only handles one incoming client at a time.  Multithreaded servers are
covered in Chapter 6 of the Camel as well as later in this manpage.

Here's the code.  We'll

 #!/usr/bin/perl -w
 use IO::Socket;
 use Net::hostent;		# for OO version of gethostbyaddr

 $PORT = 9000;			# pick something not in use

 $server = IO::Socket::INET->new( Proto     => 'tcp',
                                  LocalPort => $PORT,
                                  Listen    => SOMAXCONN,
                                  Reuse     => 1);

 die "can't setup server" unless $server;
 print "[Server $0 accepting clients]\n";

 while ($client = $server->accept()) {
   $client->autoflush(1);
   print $client "Welcome to $0; type help for command list.\n";
   $hostinfo = gethostbyaddr($client->peeraddr);
   printf "[Connect from %s]\n", $hostinfo->name || $client->peerhost;
   print $client "Command? ";
   while ( <$client>) {
     next unless /\S/;	     # blank line
     if    (/quit|exit/i)    { last;                                     }
     elsif (/date|time/i)    { printf $client "%s\n", scalar localtime;  }
     elsif (/who/i )         { print  $client `who 2>&1`;                }
     elsif (/cookie/i )      { print  $client `/usr/games/fortune 2>&1`; }
     elsif (/motd/i )        { print  $client `cat /etc/motd 2>&1`;      }
     else {
       print $client "Commands: quit date who cookie motd\n";
     }
   } continue {
      print $client "Command? ";
   }
   close $client;
 }

=head1 UDP: Message Passing

Another kind of client-server setup is one that uses not connections, but
messages.  UDP communications involve much lower overhead but also provide
less reliability, as there are no promises that messages will arrive at
all, let alone in order and unmangled.  Still, UDP offers some advantages
over TCP, including being able to "broadcast" or "multicast" to a whole
bunch of destination hosts at once (usually on your local subnet).  If you
find yourself overly concerned about reliability and start building checks
into your message system, then you probably should use just TCP to start
with.

Here's a UDP program similar to the sample Internet TCP client given
earlier.  However, instead of checking one host at a time, the UDP version
will check many of them asynchronously by simulating a multicast and then
using select() to do a timed-out wait for I/O.  To do something similar
with TCP, you'd have to use a different socket handle for each host.

    #!/usr/bin/perl -w
    use strict;
    use Socket;
    use Sys::Hostname;

    my ( $count, $hisiaddr, $hispaddr, $histime,
	 $host, $iaddr, $paddr, $port, $proto,
	 $rin, $rout, $rtime, $SECS_of_70_YEARS);

    $SECS_of_70_YEARS      = 2208988800;

    $iaddr = gethostbyname(hostname());
    $proto = getprotobyname('udp');
    $port = getservbyname('time', 'udp');
    $paddr = sockaddr_in(0, $iaddr); # 0 means let kernel pick

    socket(SOCKET, PF_INET, SOCK_DGRAM, $proto)   || die "socket: $!";
    bind(SOCKET, $paddr)                          || die "bind: $!";

    $| = 1;
    printf "%-12s %8s %s\n",  "localhost", 0, scalar localtime time;
    $count = 0;
    for $host (@ARGV) {
	$count++;
	$hisiaddr = inet_aton($host) 	|| die "unknown host";
	$hispaddr = sockaddr_in($port, $hisiaddr);
	defined(send(SOCKET, 0, 0, $hispaddr))    || die "send $host: $!";
    }

    $rin = '';
    vec($rin, fileno(SOCKET), 1) = 1;

    # timeout after 10.0 seconds
    while ($count && select($rout = $rin, undef, undef, 10.0)) {
	$rtime = '';
	($hispaddr = recv(SOCKET, $rtime, 4, 0)) 	|| die "recv: $!";
	($port, $hisiaddr) = sockaddr_in($hispaddr);
	$host = gethostbyaddr($hisiaddr, AF_INET);
	$histime = unpack("N", $rtime) - $SECS_of_70_YEARS ;
	printf "%-12s ", $host;
	printf "%8d %s\n", $histime - time, scalar localtime($histime);
	$count--;
    }

=head1 SysV IPC

While System V IPC isn't so widely used as sockets, it still has some
interesting uses.  You can't, however, effectively use SysV IPC or
Berkeley mmap() to have shared memory so as to share a variable amongst
several processes.  That's because Perl would reallocate your string when
you weren't wanting it to.

Here's a small example showing shared memory usage.

    use IPC::SysV qw(IPC_PRIVATE IPC_RMID S_IRWXU S_IRWXG S_IRWXO);

    $size = 2000;
    $key = shmget(IPC_PRIVATE, $size, S_IRWXU|S_IRWXG|S_IRWXO) || die "$!";
    print "shm key $key\n";

    $message = "Message #1";
    shmwrite($key, $message, 0, 60) || die "$!";
    print "wrote: '$message'\n";
    shmread($key, $buff, 0, 60) || die "$!";
    print "read : '$buff'\n";

    # the buffer of shmread is zero-character end-padded.
    substr($buff, index($buff, "\0")) = '';
    print "un" unless $buff eq $message;
    print "swell\n";

    print "deleting shm $key\n";
    shmctl($key, IPC_RMID, 0) || die "$!";

Here's an example of a semaphore:

    use IPC::SysV qw(IPC_CREAT);

    $IPC_KEY = 1234;
    $key = semget($IPC_KEY, 10, 0666 | IPC_CREAT ) || die "$!";
    print "shm key $key\n";

Put this code in a separate file to be run in more than one process.
Call the file F<take>:

    # create a semaphore

    $IPC_KEY = 1234;
    $key = semget($IPC_KEY,  0 , 0 );
    die if !defined($key);

    $semnum = 0;
    $semflag = 0;

    # 'take' semaphore
    # wait for semaphore to be zero
    $semop = 0;
    $opstring1 = pack("sss", $semnum, $semop, $semflag);

    # Increment the semaphore count
    $semop = 1;
    $opstring2 = pack("sss", $semnum, $semop,  $semflag);
    $opstring = $opstring1 . $opstring2;

    semop($key,$opstring) || die "$!";

Put this code in a separate file to be run in more than one process.
Call this file F<give>:

    # 'give' the semaphore
    # run this in the original process and you will see
    # that the second process continues

    $IPC_KEY = 1234;
    $key = semget($IPC_KEY, 0, 0);
    die if !defined($key);

    $semnum = 0;
    $semflag = 0;

    # Decrement the semaphore count
    $semop = -1;
    $opstring = pack("sss", $semnum, $semop, $semflag);

    semop($key,$opstring) || die "$!";

The SysV IPC code above was written long ago, and it's definitely
clunky looking.  For a more modern look, see the IPC::SysV module
which is included with Perl starting from Perl 5.005.

=head1 NOTES

Most of these routines quietly but politely return C<undef> when they
fail instead of causing your program to die right then and there due to
an uncaught exception.  (Actually, some of the new I<Socket> conversion
functions  croak() on bad arguments.)  It is therefore essential to
check return values from these functions.  Always begin your socket
programs this way for optimal success, and don't forget to add B<-T>
taint checking flag to the #! line for servers:

    #!/usr/bin/perl -Tw
    use strict;
    use sigtrap;
    use Socket;

=head1 BUGS

All these routines create system-specific portability problems.  As noted
elsewhere, Perl is at the mercy of your C libraries for much of its system
behaviour.  It's probably safest to assume broken SysV semantics for
signals and to stick with simple TCP and UDP socket operations; e.g., don't
try to pass open file descriptors over a local UDP datagram socket if you
want your code to stand a chance of being portable.

As mentioned in the signals section, because few vendors provide C
libraries that are safely re-entrant, the prudent programmer will do
little else within a handler beyond setting a numeric variable that
already exists; or, if locked into a slow (restarting) system call,
using die() to raise an exception and longjmp(3) out.  In fact, even
these may in some cases cause a core dump.  It's probably best to avoid
signals except where they are absolutely inevitable.  This 
will be addressed in a future release of Perl.

=head1 AUTHOR

Tom Christiansen, with occasional vestiges of Larry Wall's original
version and suggestions from the Perl Porters.

=head1 SEE ALSO

There's a lot more to networking than this, but this should get you
started.

For intrepid programmers, the indispensable textbook is I<Unix Network
Programming> by W. Richard Stevens (published by Addison-Wesley).  Note
that most books on networking address networking from the perspective of
a C programmer; translation to Perl is left as an exercise for the reader.

The IO::Socket(3) manpage describes the object library, and the Socket(3)
manpage describes the low-level interface to sockets.  Besides the obvious
functions in L<perlfunc>, you should also check out the F<modules> file
at your nearest CPAN site.  (See L<perlmodlib> or best yet, the F<Perl
FAQ> for a description of what CPAN is and where to get it.)

Section 5 of the F<modules> file is devoted to "Networking, Device Control
(modems), and Interprocess Communication", and contains numerous unbundled
modules numerous networking modules, Chat and Expect operations, CGI
programming, DCE, FTP, IPC, NNTP, Proxy, Ptty, RPC, SNMP, SMTP, Telnet,
Threads, and ToolTalk--just to name a few.

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美一区二区三区视频在线观看| 一区二区三区在线观看视频| 麻豆精品国产传媒mv男同| 这里只有精品电影| 日韩成人精品在线观看| 欧美精品免费视频| 男女男精品视频网| 国产亚洲成年网址在线观看| 国产夫妻精品视频| 中文字幕一区二区三区不卡在线 | 91免费版在线看| 国产精品国产三级国产专播品爱网 | 亚洲午夜一区二区| 91精品国产一区二区三区香蕉 | 香蕉av福利精品导航| 欧美一三区三区四区免费在线看| 蜜桃视频第一区免费观看| 久久久久国产精品免费免费搜索| 成人黄色免费短视频| 亚洲综合区在线| 日韩精品一区二区三区四区| 国产老妇另类xxxxx| 亚洲三级小视频| 欧美日韩国产首页| 国产高清成人在线| 一片黄亚洲嫩模| 精品国产乱码久久| 91视频com| 黄页网站大全一区二区| 亚洲情趣在线观看| 欧美mv和日韩mv的网站| 99久久国产免费看| 日韩专区一卡二卡| 国产精品毛片无遮挡高清| 欧美伊人久久久久久久久影院| 久久99国产精品久久99果冻传媒| 日韩美女视频一区| 欧美sm极限捆绑bd| 91美女在线看| 国产美女在线精品| 三级在线观看一区二区| 国产视频一区在线播放| 欧美另类一区二区三区| 99久久久国产精品免费蜜臀| 九一九一国产精品| 午夜精品久久久| 中文字幕一区二区三| 欧美大白屁股肥臀xxxxxx| 色天使久久综合网天天| 成人一级黄色片| 日本中文字幕一区| 一区二区三区欧美视频| 欧美激情中文字幕一区二区| 欧美精品第一页| 欧美午夜片在线观看| 国产69精品久久777的优势| 日韩中文字幕1| 一区二区三区在线观看动漫| 日本一区二区三区四区| 精品国产乱码久久久久久图片| 在线播放欧美女士性生活| 精品奇米国产一区二区三区| 欧美最猛性xxxxx直播| aaa欧美色吧激情视频| 国产91在线|亚洲| 精品一区在线看| 日韩成人av影视| 日韩一区精品视频| 午夜精品福利一区二区蜜股av| 一级精品视频在线观看宜春院| 日韩理论片网站| 中文字幕一区二区三区在线播放| 欧美国产激情二区三区| 国产喷白浆一区二区三区| 久久奇米777| 国产欧美在线观看一区| 久久久美女毛片| 国产片一区二区三区| 中文幕一区二区三区久久蜜桃| 国产亚洲一区二区三区四区| 久久久久国产精品麻豆ai换脸| 国产亚洲欧美日韩日本| 日本一区二区在线不卡| 国产精品色哟哟| 中文字幕在线免费不卡| 亚洲精品中文在线观看| 夜夜嗨av一区二区三区四季av| 一区二区三区中文字幕电影| 亚洲午夜精品一区二区三区他趣| 亚洲影院在线观看| 丝袜亚洲精品中文字幕一区| 久久精品国产秦先生| 韩日av一区二区| 成人动漫中文字幕| 在线一区二区三区四区| 欧美精品视频www在线观看| 欧美高清精品3d| 日韩一级片在线观看| 久久久.com| 国产精品成人在线观看| 一区二区三区精品久久久| 天堂蜜桃一区二区三区| 精品午夜一区二区三区在线观看| 国产成人自拍网| 91在线观看美女| 91精品国产综合久久久久久久久久 | 亚洲欧美一区二区三区久本道91 | 亚洲三级理论片| 亚洲第一福利一区| 麻豆精品国产91久久久久久| 成人国产免费视频| 欧美三级三级三级爽爽爽| 久久综合一区二区| 亚洲欧美日韩久久| 欧美a级一区二区| 91在线视频18| 日韩一区二区三区在线| 国产精品伦理在线| 日韩高清不卡一区二区三区| 国产成都精品91一区二区三| 欧美精选在线播放| 亚洲国产精品99久久久久久久久 | 91麻豆精品国产91久久久久久久久| 26uuu精品一区二区| 亚洲视频一二三| 国内国产精品久久| 欧美精品123区| 成人性生交大片免费看中文 | 一区二区三区鲁丝不卡| 狠狠色综合日日| 欧美特级限制片免费在线观看| 4438成人网| 国产精品国产三级国产三级人妇| 婷婷中文字幕综合| 成人看片黄a免费看在线| 91精品在线观看入口| 中文字幕亚洲一区二区av在线| 蜜桃精品在线观看| 91最新地址在线播放| 国产欧美一区二区在线| 日韩高清电影一区| 在线视频你懂得一区二区三区| 欧美激情在线一区二区| 麻豆精品久久久| 91精品国产色综合久久| 亚洲欧美日韩国产中文在线| 成人一区二区三区视频在线观看| 日韩欧美国产不卡| 午夜精品一区二区三区三上悠亚| 97久久超碰精品国产| 日本一区二区久久| 久久成人免费电影| 日韩欧美国产精品| 另类人妖一区二区av| 欧美精品乱人伦久久久久久| 一区二区三区中文字幕电影| 97久久精品人人爽人人爽蜜臀| 国产日韩欧美亚洲| 国产一区二区在线观看免费| 欧美成人女星排行榜| 免费观看成人av| 91精品国产入口| 免费观看日韩av| 亚洲少妇中出一区| 成人动漫av在线| 国产精品女同一区二区三区| 丁香一区二区三区| 国产午夜精品久久| 成人激情免费视频| 国产三级精品三级| 成人看片黄a免费看在线| 亚洲欧洲精品成人久久奇米网| 成人av网站在线观看免费| 中文字幕免费观看一区| 成人国产免费视频| 亚洲色图20p| 欧美久久久久中文字幕| 日本欧美一区二区在线观看| 日韩欧美电影一区| 国产精品影视网| 国产午夜亚洲精品午夜鲁丝片| 国产成人精品网址| 亚洲欧美色综合| 欧美日韩亚洲丝袜制服| 男女性色大片免费观看一区二区| 精品成人免费观看| 国产.欧美.日韩| 亚洲一区视频在线| 欧美一区二区不卡视频| 国产一区二区三区久久悠悠色av| 国产午夜精品一区二区| 91蜜桃在线免费视频| 天堂久久一区二区三区| 精品不卡在线视频| 91啪亚洲精品| 蜜臀久久久久久久| 国产精品麻豆网站| 4438成人网| 99精品久久免费看蜜臀剧情介绍| 亚洲国产va精品久久久不卡综合|