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

本文详细介绍了网络编程中关键的数据结构,包括sockaddr、in_addr_t、in_port_t、sockaddr_in等,这些结构用于定义互联网地址和服务信息。此外还描述了addrinfo结构,用于包含服务提供商地址信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

● 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;

};

● in_port_t

#include

/* Type to represent a port. */

typedef uint16_t in_port_t;

● sockaddr_in

#include

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

# defineh_addrh_addr_list[0] /* Address, for backward compatibility.*/

#endif

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值