English 中文(简体)
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.

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.





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签