网络编程
文章平均质量分 71
duyiwuer2009
这个作者很懒,什么都没留下…
展开
-
IP地址转换函数——inet_ntop(),inet_pton(),htonl(),ntohl()
1、点分十进制字符串(dotted-decimal notation)与二进制数值互转const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);int inet_pton(int af, const char *src, void *dst);注意:(1) 二进制数值形式是网络字节序(network原创 2012-08-15 20:29:47 · 7557 阅读 · 0 评论 -
单个进程监听多个端口及多个进程监听同一个端口
单个进程监听多个端口单个进程创建多个 socket 绑定不同的端口,TCP, UDP 都行多个进程监听同一个端口(multiple processes listen on same port)通过 fork 创建子进程的方式可以实现,其他情况下不行。当连接到来时,子进程、父进程都可以 accept, 但如果都去处理则会出现问题,造成数据不完整。NGINX 的 m原创 2015-01-28 12:46:29 · 17143 阅读 · 0 评论 -
主机名、服务与地址的映射——getaddrinfo(),getnameinfo(),gai_strerror()
/* Structure to contain information about address of a service provider. */struct addrinfo{ int ai_flags; /* Input flags. */ int ai_family; /* Protocol family for socket. */ int ai_sockt原创 2012-08-16 22:27:11 · 17430 阅读 · 1 评论 -
Linux C socket编程中几个常见的结构体定义
● sockaddr#include struct sockaddr{ sa_family_t sa_family; char sa_data[14];};● in_addr_t#include /* Internet address. */typedef uint32_t in_addr_t;struct in_addr{ in_addr_t s_addr;原创 2012-08-13 16:32:28 · 2745 阅读 · 1 评论 -
获取协议名、协议号——getprotoent(),getprotobyname(),getprotobynumber()
/* Description of data base entry for a single service. */struct protoent{ char *p_name; /* Official protocol name. */ char **p_aliases; /* Alias list. */ int p_proto; /* Protocol numb原创 2012-08-16 10:21:59 · 4089 阅读 · 0 评论 -
poll()例程(包含 bind,listen,accept,setsockopt 的用法)
Using poll() instead of select()The poll() API is part of the Single Unix Specification and the UNIX® 95/98 standard. The poll() API performs the same API as the existing select() API. The only原创 2012-10-20 17:12:42 · 2788 阅读 · 0 评论 -
常见协议头结构体定义
环境说明:Intel 小端存储Linux Ubuntu-Server 2.6.38-8-generic-pae #42-Ubuntu SMP Mon Apr 11 05:17:09 UTC 2011 i686 i686 i386 GNU/Linux● Ethernet header#include /* This is a name for the 48 bit et原创 2012-10-21 21:31:57 · 2535 阅读 · 1 评论 -
获取网络号、网络名——getnetent(),getnetbyaddr(),getnetbyname()
struct netent *getnetent(void);struct netent *getnetbyname(const char *name);struct netent *getnetbyaddr(uint32_t net, int type);void setnetent(int stayopen);void endnetent(void);/* Description原创 2012-08-15 21:39:45 · 4655 阅读 · 0 评论 -
获取主机名、IP——gethostent(),gethostbyname(),gethostbyaddr()
1、原理:查询 /etc/hosts 等文件及 DNS or NIS 服务器The domain name queries carried out by gethostbyname() and gethostbyaddr() use a combination of any or all of the name server named(8), a broken out line fro原创 2012-08-13 21:21:20 · 8649 阅读 · 1 评论 -
getaddrinfo(),gethostbyname(),getnetbyaddr(),getservbyname(),getprotobyname()对比总结
1. gethostbyname()原理:查询 /etc/hosts 等文件及 DNS or NIS 服务器The domain name queries carried out by gethostbyname() and gethostbyaddr() use a combination of any or all of the name server named(8), a b原创 2012-09-22 21:55:06 · 2645 阅读 · 0 评论 -
socket()参数中的地址族、套接字类型、协议号的取值
int socket(int domain, int type, int protocol);1、domain(地址族)和 type(套接字类型)的取值可以见 man socket2、关于 protocol(协议号)的取值可以见 netinet/in.h(可以结合 /etc/protocols看)也可以看这里:socket()--Create Socket, http://publib原创 2012-09-29 23:47:32 · 1711 阅读 · 0 评论 -
Linux socket-多进程面向连接的服务器客户端程序
总结:1.、TCP的地址复用(address reuse)问题2、HOST_NAME_MAX 的处理方法3、多进程socket编程中 close() 和 shutdown() 的问题4、gethostname() 的问题5、getaddrinfo(hostname, "ruptime", &hint, &ailist) 中 "ruptime" 服务的问题("Servname n原创 2012-09-28 11:25:02 · 3305 阅读 · 0 评论 -
获取服务名、服务端口号、服务所用协议——getservent(),getservbyname(),getservbyport()
Services are represented by the port number portion of the address. Each service is offered on a unique, well-known port number. We can map a service name to a port number withgetservbyname, map a p原创 2012-08-16 10:01:04 · 4460 阅读 · 2 评论