Monolith-0 has asked for the wisdom of the Perl Monks concerning the following question:

There are two things I want to do:

First, I'd simply like to know how I can read the output from a remote address:port when it is opened.

Second thing I'd like to know is how to make a server send some text through a port when it is opened. I have a feeling this is generally done without Perl, but I still need to know.

Replies are listed 'Best First'.
(jeffa) Re: Port stuff
by jeffa (Bishop) on Feb 15, 2001 at 05:14 UTC
    Don't forget about telnet - when you telnet to another port besides 23 - you are using telnet as sort of a generic client.

    And actually, Perl is a perfect tool for this sort of thing. There are two books out there I recommend you buy - O'Reilly's the Perl Cookbook and Addison-Wesley's Network Programming with Perl.

    But to get you set up - here is a sample server straight from the cookbook:

    use strict; use IO::Socket; my $sock = new IO::Socket::INET (LocalPort => 1200, Proto => 'tcp', Listen => 5, Reuse => 1, ); die "Can't connect: $!\n" unless $sock; while (my $new_sock = $sock->accept()) { my $msg = <$new_sock>; print "client said '$msg'\n"; } close $sock;
    and here is a sample client:
    use strict; use IO::Socket; my $sock = new IO::Socket::INET (PeerAddr => '127.0.0.1', PeerPort => 1200, Proto => 'tcp', ); die "Can't create: $!\n" unless $sock; print $sock "send money!"; close $sock;
    This is just a simple example - look into IO::Socket and also Socket.

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: Port stuff
by dws (Chancellor) on Feb 15, 2001 at 13:44 UTC
    This may go way beyond what you need at the moment, but if you're going to be writing Perl servers to do networking stuff, Lincoln Stein's new book Network Programming With Perl is excellent. It has several seriously real examples that you can borrow from.

    jeffa reviewed it here.

Re: Port stuff
by Monolith-0 (Beadle) on Feb 15, 2001 at 09:55 UTC
    Let me try to make my question a little more clearer, because I didn't explain it very well initially.

    'jazz.logicware.com:10055' when opened with telnet returns some lines of plain text, and then closes the connection. I want to ultimatly have a script that will act like that server, but also once somebody opens that port, the script will read from the original (eg. logicware) server, and forward that text on to the user.
    And since I am obviously new at this, I would just like to know how to do those two functions seperatly so that I could learn how to do more with this stuff.
      For that you should combine jeffa's responce above with this psudeo-code (I don't want to give you a solution to copy and paste) and documentation for IO::Socket may be useful too:
      bind to port for every client on port connect to remote server get text from remote server send text to client next client
      Or if the text is constant
      connect to remote server store text from remote server bind to port for every client on port send stored text to client next client
Re: Port stuff
by Monolith-0 (Beadle) on Feb 16, 2001 at 08:04 UTC
    Ok, I finally got it. Here's what I ended up with:
    use strict; use IO::Socket; my $client=new IO::Socket::INET (LocalPort => 10060, Proto => 'tcp', Listen => 5, Reuse => 1); die "Can't connect: $!\n" unless $client; while (my $return=$client->accept()) { my $server=new IO::Socket::INET (PeerAddr => 'jazz.logicware.com +', PeerPort => 10057, Proto => 'tcp'); die "Can't create: $!\n" unless $server; while (my $msg=<$server>) { print $return $msg; } } close $client;
    Any additional suggestions would be great.
    One additional thing I'd like to know is how to get the IP (and port) of the client.

    Thanks for the help.