socket编程头文件分析

Posted yuxi_o

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了socket编程头文件分析相关的知识,希望对你有一定的参考价值。

在socket网络编程中经常用到一些宏定义、结构和函数,这些经常包含在相关的头文件中,使用时直接include相关头文件即可。下面简单描述下相关的一些结构及头文件。

1. sockaddr  / bits/socket.h

socket编程最基本的就是socket地址,其定义在bits/socket.h中。但sys/socket.h包含bits/socket.h,且bits/socket.h明确指出该头文件不能直接使用,应使用sys/socket.h代替。

struct sockaddr
  {
    __SOCKADDR_COMMON (sa_);    /* Common data: address family and length.  */
    char sa_data[14];       /* Address data.  */
  };

其中__SOCKADDR_COMMON()宏定义在bits/sockaddr.h中

#define __SOCKADDR_COMMON(sa_prefix) \
  sa_family_t sa_prefix##family

#define __SOCKADDR_COMMON_SIZE  (sizeof (unsigned short int))

替换后为

struct sockaddr{
    unsigned short sa_family;
    char sa_data[14];
};

在bits/socket.h中还定义了协议族(Protocol families,PF_xxx)和地址族(Address families,AF_xx),两者一一对应,因此可以互换,应用场合根据概念稍有不同。

#define PF_UNSPEC   0   /* Unspecified.  */
#define PF_LOCAL    1   /* Local to host (pipes and file-domain).  */
#define PF_UNIX     PF_LOCAL /* POSIX name for PF_LOCAL.  */
#define PF_FILE     PF_LOCAL /* Another non-standard name for PF_LOCAL.  */
#define PF_INET     2   /* IP protocol family.  */
#define PF_AX25     3   /* Amateur Radio AX.25.  */
#define PF_IPX      4   /* Novell Internet Protocol.  */
#define PF_APPLETALK    5   /* Appletalk DDP.  */

#define PF_INET6    10  /* IP version 6.  */

#define PF_LLC      26  /* Linux LLC.  */
#define PF_CAN      29  /* Controller Area Network.  */

#define PF_NFC      39  /* NFC sockets.  */
#define PF_VSOCK    40  /* vSockets.  */
#define PF_MAX      41  /* For now..  */

2. sys/socket.h

该头文件包含常用的socket编程函数接口,如:

socket() bind() listen() connect() accept() shutdown()

send() recv() sendto() recvfrom() sendmsg() recvmsg() sendmmsg() recvmmsg()

socketpair() getsockname() getpeername()

getsockopt() setsockopt()

除此外,sys/socket.h包含bits/socket.h,需要bits/socket.h的函数直接include sys/socket.h即可。

3. 网络socket地址 sockaddr_in -- netinet/in.h

与internet网络相关的头文件大部分在netinet目录下,如in.h ip.h tcp.h udp.h ip_icmp.h ip6.h icmp6.h等。

在网络编程中用到的socket地址是经过转换的网络socket地址sockaddr_in:

/* Structure describing an Internet socket address.  */
struct sockaddr_in
  {
    __SOCKADDR_COMMON (sin_);    /* unsigned short sin_family; */
    in_port_t sin_port;         /* Port number.  */
    struct in_addr sin_addr;        /* Internet address.  */

    /* Pad to size of `struct sockaddr‘.  */
    unsigned char sin_zero[sizeof (struct sockaddr) -
               __SOCKADDR_COMMON_SIZE -
               sizeof (in_port_t) -
               sizeof (struct in_addr)];
  };

网络地址

/* Internet address.  */
typedef uint32_t in_addr_t;
struct in_addr
  {
    in_addr_t s_addr;
  };  

网络端口

/* Type to represent a port.  */
typedef uint16_t in_port_t;

netinet/in.h定义了标准协议enum:

enum
  {
    IPPROTO_IP = 0,    /* Dummy protocol for TCP.  */
#define IPPROTO_IP      IPPROTO_IP
    IPPROTO_ICMP = 1,      /* Internet Control Message Protocol.  */
#define IPPROTO_ICMP        IPPROTO_ICMP
    IPPROTO_IGMP = 2,      /* Internet Group Management Protocol. */
#define IPPROTO_IGMP        IPPROTO_IGMP
    IPPROTO_IPIP = 4,      /* IPIP tunnels (older KA9Q tunnels use 94).  */
#define IPPROTO_IPIP        IPPROTO_IPIP
    IPPROTO_TCP = 6,       /* Transmission Control Protocol.  */
#define IPPROTO_TCP     IPPROTO_TCP
    IPPROTO_EGP = 8,       /* Exterior Gateway Protocol.  */
#define IPPROTO_EGP     IPPROTO_EGP
    IPPROTO_PUP = 12,      /* PUP protocol.  */
#define IPPROTO_PUP     IPPROTO_PUP
    IPPROTO_UDP = 17,      /* User Datagram Protocol.  */
#define IPPROTO_UDP     IPPROTO_UDP
    IPPROTO_IDP = 22,      /* XNS IDP protocol.  */

    IPPROTO_IPV6 = 41,     /* IPv6 header.  */
#define IPPROTO_IPV6 IPPROTO_IPV6
    IPPROTO_RAW = 255,     /* Raw IP packets.  */
#define IPPROTO_RAW     IPPROTO_RAW
    IPPROTO_MAX };

标准端口enum

enum
  {
    IPPORT_ECHO = 7,        /* Echo service.  */
    IPPORT_DISCARD = 9,     /* Discard transmissions service.  */
    IPPORT_SYSTAT = 11,     /* System status service.  */
    IPPORT_DAYTIME = 13,    /* Time of day service.  */
    IPPORT_NETSTAT = 15,    /* Network status service.  */
    IPPORT_FTP = 21,        /* File Transfer Protocol.  */
    IPPORT_TELNET = 23,     /* Telnet protocol.  */
    IPPORT_SMTP = 25,       /* Simple Mail Transfer Protocol.  */
    IPPORT_TIMESERVER = 37, /* Timeserver service.  */
    IPPORT_NAMESERVER = 42, /* Domain Name Service.  */
    IPPORT_WHOIS = 43,      /* Internet Whois service.  */
    IPPORT_MTP = 57,

    IPPORT_TFTP = 69,       /* Trivial File Transfer Protocol.  */
    IPPORT_RJE = 77,
    IPPORT_FINGER = 79,     /* Finger service.  */
    IPPORT_TTYLINK = 87,
    IPPORT_SUPDUP = 95,     /* SUPDUP protocol.  */


    IPPORT_EXECSERVER = 512,    /* execd service.  */
    IPPORT_LOGINSERVER = 513,   /* rlogind service.  */
    IPPORT_CMDSERVER = 514,
    IPPORT_EFSSERVER = 520,

    /* UDP ports.  */
    IPPORT_BIFFUDP = 512,
    IPPORT_WHOSERVER = 513,
    IPPORT_ROUTESERVER = 520,

    /* Ports less than this value are reserved for privileged processes.  */
    IPPORT_RESERVED = 1024,

    /* Ports greater this value are reserved for (non-privileged) servers.  */
    IPPORT_USERRESERVED = 5000
  };

 

以上是关于socket编程头文件分析的主要内容,如果未能解决你的问题,请参考以下文章

在windows下用C语言如何实现socket网络编程,需要用到哪些头文件或者库?

请求达人讲解一下下面一段socket代码

QT编程可以用sys/socket.h这个头文件吗,怎么我的QT5找不到这个头文件,我的是wind

结对编程1

vs2008和socket编程

Java Socket编程 简单的WEB服务器