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

Hello, I got a book right in front of me that I'm using to test out some socket codes. Trying this one out for a socket between internal processes but got a problem when executing.
$AF_UNIX=1; $STREAMS=1; $PROTOCOL=0; socket($SERVERSOCKET,$AF_UNIX,$STREAMS,$PROTOCOL); bind($SERVERSOCKET,"./mysocket");

The error occurs at 'bind'. What exactly am I supposed to replace the "./mysocket" with? The book says its the file I am associating the socket handle with. I've tried all sorts of formats, absolute pathnames, relative pathnames but there is always an error with the binding.

Thanks for any help from you all.

Replies are listed 'Best First'.
Re: Socket between internal processes
by BrowserUk (Patriarch) on Sep 13, 2002 at 02:37 UTC

    A quick look at permfunc:bind gives the following information.

    bind SOCKET,NAME Binds a network address to a socket, just as the bind system call does +. Returns true if it succeeded, false otherwise. NAME should be a pac +ked address of the appropriate type for the socket. See the examples +in Sockets: Client/ Server Communication in the perlipc manpage

    You can find the reference in the above here. It contains several good examples.

    It also has this rather interesting comment:

    If you ever see code that does anything like explicitly setting $AF_IN +ET = 2, you know you're in for big trouble: An immeasurably superior +approach is to use the Socket module, which more reliably grants acce +ss to various constants and functions you'll need.

    This can be found here Socket though it is quite likely that this is a part of your stadard distribution It is in mine.


    Well It's better than the Abottoire, but Yorkshire!
Re: Socket between internal processes
by dws (Chancellor) on Sep 13, 2002 at 04:37 UTC
    The error occurs at 'bind'. What exactly am I supposed to replace the "./mysocket" with?

    You don't mention what OS you're doing this on. If you're using Win32, AF_UNIX isn't supported. You'll need to use AF_INET-style sockets, which work just fine for communicating between Win32 processes.

    If instead you're doing this on a *nix system, what does $@ say after bind fails? That might give you some guidance.

Re: Socket between internal processes
by sauoq (Abbot) on Sep 13, 2002 at 08:17 UTC
    bind($SERVERSOCKET,"./mysocket");

    I think you'd want that to be bind($SERVERSOCKET, sockaddr_un('./mysocket')); but you might find it easier to use IO::Socket::UNIX to hide some of the details. If you definitely want to do it by hand then, at the lesat, you should probably use the Socket module for its constants.

    By the way, the server should probably unlink('./mysocket') before binding to it and you should check the return value of bind() with bind(...) or die "..."; for instance.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Socket between internal processes
by mint1981 (Initiate) on Sep 13, 2002 at 06:10 UTC
    Thanks for the feedback. Someone here referred me to a section right here on perlmonks though and it has answered my question. So, problem solved :)