I'm going to mostly answer my own question here.

It looks like there's nothing off-the-shelf that implements a loopback socket. If someone finds something, please let me know.

However, there is a way (at least on my win32 box) that will tie a pair of sockets together without the need for multithreading.

The magic function is called socketpair. The docs say that some pipes are implemented by way of this function call (but even if this is true on win32, the resultant objects were not select()able, so they were of no use to me). The socketpair interface creates two genuine sockets and ties them together for you.

I've taken an example using socketpair() and updated it to use IO::Handle objects, which can be easily passed around to other parts of the code. I also demonstrated the use of select() on the socket handles. I didn't call shutdown() on either handle, so they can both read/write from/to each other (refer to perldoc -f socketpair to see an example of shutdown()).

#!perl -w use strict; use Socket; # for AF_UNIX, SOCK_STREAM, PF_UNSPEC use IO::Handle; # for autoflush and object constructor use IO::Select; # to demonstrate select()ability my $line; my $child = IO::Handle->new(); my $parent = IO::Handle->new(); socketpair($child, $parent, AF_UNIX, SOCK_STREAM, PF_UNSPEC) or die "socketpair: $!"; my %names = ( $parent->fileno() => 'Parent', $child ->fileno() => 'Child', ); $child ->autoflush(1); $parent->autoflush(1); my $sel = IO::Select->new( $child, $parent ); $child ->print( "Hello, Parent\n" ); $parent->print( "Hello, Child\n" ); my @ready = $sel->can_read(0.1); print "There are ${\$sel->count} readable handles\n"; foreach my $ready ( @ready ) { my $name = $names{ $ready->fileno() }; print "$name receives: " . $ready->getline(); } $child->close(); $parent->close();

In reply to Re: Looking for a loopback socket for win32 by dpmott
in thread Looking for a loopback socket for win32 by dpmott

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.