I generally use Socket instead of the higher-level IO::Socket, but that's just my personaly preference. Here's some code to get you started. First the server:
use strict; use Socket; my $socket; # the socket waiting for incoming connections my $incoming; # an incoming connection my $port = 4000; my $ipaddr = "localhost"; my $numConnections = 1; socket($socket, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die "Could not create socket: $!\n"; bind($socket, sockaddr_in($port, inet_aton($ipaddr))) || die "Could not bind socket: $!\n"; listen($socket, $numConnections) || die "Could not listen on socket: $!\n"; accept($incoming, $socket) || die "Could not accept connection on socket: $!\n"; my $message; my $messageLen = 999999; # the maximum number of bytes to read off th +e socket defined(recv($incoming, $message, $messageLen, 0)) || die "Receive err +or: $!"; print "I got the following message: $message.\n"; defined(send($incoming, "2", 0)) || die "Send error: $!"; shutdown($incoming, 2); shutdown($socket, 2);
and now the client.
use strict; use Socket; my $socket; my $port = 4000; my $ipaddr = "localhost"; socket($socket, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die "Could not create socket: $!\n"; connect($socket, sockaddr_in($port, inet_aton($ipaddr))) || die "Could not connect: $!\n"; my $message; my $messageLen = 999999; # the maximum number of bytes to read off the + socket defined(send($socket, "1", 0)) || die "Send error: $!"; defined(recv($socket, $message, $messageLen, 0)) or die "Receive error +: $!"; print "I got the following message: $message.\n"; shutdown($socket, 2);
Good luck!

-Ton
-----
Be bloody, bold, and resolute; laugh to scorn
The power of man...


In reply to Re: Simple Msg/Response Socket Setup by ton
in thread Simple Msg/Response Socket Setup by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.