Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1069596
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类: LINUX

2009-12-10 21:07:28

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <linux/reboot.h>

#define err(msg) perror(msg)

int main(void)
{
        unsigned char ip[] = "192.168.10.40";
        unsigned char mask[] = "255.255.255.0";
        unsigned char *p;
        struct sockaddr_in ipaddr, maskaddr;
        unsigned int broadcast;

        memset(&ipaddr, '\0', sizeof(ipaddr));
        memset(&maskaddr, '\0', sizeof(maskaddr));

        if (inet_pton(AF_INET, ip, &ipaddr.sin_addr) < 0) {
                err("inet_pton");
                goto out;
        }
        if (inet_pton(AF_INET, mask, &maskaddr.sin_addr) < 0) {
                err("inet_pton");
                goto out;
        }

        broadcast = ipaddr.sin_addr.s_addr | ~maskaddr.sin_addr.s_addr;
        p = (unsigned char *)&broadcast;
        printf("%u.%u.%u.%u\n", *p, *(p+1), *(p+2), *(p+3));

        return 0;
out:
        return -1;
}


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