[an error occurred while processing this directive]
HP OpenVMS SystemsC Programming Language |
Compaq C
|
Previous | Contents | Index |
Since the routines that set h_errno can also set errno , this section explains the relationship between the two variables.
A given routine failure does not set both h_errno and errno for the same failure. A routine failure sets either h_errno or errno depending on which is appropriate for the failing condition.
For example, consider the following program:
/* Demonstrate relationship between errno and h_errno */ /* from Appendix A of the reference manual */ #include <errno.h> #include <stdio.h> #include <netdb.h> main() { static char hostname[256]; struct hostent *hostentptr; errno = 0; h_errno = 0; if ((hostentptr = gethostbyname("hndy")) == NULL) { printf("unknown host name errno is: %d h_errno is: %d\n", errno, h_errn perror("p_gethostbyname"); herror("h_gethostbyname"); } errno = 0; h_errno = 0; if ((hostentptr = gethostbyname(0)) == NULL) { printf("illegal host name errno is: %d h_errno is: %d\n", errno, h_errn perror("p_gethostbyname"); herror("h_gethostbyname"); } } |
In the first call to gethostbyname , the host name parameter "hndy" does not represent a known host. In this case, gethostbyname sets h_errno to HOST_NOT_FOUND , but does not set errno .
The call to perror in this example would output:
p_gethostbyname: Error 0 |
The call to herror in this example would print a message describing the failure:
h_gethostbyname: Unknown |
In the second call to gethostbyname , the host name parameter is 0 (zero), an invalid argument. In this case, gethostbyname sets errno to EINVAL , but does not set h_errno .
A call to perror would print a message describing the failure:
p_gethostbyname: Invalid |
A call to herror would print:
h_gethostbyname: Error 0 |
The Compaq C RTL provides the language bindings for a 4.4BSD-compatible TCP/IP implementation. The underlying 4.4BSD semantics for the specific TCP/IP routines are the responsibility of the TCP/IP back-end vendors.
With 4.4BSD semantics, gethostbyname and gethostbyaddr set the external variable h_errno . Other routines (see Table A-3) deal with 4.4BSD versions of the sockaddr and msghdr data structures. See the <socket.h> header file for a description of these structures.
The TCP/IP backend routines need to know the following:
This information is controlled by the _SOCKADDR_LEN feature-test macro (see Section 1.5).
Compiling with _SOCKADDR_LEN defined will enable the entry points that have 4.4BSD semantics. See Table A-3. Otherwise, entry points expecting pre-4.4BSD semantics are enabled.
This section groups and briefly describes the socket routines. For complete descriptions, see the routine reference section that follows in this appendix.
Table A-4 lists the basic communication routines that make up the building blocks of Internet programs.
Table A-5 lists the auxiliary communication routines. These routines are used to provide information about a socket and to set the options on a socket.
Table A-6 lists the h_errno support routines. These routines map and write error-message strings generated by the external integer h_errno .
Routine | Description |
---|---|
herror | Writes a message explaining a routine error. |
hstrerror | Accesses message explaining routine errors. |
Table A-7 lists the communication support routines. These routines perform operations such as searching databases, converting byte order of network and host addresses, reading records, and returning Internet addresses.
Accepts a connection on a socket.
#include <socket.h>Routine Variants This socket routine has a variant named __bsd44_accept . Enabled by defining _SOCKADDR_LEN , this variant implements 4.4BSD-compatible semantics. See Section A.7 for more information.int accept (int s, struct sockaddr *addr, int *addrlen);
(_DECC_V4_SOURCE) int accept (int s, struct sockaddr *addr, size_t *addrlen);
(NOT _DECC_V4_SOURCE)
s
A socket descriptor that has been returned by socket , subsequently bound to an address with bind , and that is listening for connections after a listen .addr
A result parameter that is filled in with the address of the connecting entity, as known to the communications layer. The exact format of the structure to which the address parameter points is determined by the domain in which the communication is occurring. This version of Compaq C supports only the Internet domain (AF_INET).addrlen
A value-result parameter; it should initially contain the size of the structure pointed to by addr. On return it will contain the actual length, in bytes, of the structure that has been filled in by the communication layer. See <socket.h> for a description of the sockaddr structure.
This routine completes the first connection on the queue of pending connections, creates a new socket with the same properties as s, and allocates and returns a new descriptor for the socket. If no pending connections are present on the queue, and the socket is not marked as nonblocking, accept blocks the caller until a connection request is present. If the socket is marked nonblocking by using a setsockopt call and no pending connections are present on the queue, accept returns an error. The accepted socket may not be used to accept connections. The original socket s remains open (listening) for other connection requests. This call is used with connection-based socket types, currently with SOCK_STREAM.It is possible to select a socket for the purposes of performing an accept by selecting it for read.
See also bind , connect , listen , select , and socket in this section.
x A nonnegative integer that is a descriptor for the accepted socket. --1 Indicates an error; errno is set to one of the following:
- EBADF -- The socket descriptor is invalid.
- ENOTSOCK -- The socket descriptor references a file, not a socket.
- EOPNOTSUPP -- The reference socket is not of type SOCK_STREAM.
- EFAULT -- The addr parameter is not in a writable part of the user address space.
- EWOULDBLOCK -- The socket is marked nonblocking and no connections are present to be accepted.
Binds a name to a socket.
#include <socket.h>Routine Variants This socket routine has a variant named __bsd44_bind . Enabled by defining _SOCKADDR_LEN , this variant implements 4.4BSD-compatible semantics. See Section A.7 for more information.int bind (int s, struct sockaddr *name, int namelen);
(_DECC_V4_SOURCE) int bind (int s, const struct sockaddr *name, size_t namelen);
(NOT _DECC_V4_SOURCE)
s
A socket descriptor that has been created with socket .name
Address of a structure used to assign a name to the socket in the format specific to the family (AF_INET) socket address. See <socket.h> for a description of the sockaddr structure.namelen
The size, in bytes, of the structure pointed to by name.
This routine assigns a name to an unnamed socket. When a socket is created with socket it exists in a name space (address family) but has no name assigned. The bind routine requests that a name be assigned to the socket.See also connect , getsockname , listen , and socket in this appendix.
0 Indicates success. --1 Indicates an error; errno is set to one of the following values:
- EBADF -- The socket descriptor is invalid.
- ENOTSOCK -- The socket descriptor references a file, not a socket.
- EADDRNOTAVAIL -- specified address is not available from the local machine.
- EADDRINUSE -- The specified Internet address and ports are already in use.
- EINVAL -- The socket is already bound to an address.
- EACCESS -- The requested address is protected, and the current user has inadequate permission to access it.
- EFAULT -- The name parameter is not a valid part of the user address space.
Previous | Next | Contents | Index |