Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Unix Domain Sockets

by pileofrogs (Priest)
on Feb 10, 2009 at 18:20 UTC ( [id://742842]=perlquestion: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
Re: Unix Domain Sockets
by ikegami (Patriarch) on Feb 10, 2009 at 19:19 UTC

    Two problems:

    • You don't flush your output (Suffering from buffering)
    • You instruct the programs to read until a newline is received, but you don't send newlines.

    By the way, IO::Socket::UNIX is a much cleaner interface.

    Update: Working code:

    Server:

    #! /usr/bin/perl -w -T use strict; use IO::Socket::UNIX qw( SOCK_STREAM SOMAXCONN ); my $socket_path = '/tmp/wibble'; unlink($socket_path); my $listner = IO::Socket::UNIX->new( Type => SOCK_STREAM, Local => $socket_path, Listen => SOMAXCONN, ) or die("Can't create server socket: $!\n"); my $socket = $listner->accept() or die("Can't accept connection: $!\n"); chomp( my $line = <$socket> ); print qq{Client Sez "$line"\n}; print $socket "Same to ya, fella\n";

    Client:

    #!/usr/bin/perl -w -T use strict; use IO::Socket::UNIX qw( SOCK_STREAM ); my $socket_path = '/tmp/wibble'; my $socket = IO::Socket::UNIX->new( Type => SOCK_STREAM, Peer => $socket_path, ) or die("Can't connect to server: $!\n"); print $socket "Wibble\n"; chomp( my $line = <$socket> ); print qq{Server Sez "$line"\n};

    Note that IO::Socket::UNIX turns on autoflushing on the socket for you.

    Note that SOMAXCONN is silly if you only ever accept one connection.

      ++

      Thanks!!

Re: Unix Domain Sockets
by samtregar (Abbot) on Feb 10, 2009 at 18:29 UTC
    Did you read perlipc? There's a unix domain socket example there. It doesn't do bidirectional communication per-se but it may suggest a fix for your problem if you're setting up your sockets incorrectly.

    Maybe you could post a small sample of code that demonstrates your problem?

    -sam

      Heh... Yes, I've read perlipc about 100 times. I'll go post that small sample now...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://742842]
Approved by MidLifeXis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-26 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found