17.16. Writing a Multihomed Server17.16.1. ProblemYou want to write a server that knows that the machine it runs on has multiple IP addresses, and that it should possibly do different things for each address. 17.16.2. SolutionDon't bind your server to a particular address. Instead, bind to INADDR_ANY. Then once you've accept ed a connection, use getsockname on the client socket to find out which address the client connected to:
17.16.3. DiscussionWhereas getpeername (as discussed in Recipe 17.7) returns the address of the remote end of the socket, getsockname returns the address of the local end. When we've bound to INADDR_ANY, thus accepting connections on any address the machine has, we need to use getsockname to identify which address the client connected to. If you're using IO::Socket::INET, your code will look like this:
If you don't specify a local port to IO::Socket::INET->new, your socket will be bound to INADDR_ANY. If you want your server to listen only for a particular virtual host, don't use INADDR_ANY. Instead, bind to a specific host address:
17.16.4. See AlsoThe getsockname function in Chapter 29 of Programming Perl and in perlfunc(1); the documentation for the standard Socket and IO::Socket modules; the section on "Sockets" in Chapter 16 of Programming Perl or perlipc(1)
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|