Blessings upon ye Monks.

I'm trying to do a conversation between a client and server using unix domain sockets. I can do one way communication, but not two-way. I haven't been able to find any docs specific to unix domain sockets that show a two way conversation. The docs about other sockets (tcp/ip, for instance) seem to imply you can just read and write to the same socket and it will "just work". I've found examples of one-way conversations, but no two way conversations.

So! My question to you monks is, does anyone have an example of a two way conversation using unix domain sockets that they could point me to? Or, if you know of something special I need to do (select?), that would also be great.

Update:

Now with example code!

The Server

#! /usr/bin/perl -w -T %ENV = (); $ENV{PATH} = "/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/ +sbin"; use strict; use Socket; my $socket_path = '/tmp/wibble'; my $sock_addr = sockaddr_un($socket_path); socket(my $server, PF_UNIX,SOCK_STREAM,0) || die "socket: $!"; unlink($socket_path); bind($server,$sock_addr) || die "bind: $!"; listen($server,SOMAXCONN) || die "listen: $!"; accept(my $client,$server); print 'Client Sez "'.<$client>."\"\n"; print $client "Same to ya, fella\n";

The Client

#! /usr/bin/perl -w -T %ENV = (); $ENV{PATH} = "/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/ +sbin"; use Socket; use strict; my $name = '/tmp/wibble'; my $sock_addr = sockaddr_un($name); socket(my $server, PF_UNIX,SOCK_STREAM,0) || die "socket: $!"; connect($server, $sock_addr) || die "connect: $!"; print $server "Wibble"; # if you comment this out, the 1 way conversation will work print "Server Sez ".<$server>."\n";

Note, that if you comment out the bit in the client where it attempts to reply to the server, the one way communication will work. If you leave it there, no communication works at all.

Thanks!

--Pileofrogs


In reply to Unix Domain Sockets by pileofrogs

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.