简体   繁体   中英

How can I connect to a remote machine via TCP and UDP with Perl?

I have a script that runs on a server and I want it now to send messages to my PC. I want to send TCP or UDP messages to some port.

What is the best way to do this (a tutorial will be great)?

And is there some client program that I can run on my PC that will listen to a local port for the messages?

A client can send TCP and UDP messages using a socket, for example:

use IO::Socket;
my $socket = IO::Socket::INET->new(PeerAddr => 'theserver',
    PeerPort => '1234', Proto => 'tcp', Type => SOCK_STREAM);
print $socket "Hello server!";
close($socket);

A server has to listen on a port, and accept messages that arrive on it:

my $socket = new IO::Socket::INET (LocalHost => 'hostname',
    LocalPort => '1234', Proto => 'tcp', Listen => 1, Reuse => 1);
my $incoming = $sock->accept();
while(<$incoming>) {
    print $_;
}
close($incoming);

This is just the tip of the iceberg, but hopefully this will help to get you started.

从阅读perlipc文档开始-它包含简单的服务器和客户端的示例,以及许多非常重要的附加组件(例如:如何正确守护程序)。

Although a bit dated, you want want to find a copy of Network Programming with Perl by Lincoln Stein. Also, look at the Perl modules with Socket in their names.

There are a lot of ways to do it. One of the easiest is to have your program email your email address. In Perl, you can use, eg Email::Send . Other ways include sockets, or having your program access a web server on your PC. It depends what facilities you want to use. I usually use a ssh command to access other computers across a network.

You could use Net Send if your on windows, Write "Net Help Send" for more info in CMD i don't know perl but start CMD Net Send Computer Message

Make sure thet the net send service is running... google it for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM