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

I've tried the code in Perl Cookbook, and many modifications. Here's my socket programme code:
#!/usr/bin/perl use IO::Socket; use strict; use warnings; unlink "/tmp/mysock"; our $server = IO::Socket::UNIX->new(LocalAddr => "/tmp/mysock", Type => SOCK_DGRAM, Listen => 5) or die $!; while (my $client = $server->accept()) { while (defined (my $buf = <$client>)) { print "$buf\n"; } } close ($server); exit(0);

When I run it, the ->new() call appears to work. No error codes and the debugger reports that $server is a socket. The /tmp/mysock file doesn't get created, though. Then the while($client = ... ) just falls through. It doesn't wait for the socket to say anything, it just skips to the close() statement and exits.

I've done this with INET sockets a number of times with no problems, but this is the first time I've tried UNIX sockets. If anyone has any ideas, I'd appreciate hearing about them. Thanks Russ Jones

Update Thanks !! IT works

Replies are listed 'Best First'.
Re: problem with UNIX Domain sockets
by gmargo (Hermit) on Dec 19, 2009 at 10:04 UTC

    For streams, use SOCK_STREAM and accept(). (See TCP server in Cookbook.)

    For datagrams, use SOCK_DGRAM and recv(). (See UDP server in Cookbook.)

    You are mixing SOCK_DGRAM and accept().

Re: problem with UNIX Domain sockets
by zwon (Abbot) on Dec 19, 2009 at 10:39 UTC

    The socket isn't created because the parameter name is Local, not LocalAddr. It should be:

    my $server = IO::Socket::UNIX->new( Local => '/tmp/mysock' ) or die $! +;
      Hi Thanks a lot for your reply !! It works!!

      I am facing with another problem .I want to communicate between two process using sockets.One of those process is a C program(client) and another is a perl script(server).I am able to send messages from client, but some how I am unbale to recieve those messages at server side.

      #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> #include <stdio.h> #include <string.h> #define NSTRS 2 /* no. of strings */ #define ADDRESS "/tmp/temp_socket" /* addr to connect */ /* * Strings we send to the server. */ char *strs[NSTRS] = { "This is the second string from the client.\n", "This is the third string from the client.\n" }; int main() { char c; FILE *fp; register int i, s, len; struct sockaddr_un saun; /* * Get a socket to work with. This socket will * be in the UNIX domain, and will be a * stream socket. */ if ((s = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { perror("client: socket"); return(1); } /* * Create the address we will be connecting to. */ saun.sun_family = AF_UNIX; strcpy(saun.sun_path, ADDRESS); /* * Try to connect to the address. For this to * succeed, the server must already have bound * this address, and must have issued a listen() * request. * * The third argument indicates the "length" of * the structure, not just the length of the * socket name. */ len = sizeof(saun.sun_family) + strlen(saun.sun_path); if (connect(s, &saun, len) < 0) { perror("client: connect"); return(1); } /* * We'll use stdio for reading * the socket. */ fp = fdopen(s, "r"); /* * First we read some strings from the server * and print them out. */ /*for (i = 0; i < 1; i++) { while ((c = fgetc(fp)) != EOF) { putchar(c); if (c == '\n') break; } }*/ /* * Now we send some strings to the server. */ for (i = 0; i < NSTRS; i++) sendto(s, strs[i], strlen(strs[i]), 0,(struct sockaddr *)&saun +, sizeof(saun)); /* * We can simply use close() to terminate the * connection, since we're done with both sides. */ close(s); return(0); } Server(perl script) : #!/usr/bin/perl -w use strict; use IO::Socket::UNIX; # simple server use IO::Handle; unlink "/tmp/temp_socke"; our $server = IO::Socket::UNIX->new(Local => "/tmp/temp_socke", Type => SOCK_DGRAM, Listen => 5) or die $ !; print "I am here\n"; my $wanted = 1024; my $buff; while ($server->recv($buff, $wanted)) { print "$buff"; } close ($server); exit(0);
      Please correct me where I am going wrong ? Thanks Zebith

        First of all, please don't delete your original post. The thread will make little sense in the future without it.

        Second of all, you again have a datagram vs. stream problem. The connect() is to connect to a TCP server, but you are running a UDP server. A UDP client just creates the socket and starts sending.

        This may help: http://www.prasannatech.net/2008/07/socket-programming-tutorial.html This gentleman has provided simple examples of TCP and UDP servers and clients in C, Perl, Python, and Java.

Re: problem with UNIX Domain sockets (orig OP)
by ikegami (Patriarch) on Dec 21, 2009 at 14:38 UTC
    Original content of OP:
    I've tried the code in Perl Cookbook, and many modifications. Here's my socket programme code:
    #!/usr/bin/perl use IO::Socket; use strict; use warnings; unlink "/tmp/mysock"; our $server = IO::Socket::UNIX->new(LocalAddr => "/tmp/mysock", Type => SOCK_DGRAM, Listen => 5) or die $!; while (my $client = $server->accept()) { while (defined (my $buf = <$client>)) { print "$buf\n"; } } close ($server); exit(0);

    When I run it, the ->new() call appears to work. No error codes and the debugger reports that $server is a socket. The /tmp/mysock file doesn't get created, though. Then the while($client = ... ) just falls through. It doesn't wait for the socket to say anything, it just skips to the close() statement and exits.

    I've done this with INET sockets a number of times with no problems, but this is the first time I've tried UNIX sockets. If anyone has any ideas, I'd appreciate hearing about them. Thanks Russ Jones