Linux C socket编程中几个常见的结构体定义

● sockaddr
#include	<sys/socket.h>
struct sockaddr
{
	sa_family_t sa_family;
	char sa_data[14];
};


● in_addr_t
#include	<netinet/in.h>
/* Internet address.  */
typedef uint32_t in_addr_t;
struct in_addr
{
	in_addr_t s_addr;
};

● in_port_t
#include	<netinet/in.h>
/* Type to represent a port.  */
typedef uint16_t in_port_t;

● sockaddr_in
#include	<netinet/in.h>
struct sockaddr_in
{
	sa_family_t sin_family;
	in_port_t sin_port;
	struct in_addr sin_addr;


	unsigned char sin_zero[sizeof (struct sockaddr) -
		(sizeof (unsigned short int)) -
		sizeof (in_port_t) -
		sizeof (struct in_addr)];
};

● addrinfo, servent, protoent, netent, hostent
/* 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_socktype;		/* Socket type.  */
	int ai_protocol;		/* Protocol for socket.  */
	socklen_t ai_addrlen;		/* Length of socket address.  */
	struct sockaddr *ai_addr;	/* Socket address for socket.  */
	char *ai_canonname;		/* Canonical name for service location.  */
	struct addrinfo *ai_next;	/* Pointer to next in list.  */
};


/* Description of data base entry for a single service.  */
struct servent
{
	char *s_name;			/* Official service name.  */
	char **s_aliases;		/* Alias list.  */
	int s_port;			/* Port number.  */
	char *s_proto;		/* Protocol to use.  */
};


/* 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 number.  */
};




/* Description of data base entry for a single network.  NOTE: here a
   poor assumption is made.  The network number is expected to fit
   into an unsigned long int variable.  */
struct netent
{
	char *n_name;			/* Official name of network.  */
	char **n_aliases;		/* Alias list.  */
	int n_addrtype;		/* Net address type.  */
	uint32_t n_net;		/* Network number.  */
};




/* Description of data base entry for a single host.  */
struct hostent
{
	char *h_name;			/* Official name of host.  */
	char **h_aliases;		/* Alias list.  */
	int h_addrtype;		/* Host address type.  */
	int h_length;			/* Length of address.  */
	char **h_addr_list;		/* List of addresses from name server.  */
#if defined __USE_MISC || defined __USE_GNU
# define	h_addr	h_addr_list[0] /* Address, for backward compatibility.*/
#endif
};







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值