开发者

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

开发者 https://www.devze.com 2022-12-12 06:48 出处:网络
I have a script that runs on a server and I want it now 开发者_如何学Pythonto send messages to my PC. I want to send TCP or UDP messages to some port.

I have a script that runs on a server and I want it now 开发者_如何学Pythonto 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.


Start with reading of perlipc documentation - it contains examples of simple servers and clients, with a lot of very important addons (like: how to properly daemonize).


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.


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.


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, e.g. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消