Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
P is for Practical
 
PerlMonks  

local socket

by Punto (Scribe)
on Jun 04, 2001 at 03:07 UTC ( [id://85400]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

Hi.

I need to leave a program on the background, and then, from time to time, 're take' control of it, to monitor it's progress.
So I was thinking of running it from a perl program, with open3(), and then open a socket to listen for commands (and print the output).

So my question is: is there any way to open a 'local' socket? All the documentation I see is about network sockets, but I only want it to be accesible from the local computer (no network)

Thanks..

Replies are listed 'Best First'.
Re: local socket
by chipmunk (Parson) on Jun 04, 2001 at 03:42 UTC
    If the listening process binds the socket to localhost, then it will only accept connections from the local computer.

    Here's an example, using IO::Socket:

    #!/usr/local/bin/perl -w use strict; use IO::Socket; my $sock = IO::Socket::INET->new(Listen => 1, LocalAddr => 'localhost', LocalPort => 43546, Proto => 'tcp', Reuse => 1, ); my $NL = "\015\012"; while (my $client = $sock->accept()) { print $client "Hello. To close connection, type quit$NL"; while (<$client>) { tr/\015\012//d; print "Client said $_\n"; print $client "You said $_$NL"; last if /^quit/i; } print "Client quit.\n"; print $client "Bye!$NL"; close $client; }
    Start this running, then try connecting from various computers. You'll only be able to connect from the local computer, and only when connecting specifically to 'localhost'. (Exit the server with a kill signal, e.g. Ctrl-C)
      Chipmunk, that's interesting. I checked the IO::Socket::INET documentation and related pages, plus perlfunc and perlipc, but can't find a description of restricting a listener. I didn't know sockets could do that at all! Can you point to more info on that?
        I don't remember where I learned about restricting sockets to specific addresses, and I can't find any really descriptive information on it. Very frustrating... :(

        Here is a relevant snippet from perlipc:

        And here's a corresponding server to go along with it. We'll leave the address as INADDR_ANY so that the kernel can choose the appropriate interface on multihomed hosts. If you want sit on a particular interface (like the external side of a gateway or firewall machine), you should fill this in with your real address instead.
        And here's something from Socket:
        INADDR_ANY Note: does not return a number, but a packed string. Returns the 4-byte wildcard ip address which specifies any of the hosts ip addresses. (A particular machine can have more than one ip address, each address corresponding to a particular network interface. This wildcard address allows you to bind to all of them simultaneously.) Normally equivalent to inet_aton('0.0.0.0').
        Let's say you have a machine that has two IP addresses: mymachine.example.com and foo.bar.baz.com. A server program which binds to the address mymachine.example.com will only accept connections made to mymachine.example.com. A program which binds to foo.bar.baz.com will only accept connections to foo.bar.baz.com, and a program that binds to localhost will only accept connections to localhost (which must, of course, also be from localhost).

        On the other hand, a server program which binds to INADDR_ANY will accept connections made to any address that happens to resolve to the machine in question, including mymachine.example.com, foo.bar.baz.com, and localhost. That's usually what you want.

      Hi. thanks for your reply, it's very useful.

      But I was looking for one of those sockets that are like files, on the filesystem, wich would be great, because I need to set restrictions on them (after all, I'm using it to pass commands to a running program)
      I think I saw mysql using them. Do you know anything about it?

      Thanks..

        IO::Socket::UNIX

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://85400]
Approved by root
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.