|
» |
|
|
|
| | |
C Interface | |
AF_UNIX only:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
bind(s, addr, addrlen)
int s;
struct sockaddr_un *addr;
int addrlen;
AF_INET only:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
bind(s, addr, addrlen)
int s;
struct sockaddr_in *addr;
int addrlen;
|
Description | |
The bind system call assigns an address to an unbound socket.
When a socket is created with socket, it exists in an
address space (address family) but has no address assigned. The bind
call causes the socket whose descriptor is s to become bound to the address specified in the socket address structure pointed to
by addr. The addrlen parameter must specify the size
of the address structure. Since the size of the socket address structure
varies between socket address families (16 bytes for AF_INET;
110 bytes for AF_UNIX), the correct socket address structure
should be used with each address family (struct sockaddr_in
for AF_INET; struct sockaddr_un for AF_UNIX). Here is the socket address structure for AF_INET, extracted
from the IN.H.SYS file:
struct in_addr {
union {
struct { u_char s_b1,s_b2,s_b3,s_b4; } s_un_b;
struct { u_short s_w1,s_w2; } S_un_w;
u_long S_addr;
} S_un;
#define s_addr S_un.S_addr /* can be used for most tcp & ip code
*/
#define s_host S_un.S_un_b.s_b2 /* host on imp */
#define s_net S_un.S_un_b.s_b1 /* network */
#define s_imp S_un.S_un_w.s_w2 /* imp */
#define s_impno S_un.S_un_b.s_b4 /* imp # */
#define s_lh S_un.S_un_b.s_b3 /* logical host */
};
#define INADDR_ANY (u_long)0x00000000
#define INADDR_THISHOST (u_long)0x00000000
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
|
Here is the socket address structure for AF_UNIX, extracted
from the UN.H.SYS file:
struct sockaddr_un {
short sun_family; /* AF_UNIX */
char sun_path[108]; /* path name */
};
|
Examples | |
Here is an example program to create and bind an AF_UNIX
socket:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <sys/un.h>
main ()
{
int s;
int af, type, protocol;
struct sockaddr_un addr;
int addrlen;
s = socket (AF_UNIX, SOCK_STREAM, 0);
addr.sun_family = AF_UNIX;
strcpy (addr.sun_path,"tmp/socket");
addrlen = 110;
bind (s,addr, addrlen);
}
|
Return Value | |
If the bind is successful, a 0 value is returned. If
it fails, -1 is returned, and an error code is stored in errno. Errors | |
The following errors are returned by bind: - [EBADF]
The argument s is not a valid descriptor.
- [ENOTSOCK]
The argument s is not a socket.
- [EADDRNOTAVAIL]
The specified address is bad or is not available from
the local machine.
- [EADDRINUSE]
The specified address is already in use.
- [EINVAL]
The socket is already bound to an address, the socket
has been shut down or addrlen is a bad value.
- [EAFNOSUPPORT]
The requested address does not match the address family
of this socket.
- [EACCES]
The requested address is protected, and the current user
has inadequate permission to access it. (This error can be returned
by AF_INET only.)
- [EFAULT]
The addr parameter is not a valid pointer.
- [EOPNOTSUPP]
The socket whose descriptor is s is of a type
that does not support address binding.
- [ENOBUFS]
Insufficient buffer memory is available. The bind
cannot complete.
- [EDESTADDREQ]
No addr parameter was specified.
- [ENETUNREACH]
The network is not reachable from this host.
MPE/iX Specific | |
On HP-UX, when binding an AF_UNIX socket to a path name
(such as /tmp/mysocket, an open file having that name
is created in the file system. When the bound socket is closed, that
file still exists unless it is removed or unlinked. This does not
occur on MPE/iX (that is, no file is created). On HP-UX, you are allowed to specify a specific network while binding. MPE/iX
does not. The IP address portion of sockaddr for AF_INET
must be zero. Author | |
UCB (University of California at Berkeley)
See Also | |
connect, listen, socket, getsockname,
Name Service Routines
|