UNIX domain sockets

E

Erik Anderson

I'm trying to implement IPC using IO::Socket::UNIX, but have come into
significant difficulty. I've searched groups.google.com, and have been
able to find very few examples of the proper use of IO::Socket::UNIX.

I have written a very simple client and server system to test this. The
code is below. The client sends some data to the server, which should
display it...it's not working though. Any ideas?

Client Code:
--------------------
#!/usr/bin/perl

use IO::Handle;
use IO::Socket;

$socket = IO::Socket::UNIX->new( Peer => "/sckt",
Type => SOCK_STREAM,
Timeout => 10 ) or die "$!\n$@";

$socket->autoflush(1);

$num=0;
while(1) {
print $socket "$num";
$socket->flush();
$num++;
sleep(2);
}

exit;
--------------------

Server code:
--------------------
#!/usr/bin/perl

use IO::Handle;
use IO::Socket;
unlink "/sckt";
my $Server = IO::Socket::UNIX->new( Local => "/sckt",
Type => SOCK_STREAM,
Listen => 5
) or die $@;
while($session = $Server->accept) {
chomp($line = <$session>);
print "From client: " . $line . "\n";
$Server->flush();

sleep(1);
}

exit;
 
B

Bruce James

Hi,
Just out of interest, is /sckt writable? IE do you have permission to
write to / ?
Perhaps /tmp/sckt would be a better location.

Good luck

Bruce

Jim Gibson said:
Erik said:
I'm trying to implement IPC using IO::Socket::UNIX, but have come into
significant difficulty. I've searched groups.google.com, and have been
able to find very few examples of the proper use of IO::Socket::UNIX.

I have written a very simple client and server system to test this. The
code is below. The client sends some data to the server, which should
display it...it's not working though. Any ideas?

As Tim Heany pointed out, the main problem is that you are not sending
any end-of-line characers from the client to the server, but the server
is waiting for an EOL before proceeding. I have some additional comments:
Client Code:

Always:

use strict;
use IO::Handle;
use IO::Socket;
$socket = IO::Socket::UNIX->new( Peer => "/sckt",
Type => SOCK_STREAM,
Timeout => 10 ) or die "$!\n$@";

my $socket = ...
$socket->autoflush(1);

Sockets are autoflush be default, but this line shouldn't hurt.

my $num = 0;
while(1) {
print $socket "$num";
$socket->flush();
$num++;
sleep(2);
}

You are writing an infinite amount of data, but very slowly: 10
characters total in the first 20 seconds. Is your client ever going to
quit? Also, for consistency, use $socket->print():

while( $num < 10 ) [
$socket->print("Line $num\n");
$socket->flush();
$num++;
sleep(2);
}

No need to call exit as the last line in your program.
--------------------

Server code:
--------------------
#!/usr/bin/perl

use IO::Handle;
use IO::Socket;
unlink "/sckt";
my $Server = IO::Socket::UNIX->new( Local => "/sckt",
Type => SOCK_STREAM,
Listen => 5
) or die $@;

$@ contains the error for the last eval function call. You may want to
check $! instead.
while($session = $Server->accept) {
chomp($line = <$session>);

You are only reading one line from the client. You should put this in a
while loop and read until said:
print "From client: " . $line . "\n";
$Server->flush();

There should be no need to flush the $Server socket. You are not writing
to this socket, and flushing an input buffer should rarely be done. You
may be dumping incoming data.
sleep(1);

You may want to call $session->close() here -- I am not sure if it will
be closed automatically.

If you really need to read and write data without line endings, you
should consider using syswrite and sysread instead of print and <>.

I recommend getting and reading a copy of "Unix Network Programming,
Vol. 1", by W. Richard Stevens.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top