网络编程的头文件

网络编程的头文件(这里所有的头文件都在/usr/include目录下面)
经常被一些头文件搞大,不知到到哪个头文件去找结构。这里做个总结。

ip头部 
有两个ip头部结构,分别是 
linux/ip.h 下面结构名字struct iphdr
netinet/ip.h 结构名字,这里有两个struct iphdr 和 struct ip 
这三个都是一样的,看个人喜好,我个人喜换netinet/ip.h下面的struct iphdr;

tcp头部
两个
linux/tcp.h  struct tcphdr
netinet/tcp.h struct tcphdr
任选,不过网络编程的一般在netinet这个地下选,系统变成的一般去linux目录。


udp头部
两个
linux/udp.h struct udphdr
netinet/udp.h struct udphdr

icmp头部
linux/icmp.h struct icmphdr
netinet/ip_icmp.h struct icmphdr    

arp头部
linux/if_arp.h struct arphdr
net/if_arp.h   struct arphdr  // 主要不是netinet目录了,net。

以太网头部
linux/if_ether.h  struct ethhdr
原则上在netinet的if_ether.h 文件中没有定义struct ethhdr,但是,在这个文件中,
它引用了上面的文件linux/if_ether.h,所以也可也引用这个文件来定义 struct ethhdr.
这样就统一了。
值得说明的是,netinet/if_ether.h中定义了struct ether_arp ,它是整个arp包(包括arp首部)的定义。

以上就是各个数据包的头部格式,这在编写构造自己的包时候很有用。

另外,还有些头文件。
netinet/in.h  定义了ip地址结构 struct sockaddr_in (struct in_addr作为sockaddr_in 的成员) 还有函数 htons等,以及一些宏。
在编写用户应用程序时,是少不了这个头文件的(因为要构造ip地址呀)

arpa/inet.h  定义了地址转换函数  inet_ntoa inet_aton inet_pton 等等。(不过实现在别处)。

sys/socket.h 定义了一些列的socket API函数 如 socket,bind listen , send ,sendmsg,setsockopt,等等。所以这个头文件和netinet/in.h头文件是用户程序不可缺少的。


......................................................................
#include <linux/udp.h>
//udp
struct udphdr
{
    u_int16_t source;
    u_int16_t dest;
    u_int16_t len;
    u_int16_t check;
};

........................................................................
#include <linux/icmp.h>
struct icmphdr
{
    u_int8_t type;//0--reques,8---reply,11----timeout;
    u_int8_t code;
    u_int16_t checksum;
    ........
};
..........................................................................
#include <linux/ip.h>
struct iphdr
{
#if defined(__LITTLE_ENDIAN_BITFIELD)
    __u8    ihl:4,
        version:4;
#elif defined (__BIG_ENDIAN_BITFIELD)
    __u8    version:4,
          ihl:4;
#else
#error    "Please fix <asm/byteorder.h>"
#endif
    __u8    tos;
    __be16    tot_len;
    __be16    id;
    __be16    frag_off;
    __u8    ttl;
    __u8    protocol; // 1--icmp 6---tcp 17---udp
    __sum16    check;
    __be32    saddr;
    __be32    daddr;
    /*The options start here. */
};
.........................................................................
#include <linux/tcp.h>
struct tcphdr {
    __be16    source;
    __be16    dest;
    __be32    seq;
    __be32    ack_seq;
#if defined(__LITTLE_ENDIAN_BITFIELD)
    __u16    res1:4,
        doff:4,
        fin:1,
        syn:1,
        rst:1,
        psh:1,
        ack:1,
        urg:1,
        ece:1,
        cwr:1;
#elif defined(__BIG_ENDIAN_BITFIELD)
    __u16    doff:4,
        res1:4,
        cwr:1,
        ece:1,
        urg:1,
        ack:1,
        psh:1,
        rst:1,
        syn:1,
        fin:1;
#else
#error    "Adjust your <asm/byteorder.h> defines"
#endif    
    __be16    window;
    __sum16    check;
    __be16    urg_ptr;
}

.........................................................................
#include <linux/if_ether.h>
struct ethhdr
{
    unsigned char h_dest[6];
    unsigned char h_source[6];
    __be16       h_proto;//0x0806---arp,0x0800--ip,0x0835---rarp.
}
.........................................................................



#include <linux/...//暂时没找到位置>
struct arphdr
{
    unsigned short hard_type;//硬件类型,通常0x0001(以太网)
    unsigned short protocol;//协议类型,通常0x0800(IP)
    unsigned char hard_length;
    .....
    unsigned short operation_type;//操作类型,1,ARP请求,2,ARP应答
    //参考http://baike.baidu.com/view/121586.htm?fr=aladdin
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Qt中进行网络编程,主要涉及到的头文件有**<QTcpSocket>和<QTcpServer>**。以下是一些关于Qt网络编程的信息: 1. **客户端编程**: - **创建QTcpSocket对象**:用于建立与服务器的连接。 - **连接至服务器**:使用`connectToHost()`函数连接到指定的服务器。 - **发送数据**:通过`write()`函数来发送数据到服务器。 - **读取数据**:使用`readAll()`函数来接收来自服务器的数据。 - **断开连接**:完成数据传输后,需要断开与服务器的连接。 2. **服务器端编程**: - **创建QTcpServer对象**:用于监听客户端的连接请求。 - **绑定本地信息**:通过调用`listen()`函数并指定地址和端口来开始监听。 - **等待客户端连接**:当有新的连接请求时,`newConnection()`信号会被触发。 - **读取和发送数据**:与客户端类似,服务器也需要读取和发送数据。 - **断开连接**:结束客户端的连接。 3. **头文件引入**: - 对于客户端,需要包含`#include <QTcpSocket>`。 - 对于服务器,需要包含`#include <QTcpServer>`。 4. **项目文件配置**: - 在项目的`.pro`文件中,需要添加`QT += network`以启用网络模块。 5. **数据流操作**: - 在进行数据收发时,可以使用`QDataStream`类来处理数据流,它提供了一种方便的方式来序列化和反序列化数据。 综上所述,这些是Qt网络编程的基本概念和步骤。在实际开发中,还需要考虑到错误处理、多线程管理以及安全性等问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值