snafu has asked for the wisdom of the Perl Monks concerning the following question:

I have looked all over the place for something about the basic steps to setting up a tcp socket in Perl using Socket.pm and I simply cannot find it. I am using Socket.pm and I need to know if anyone out there would be willing to provide this.

Now, I know that there are examples for using Socket.pm in the Camel and the Cookbook but the explanations (for me) are sketchy at best and are in no way IMO easy for a beginner to get.

So, for example, what I would like to know is what are the steps to setting up a socket? Something like the following would be helpful:

# First, get the remote IP address $remote_ipaddr = "192.168.120.1" # Then get the remote port $remote_port = "1400"; # Then what? And better yet, why?

The Socket.pm functions just totally baffle me as to their purpose and needs. There are so many of them yet only a few them are needed it seems like, right? And if so which ones are absolutely necessary to use? Why? At what times are they to be used and not used?

I am opting *not* to use IO::Socket::INET simply because its so easy to use and I really want to know what the needs are for Socket.pm (and my current project dissallows me from installing the module too).

----------
- Jim

Replies are listed 'Best First'.
Re: REQ concise steps sockets using Socket.pm
by mitd (Curate) on Aug 27, 2001 at 10:42 UTC

    Before We look at a little code, if you like would to learn about BSD style sockets in depth then beg, borrow, steal or buy Richard Stevens TCPIP Illustrated series. Expensive but worth every penny. If you would prefer your networking depth served up as Perl; I have recently bought Lincoln Steins 'Networking Programming with Perl' and highly recommend it.

    Now here's a little code taken from Lincoln's book. the comments are my own. It is a fragment from a simple daytime client

    use Socket; # declare some self describing constants use constant DEFAULT_ADDR => '127.0.0.1'; # the local host use contant PORT => 13; # well known daytime service port use constant IPPROTO_TCP => 6; # lets use TCP shall we # get host from commandline or use default my $address = shift || DEFAULT_ADDR; # well IP address is really a 32bit UINT my $packed_addr = inet_aton($address); # whoops better include socket/port my $destination = sockaddr_in(PORT, $packed_addr); #ok lets put it together get ourselfs a SOKET handle socket(SOCK,PF_INET, SOCK_STREAM, IPPROTO_TCP) or die; # hey man whats the time connect(SOCK, $destination); print <SOCK>; # ## there is a lot of good stuff around the Monasterary on this ##subject, like why it is often not a good idea to use things like ## print <SOCK> and $line = <SOCK> #

    Hopefully the above helps. There is lots more if you look around the joint.

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'

Re: REQ concise steps sockets using Socket.pm
by Cine (Friar) on Aug 27, 2001 at 04:40 UTC
    You could checkout 106156 or Net::Ping from where it is stolen...

    T I M T O W T D I