UNP函数笔记一: 套接字编程简介
来源:互联网 发布:上古卷轴5优化字体 编辑:程序博客网 时间:2024/04/28 11:50
第三章 套接字编程简介:
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_LENstruct 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 0in_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 -1const 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_twriten(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 1024static 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);}
- UNP函数笔记一: 套接字编程简介
- UNP学习笔记(第三章:套接字编程简介)
- UNP卷一学习笔记:基本UDP套接字编程
- UNP函数笔记二: 基本TCP套接字编程
- UNP函数笔记六: 基本UDP套接字编程
- UNP函数笔记七: 基本SCTP套接字编程
- UNP---套接字简介
- UNP函数笔记五: 套接字选项
- UNP总结 Chapter 3 套接字编程简介
- UNP卷1:第三章(套接字编程简介)
- UNP——Chapter 3:套接字编程简介
- UNP笔记(2)——基本套接字编程
- UNP卷一学习笔记:基本TCP套接字
- 【UNP学习笔记】套接字地址相关函数
- 套接字编程简介<一>
- UNP学习:基本套接字编程
- UNP《网络编程》笔记 第一章 简介
- UNP学习笔记(第四章 基本TCP套接字编程)
- IE6下部分样式无法加载问题以及连接块状背景切换
- 修改 ios 状态栏
- C# 连接到sql server 2005
- Linux C解析xml
- Spring MVC使用Freemaker 中文乱码
- UNP函数笔记一: 套接字编程简介
- db2创建用户及授权的研究
- 基于词表和N-gram算法的新词识别实验
- 智能指针以及智能指针类
- SSD新手必读!合理设置发挥出最强性能
- infoQ 百度技术沙龙第25期回顾:海量数据处理技术解析
- NSPredicate的用法
- case用法
- Android地图开发:自定义地图弹出框内容