17.6. Using UNIX Domain SocketsProblemYou want to communicate with other processes on only the local machine. SolutionUse domain sockets. You can use the code and techniques from the preceding Internet domain recipes, with the following changes: Discussion
Unix domain sockets have names like files on the filesystem. In fact, most systems implement them as special files; that's what Perl's
Supply the filename as the Peer argument to use IO::Socket; unlink "/tmp/mysock"; $server = IO::Socket::UNIX->new(LocalAddr => "/tmp/mysock", Type => SOCK_DGRAM, Listen => 5 ) or die $@; $client = IO::Socket::UNIX->new(PeerAddr => "/tmp/mysock", Type => SOCK_DGRAM, Timeout => 10 ) or die $@; Here's how to use the traditional functions to make stream sockets: use Socket; socket(SERVER, PF_UNIX, SOCK_STREAM, 0); unlink "/tmp/mysock"; bind(SERVER, sockaddr_un("/tmp/mysock")) or die "Can't create server: $!"; socket(CLIENT, PF_UNIX, SOCK_STREAM, 0); connect(CLIENT, sockaddr_un("/tmp/mysock")) or die "Can't connect to /tmp/mysock: $!";
Unless you know what you're doing, set the protocol (the Proto argument to
Because many systems actually create a special file in the filesystem, you should delete the file before you try to bind the socket. Even though there is a race condition (somebody could create a file with the name of your socket between your calls to See AlsoRecipes Recipe 17.1 through Recipe 17.5 Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|