linux网络编程之bind函数

NAME

    bind - bind a name to a socket

SYNOPSIS

    #include <sys/socket.h>

    int bind(int socket, const struct sockaddr *address,
           socklen_t address_len);


DESCRIPTION

    The bind() function shall assign a local socket address address to a socket identified by descriptor socket that has no local socket address assigned. Sockets created with the socket() function are initially unnamed; they are identified only by their address family.

    The bind() function takes the following arguments:

    socket
        Specifies the file descriptor of the socket to be bound.
    address
        Points to a sockaddr structure containing the address to be bound to the socket. The length and format of the address depend on the address family of the socket.
    address_len
        Specifies the length of the sockaddr structure pointed to by the address argument.

    The socket specified by socket may require the process to have appropriate privileges to use the bind() function.

RETURN VALUE

    Upon successful completion, bind() shall return 0; otherwise, -1 shall be returned and errno set to indicate the error.




bind — Bind a local name to the socket

The bind socket function binds a unique local name to the socket with descriptor s.
 
Format

#include <sys/socket.h>
int      bind(int s,
              struct sockaddr *name,
              int namelen);

s
    The socket descriptor.
name
    Pointer to a sockaddr structure (buffer) containing the name that is to be bound to s.
namelen
    Size of the buffer pointed to by name, in bytes.

Normal return

Return code 0 indicates that the function was successful.

Error return

The return code -1 indicates an error. You can get the specific error code by calling sock_errno.


Programming considerations

    The bind function binds a unique local name to the socket with descriptor s. After calling socket, a descriptor does not have a name associated with it. The bind procedure also allows servers to specify from which network interfaces they want to receive UDP packets and TCP connection requests.
    The binding of a stream socket is not complete until a successful call to bind, listen, or connect is made. Applications using stream sockets must check the return values of bind, listen, and connect before using any function that requires a bound stream socket.
    When binding a socket to all local IP addresses (that is, INADDR_ANY is specified), the socket is bound to all IP interfaces that are currently active, as well as to any IP interfaces that are subsequently activated.

Examples

    Bind to a specific interface in the internet domain and make sure the sin_zero field is cleared:

    #include <sys/socket.h>
   
    int rc;
    int s;
    struct sockaddr_in myname;
   
    memset(&myname, 0, sizeof(myname));
    myname.sin_family      = AF_INET;
    myname.sin_port        = 5001;
    myname.sin_addr.s_addr = inetaddr("129.5.24.1"); /*specific interface*/
    rc = bind(s, (struct sockaddr *) &myname, sizeof(myname));

    Bind to all network interfaces in the internet domain.

    memset(&myname, 0, sizeof(myname));
    myname.sin_family      = AF_INET;
    myname.sin_port        = 5001;
    myname.sin_addr.s_addr = INADDR_ANY; /* all interfaces */
    rc = bind(s, (struct sockaddr *) &myname, sizeof(myname));


    Bind to a specific interface in the internet domain and let the system choose a port.

    memset(&myname, 0, sizeof(myname));
    myname.sin_family      = AF_INET;
    myname.sin_port        = INADDR_ANY;
    myname.sin_addr.s_addr = inetaddr("129.5.24.1"); /*specific interface*/
    rc = bind(s, (struct sockaddr *) &myname, sizeof(myname));





bind


Assigns a Name to a Socket



SYNOPSIS

#include <sys/types.h>
#include <sys/socket.h>


int bind(int s, const void *addr, int addrlen);

DESCRIPTION

bind assigns a name to an unnamed socket s . When a socket is created, it exists in an address family, but it does not have a name assigned to it. Servers use bind to associate themselves with a well-known port. Servers may also use bind to restrict access by other network addresses on a host with multiple network addresses. bind enables a connectionless client to have an address that the server can use for responses.

For addresses in the AF_INET family, addr points to a sockaddr or sockaddr_in structure. addrlen is the length of the address, in bytes. addrlen should be greater than or equal to the size of the sockaddr or sockaddr_in structure. The INADDR_ANY constant in the <netinet/in.h> header file specifies that the network address is not restricted. If the sin_port field of the sockaddr structure is zero, bind chooses a port. Alternatively, a well-known port number can be passed in the sin_port field. Internet host addresses and port numbers in sockaddr_in are always in network byte order. The remainder of the sockaddr or sockaddr_in structure should be 0 .

