The Linux Programming Interface

(nextflipdebug5) #1

1226 Chapter 59


Listing 59-8: Header file for inet_sockets.c
––––––––––––––––––––––––––––––––––––––––––––––––––––sockets/inet_sockets.h
#ifndef INET_SOCKETS_H
#define INET_SOCKETS_H /* Prevent accidental double inclusion */

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

int inetConnect(const char *host, const char *service, int type);

int inetListen(const char *service, int backlog, socklen_t *addrlen);

int inetBind(const char *service, int type, socklen_t *addrlen);

char *inetAddressStr(const struct sockaddr *addr, socklen_t addrlen,
char *addrStr, int addrStrLen);

#define IS_ADDR_STR_LEN 4096
/* Suggested length for string buffer that caller
should pass to inetAddressStr(). Must be greater
than (NI_MAXHOST + NI_MAXSERV + 4) */
#endif
––––––––––––––––––––––––––––––––––––––––––––––––––––sockets/inet_sockets.h
The inetConnect() function creates a socket with the given socket type, and connects
it to the address specified by host and service. This function is designed for TCP or
UDP clients that need to connect their socket to a server socket.

The file descriptor for the new socket is returned as the function result.
The inetListen() function creates a listening stream (SOCK_STREAM) socket bound
to the wildcard IP address on the TCP port specified by service. This function is
designed for use by TCP servers.

The file descriptor for the new socket is returned as the function result.
The backlog argument specifies the permitted backlog of pending connections
(as for listen()).

#include "inet_sockets.h"

int inetConnect(const char *host, const char *service, int type);
Returns a file descriptor on success, or –1 on error

#include "inet_sockets.h"

int inetListen(const char *service, int backlog, socklen_t *addrlen);
Returns a file descriptor on success, or –1 on error
Free download pdf