Describe Bind System Call

, , No Comments

 After creating “socket” and before Sending/Receiving data using socket, it must be

associated with a local port and a network interface address. That’s why we can say
mapping of a socket to a port number and IP address is called a “binding”. Why
binding is needed, as you know server use socket so it can attend client request on
specific port. Socket must be associated with particular port on one network interface.
But think when we can have multiple network addresses on one host and each
network, we have some port addresses. In this case with the help of bind we can
define, for which network address (IP address + port address) with which port is
associated, otherwise imagine how much confusion can happen.

int bind(int sockfd, struct sockaddr_in *localaddr , intaddrlen);

Here in the above prototype of bind, sockfd is File descriptor of local socket, as
created by the socket function. Localaddr is a pointer to protocol address structure of
local socket. The special address ANY_ADDR can be used to allow the connection to
be made on any of the host’s interfaces and addrlen is indicating length in bytes of
structure referenced by address. On success, bind () returns a zero. On failure, it
returns -1 with an error number.

Use of Bind
During networking programming we find that there are three user of Bind system
A specific network address and port address can be registered to a client.
Server needs to register a specific network & port address with its socket,
basically it informs the system that “The address associated with me is this, and if
you get any message on this address just transfer it to me”.

• In case of connectionless client (see your unit 1 of block 1 to know about
connectionless and connection oriented services), it needs to assure that system
has provided some valid unique addresses, so that when server sends some
message it will reach the desired client. This approach of client is same like us,
when we write letter we always check whether we have written own address on
that envelop or not so that we can get reply on that address.

The bind system call fills the two tuple of association, Local address and Local
process elements. When we will use Bind function the client needs to call socket
system call because it needs to use the returned value as socket descriptor.

When binding is over, then in-case of UDP socket it is ready to send and receive
datagram’s. For TCP sockets, the socket is ready to connect or accept calls. Let’s see
what are these accept and connect call.

Let’s have an example:

#define CLIENTPORT 8090

int sockfd;
struct sockaddr_in local_addr;

sockfd = socket(AF_INET, SOCK_STREAM, 0);
client_addr.sin_family = AF_INET; /* host byte order */
client_addr.sin_port = htons(CLIENTPORT); /* short, network byte order */
client_addr.sin_addr.s_addr = inet_addr("");
bzero(&(client_addr.sin_zero), 8); /* zero the rest of the struct */

/* error checking for bind(): */
bind(sockfd, (struct sockaddr *)&client_addr, sizeof(struct sockaddr));

In socket programming we have different methods which may be useful to you, when
you want to choose an unused port at random you can write /* choose an unused port
at random */

and for choosing IP address you can use / use client IP address */

0 टिप्पणियाँ:

Post a Comment