Chinaunix首页 | 论坛 | 博客
  • 博客访问: 117677
  • 博文数量: 38
  • 博客积分: 2015
  • 博客等级: 大尉
  • 技术积分: 405
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-26 17:19
文章分类

全部博文(38)

文章存档

2011年(4)

2010年(34)

我的朋友

分类: C/C++

2011-06-10 11:45:13

IP结构体的使用。
struct in_addr

struct in_addr {

union {   

struct {     

u_char s_b1,s_b2,s_b3,s_b4;   

} S_un_b;      //An IPv4 address formatted as four u_chars.  

struct {     

u_short s_w1,s_w2;   

} S_un_w;   //An IPv4 address formatted as two u_shorts

u_long S_addr; //An IPv4 address formatted as a u_long

} S_un;

};


#include
#include
#include

#pragma comment(lib, "ws2_32.lib")

int main()
{
    in_addr addr;
    addr.S_un.S_un_b.s_b1 = 222;
    addr.S_un.S_un_b.s_b2 = 205;
    addr.S_un.S_un_b.s_b3 = 41;
    addr.S_un.S_un_b.s_b4 = 160;
    printf("%s\n", inet_ntoa(addr)); //222.205.41.160

    in_addr addr1;
    addr1.S_un.S_un_w.s_w1 = htons((222 << 8) + 205);
    addr1.S_un.S_un_w.s_w2 = htons((41 << 8) + 161);
    printf("%s\n", inet_ntoa(addr1)); //222.205.41.161

    in_addr addr2;
    addr2.S_un.S_addr = htonl((((((222 << 8) + 205) << 8) + 41) << 8) + 162);
    printf("%s\n", inet_ntoa(addr2)); //222.205.41.162
}


阅读(6132) | 评论(0) | 转发(0) |
0

上一篇:软件系统设计层次与内容

下一篇:没有了

给主人留下些什么吧!~~