Upon return, if a port of 0 was specified, the selected port value is filled in. On return, the structure pointed to by addr should be the same as the structure pointed to by getsockname for this socket s .

RETURN VALUE

If bind is successful, it returns a 0 ; otherwise, it returns a -1 and sets errno to indicate the type of error.

PORTABILITY

bind is portable to other environments, including most UNIX systems, that implement BSD sockets.

EXAMPLE

In this example, bind assigns socket s to an arbitrary port without restriction by network address.

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
f()
{
   struct sockaddr_in sa;
   int s;
   struct servent *serv;
   .
   .
   .
      /* Specify the port. */
   memset(&sa,\'\0\',sizeof(sa));
   sa.sin_family = AF_INET;
   sa.sin_addr.s_addr = INADDR_ANY;
   sa.sin_port = serv->s_port;
   if (bind(s, &sa, sizeof(sa)) == -1) {
      perror("bind() failed");
      return -1;
   }
   .
   .
   .
      /* Let TCP/IP choose the port. */
   memset(&sa,\'\0\',sizeof(sa));
   sa.sin_family = AF_INET;
   sa.sin_addr.s_addr = INADDR_ANY;
   sa.sin_port = 0;
   if (bind(s, &sa, sizeof(sa)) == -1) {
      perror("bind() failed");
      return -1;
   }
   .
   .
   .

}








NAME

       bind - bind a name to a socket

SYNOPSIS

       #include <sys/types.h>          /* See NOTES */
       #include <sys/socket.h>

       int bind(int sockfd, const struct sockaddr *addr,
                socklen_t addrlen);


DESCRIPTION

       When  a  socket  is  created  with socket, it exists in a name space
       (address family) but has no address assigned to it.  bind() assigns the
       address  specified  to  by  addr  to the socket referred to by the file
       descriptor sockfd.  addrlen  specifies  the  size,  in  bytes,  of  the
       address structure pointed to by addr.  Traditionally, this operation is
       called “assigning a name to a socket”.

       It is normally necessary to assign a local address using bind()  before
       a SOCK_STREAM socket may receive connections.

       

       The  actual  structure  passed for the addr argument will depend on the
       address family.  The sockaddr structure is defined as something like:

           struct sockaddr {
               sa_family_t sa_family;
               char        sa_data[14];
           }

       The only purpose of this structure is to  cast  the  structure  pointer
       passed in addr in order to avoid compiler warnings.  See EXAMPLE below.

RETURN VALUE

       On success, zero is returned.  On error, -1 is returned, and  errno  is
       set appropriately.
 

EXAMPLE

      

       The following example shows how to bind a stream  socket  in  the  Unix
       (AF_UNIX) domain, and accept connections:

       #include <sys/socket.h>
       #include <sys/un.h>
       #include <stdlib.h>
       #include <stdio.h>
       #include <string.h>

       #define MY_SOCK_PATH "/somepath"
       #define LISTEN_BACKLOG 50

       #define handle_error(msg) \
           do { perror(msg); exit(EXIT_FAILURE); } while (0)

       int
       main(int argc, char *argv[])
       {
           int sfd, cfd;
           struct sockaddr_un my_addr, peer_addr;
           socklen_t peer_addr_size;

           sfd = socket(AF_UNIX, SOCK_STREAM, 0);
           if (sfd == -1)
               handle_error("socket");

           memset(&my_addr, 0, sizeof(struct sockaddr_un));
                               /* Clear structure */
           my_addr.sun_family = AF_UNIX;
           strncpy(my_addr.sun_path, MY_SOCK_PATH,
                   sizeof(my_addr.sun_path) - 1);

           if (bind(sfd, (struct sockaddr *) &my_addr,
                   sizeof(struct sockaddr_un)) == -1)
               handle_error("bind");

           if (listen(sfd, LISTEN_BACKLOG) == -1)
               handle_error("listen");

           /* Now we can accept incoming connections one
              at a time using accept(2) */

           peer_addr_size = sizeof(struct sockaddr_un);
           cfd = accept(sfd, (struct sockaddr *) &peer_addr,
                        &peer_addr_size);
           if (cfd == -1)
               handle_error("accept");

           /* Code to deal with incoming connection(s)... */

           /* When no longer required, the socket pathname, MY_SOCK_PATH
              should be deleted using unlink(2) or remove(3) */
       }




内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgfgyz.html