Chinaunix首页 | 论坛 | 博客
  • 博客访问: 55788
  • 博文数量: 23
  • 博客积分: 1598
  • 博客等级: 上尉
  • 技术积分: 210
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-27 10:26
文章分类

全部博文(23)

文章存档

2011年(2)

2010年(21)

我的朋友

分类: LINUX

2010-11-19 11:47:37

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
}

阅读(3323) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~