Re: UNIX domain sockets
From: Bruce James (bruce_at_pomegranate.ltd.uk)
Date: 11/02/03
- Previous message: Samurai Jesus: "Newbie to perl, need help"
- In reply to: Jim Gibson: "Re: UNIX domain sockets"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 02 Nov 2003 00:43:23 +0000
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
In article <3fa2ca69_2@127.0.0.1>, Jim Gibson
<jgibson@mail.arc.nasa.gov> wrote:
> Erik Anderson wrote:
> > 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:
> > --------------------
> > #!/usr/bin/perl
>
> 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.
>
> >
> > $num=0;
>
> 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);
> }
>
> >
> > exit;
>
> 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 <$session> returns undef.
>
> > 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.
>
> > }
> >
> > exit;
> > --------------------
> >
> > Thanks!
> > -Erik Anderson
> >
>
> 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.
>
- Previous message: Samurai Jesus: "Newbie to perl, need help"
- In reply to: Jim Gibson: "Re: UNIX domain sockets"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|