第三章 套接字编程简介:
struct in_addr {
in_addr_t s_addr;
};
struct sockaddr_in {
uint8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
struct sockaddr {
uint8_t sa_len;
sa_family_t sa_family;
char sa_data[14];
};
struct in6_addr {
uint8_t s6_addr[16];
};
#define SIN6_LEN
struct sockaddr_in6 {
uint8_t sin6_len;
sa_family_t sin6_family;
in_port_t sin6_port;
uint32_t sin6_flowinfo;
struct in6_addr sin6_addr;
uint32_t sin6_scope_id;
};
struct sockaddr_storage {
uint8_t ss_len;
sa_family_t ss_family;
...
};
#include <netinet/in.h>
uint16_t htons(uint16_t host16bitvalue);
uint32_t htonl(uint32_t host32bitvalue);
uint16_t ntohs(uint16_t net16bitvalue);
uint32_t ntohl(uint32_t net32bitvalue);
#include <strings.h>
void bzero(void * dest, size_t nbytes);
void bcopy(const void * src, void * dest, size_t nbytes);
int bcmp(const void * ptr1, const void * ptr2, size_t nbytes);
#include <string.h>
void * memset(void * dest, int c, size_t len);
void * memcpy(void * dest, const void * src, size_t nbytes);
int memcmp(const void * ptr1, const void * ptr2, size_t nbytes);
#include <arpa/inet.h>
int inet_aton(const char * strptr, struct in_addr * addrptr);
success return 1, error return 0
in_addr_t inet_addr(const char * strptr);
success return net-order-ipv4-addr, error return INADDR_NONE(~0)
char * inet_ntoa(struct in_addr inaddr);
#include <arpa/inet.h>
int inet_pton(int family, const char * strptr, void * addrptr);
success return 1, invalid strptr return 0, error return -1
const char * inet_ntop(int family, const void * addrptr,
char * strptr, size_t len);
#define INET_ADDRSTRLEN 16
#define INET6_ADDRSTRLEN 46
示例:
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char * argv[])
{
union {
short s;
char c[sizeof(short)];
} un;
un.s = 0x0102;
if (sizeof(short) == 2) {
if (un.c[0] == 1 && un.c[1] == 2) {
printf("big-endian\n");
}
else if (un.c[0] == 2 && un.c[1] == 1) {
printf("little-endian\n");
}
else {
printf("unknown\n");
}
}
else {
printf("sizeof(short) = %d\n", sizeof(short));
}
exit(0);
}
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
int
inet_pton(int family, const char *strptr, void *addrptr)
{
if (family == AF_INET) {
struct in_addr in_val;
if (inet_aton(strptr, &in_val)) {
memcpy(addrptr, &in_val, sizeof(struct in_addr));
return(1);
}
return(0);
}
errno = EAFNOSUPPORT;
return(-1);
}
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
const char *
inet_ntop(int family, const void * addrptr, char * strptr, size_t len)
{
const u_char * p = (const u_char *) addrptr;
if (family == AF_INET) {
char temp[INET_ADDRSTRLEN];
snprintf(temp, sizeof(temp), "%d.%d.%d.%d",
p[0], p[1], p[2], p[3]);
if (strlen(temp) >= len) {
errno = ENOSPC;
return(NULL);
}
strcpy(strptr, temp);
return(strptr);
}
errno = EAFNOSUPPORT;
return(NULL);
}
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
char *
sock_ntop(const struct sockaddr * sa, socklen_t salen)
{
char portstr[8];
static char str[128]; /* Unix domain is largest */
switch (sa->sa_family)
{
case AF_INET:
{
struct sockaddr_in * sin = (struct sockaddr_in *) sa;
if (inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str)) == NULL) {
return(NULL);
}
if (ntohs(sin->sin_port) != 0) {
snprintf(portstr, sizeof(portstr), ":%d", ntohs(sin->sin_port));
}
strcat(str, portstr);
return(str);
}
}
return (NULL);
}
#include <fcntl.h>
#include <errno.h>
ssize_t
readn(int fd, void * vptr, size_t n)
{
size_t nleft;
ssize_t nread;
char * ptr;
ptr = vptr;
nleft = n;
while (nleft > 0) {
if ((nread = read(fd, ptr, nleft)) < 0) {
if (errno == EINTR) {
nread = 0;
}
else {
return(-1);
}
}
else if (nread == 0) {
break;
}
nleft -= nread;
ptr += nread;
}
return(n - nleft);
}
#include <fcntl.h>
#include <errno.h>
ssize_t
writen(int fd, const void * vptr, size_t n)
{
size_t nleft;
ssize_t nwritten;
const char * ptr;
ptr = vptr;
nleft = n;
while (nleft > 0) {
if ((nwritten = write(fd, ptr, nleft)) <= 0) {
if (nwritten < 0 && errno == EINTR)
nwritten = 0;
else
return(-1);
}
nleft -= nwritten;
ptr += nwritten;
}
return(n);
}
#include <fcntl.h>
#include <errno.h>
ssize_t
readline(int fd, void * vptr, size_t maxlen)
{
ssize_t n;
ssize_t rc;
char c;
char * ptr;
ptr = vptr;
for (n = 1; n < maxlen; n++) {
again:
if ((rc = read(fd, &c, 1)) == 1) {
*ptr++ = c;
if (c == '\n') {
break;
}
}
else if (rc == 0) {
*ptr = 0;
return(n - 1);
}
else {
if (errno == EINTR) {
goto again;
}
return(-1);
}
}
*ptr = 0;
return(n);
}
#include <fcntl.h>
#include <errno.h>
#define MAXLINE 1024
static int read_cnt;
static char * read_ptr;
static char read_buf[MAXLINE];
static ssize_t
my_read(int fd, char * ptr)
{
if (read_cnt <= 0) {
again:
if ((read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0) {
if (errno == EINTR) {
goto again;
}
return(-1);
}
else if (read_cnt == 0) {
return(0);
}
read_ptr = read_buf;
}
read_cnt--;
*ptr = *read_ptr++;
return(1);
}
ssize_t
readline(int fd, void * vptr, size_t maxlen)
{
ssize_t n;
ssize_t rc;
char c;
char * ptr;
ptr = vptr;
for (n = 1; n < maxlen; n++) {
if ((rc = my_read(fd, &c)) == 1) {
*ptr++ = c;
if (c == '\n') {
break;
}
}
else if (rc == 0) {
*ptr = 0;
return(n - 1);
}
else {
return(-1);
}
}
*ptr = 0;
return(n);
}
ssize_t
readlinebuf(void ** vptrptr)
{
if (read_cnt) {
*vptrptr = read_ptr;
}
return(read_cnt);
}