bind Function

 < Day Day Up > 



bind Function

The bind function binds a local name to a newly created socket. A name within the AF_INET context is an IP address (that represents an interface) and a port number. The bind function has the following prototype:

#include <sys/types.h> #include <sys/socket.h> int bind( int sock, struct sockaddr *addr, int addrlen );

Upon successful completion of the bind function (identified by a return value of 0), the socket identified by argument sock will be bound to the address defined in addr. The sock argument is a previously created socket returned from the socket function. The addr argument is a special structure that defines the interface address and port number (collectively called an address). Although the bind function utilizes the sockaddr structure, for AF_INET sockets, the sockaddr_in structure is used instead. This structure has the following layout as is shown in Listing 3.2.

Listing 3.2 Format of the sockaddr_in address structure.

start example
struct sockaddr_in {     int16_t  sin_family;     uint16_t sin_port;     struct in_addr sin_addr;     char     sin_zero[8]; }; struct in_addr {     uint32_t s_addr; };
end example

For Internet communication using the IPv4 protocol suite, we’ll use AF_INET solely for sin_family. Field sin_port is used to define our specified port number in network byte order. Therefore, we must use htons to load the port and ntohs to read it from this structure. Field sin_addr is, through s_addr, a 32-bit field that represents an IPv4 Internet address. Recall that IPv4 addresses are four-byte addresses. We’ll see quite often that the sin_addr is set to INADDR_ANY, which is the wildcard. When we’re accepting connections (server socket), this wildcard says we can accept connections from any available interface on the host. For client sockets, the rules differ.

Let’s now look at a quick example of addressing for both a server and client socket. In this example, we create a stream socket (servsock), create a socket address, and initialize it (servaddr), and then finally bind the address to the socket, as shown in Listing 3.3.

Listing 3.3 Example of the bind function for a server socket.

start example
int servsock; struct sockaddr_in servaddr; servsock = socket( AF_INET, SOCK_STREAM, 0); memset( &servaddr, 0, sizeof(servaddr) ); servaddr.sin_family = AF_INET; servaddr.sin_port = htons( 25 ); servaddr.sin_addr.s_addr = inet_addr( INADDR_ANY ); bind( servsock, (struct sockaddr_in *)&servaddr, sizeof(servaddr) );
end example

Note that if the sin_port had been defined as zero, the bind function would automatically assign an ephemeral (dynamic) port to it. This is a common method for client sockets, but server sockets typically require a known port number and, therefore, define one statically.

Let’s now look at what the bind function actually does from an interface perspective for both a client and a server. In Figure 3.1, we see an example in which we bind our server to a specific interface using the bind function. In the source code shown in the listing contained in Figure 3.1, what is interesting is the use of an IP address to define the address structure’s sin_addr element. An IP address is specified that is associated with a particular interface (bottom right of Figure 3.1). Once the bind is performed with this address, incoming connections will occur only through this interface. Clients attempting to connect to the server application in Figure 3.1 through the alternate interface (defined by “10.0.0.1”) will be refused.

click to expand
Figure 3.1: Binding a specific address to a server socket.

In the next example, Figure 3.2, we see another server bind function example. This example is the more traditional use of the bind function in which all interfaces are permitted for incoming connections to the server application. This functionality is configured using the INADDR_ANY symbolic constant, which represents the wildcard address (all available interfaces). In this example, clients can connect to the server application through either interface; neither will refuse connections.

click to expand
Figure 3.2: Binding the wildcard address to a server socket.

Finally, let’s look at a client example. Figure 3.3 shows an example of bind within the context of a client socket. This differs from the server example because instead of determining from which interface a connection can arrive, the client example determines through which interface a client may connect. Therefore, when a client performs the connect function (discussed later in this chapter), the outgoing socket will utilize only the “10.0.0.1” interface.

click to expand
Figure 3.3: Binding an address to a client socket.

A valid question for binding an address to a client socket is why restrict the outgoing connection to a single interface? One application is the balancing of sockets over two or more interfaces. As each socket is created, a different interface address is bound to the client socket. In this way, the number of sockets created are bound to a separate interface and, therefore, are shared among the available interfaces.

One final item to note is the effect of calling the bind function with incomplete information in the address structure. If the port number is set to zero in the sockaddr_in structure (sin_port), the bind function automatically assigns an ephemeral port to the socket. If the sin_addr.s_addr field is set to zero, then this has the same effect as the wildcard address (INADDR_ANY is zero on most systems). For a server socket, incoming connections are permitted from any interface. For a client socket, outgoing connections are permitted through any interface. Therefore, if a developer desired to provide a static port to a socket, the bind function with a sin_addr.s_addr of zero would provide this functionality.



 < Day Day Up > 



BSD Sockets Programming from a Multi-Language Perspective
Network Programming for Microsoft Windows , Second Edition (Microsoft Programming Series)
ISBN: 1584502681
EAN: 2147483647
Year: 2003
Pages: 225
Authors: Jim Ohlund

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net