码迷,mamicode.com
首页 > 其他好文 > 详细

ping(1)

时间:2014-10-25 17:12:02      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   sp   div   on   

/*


ping program for learning IP protocol


    author: jeff
    date:    2014/10/25

*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>

static int ICMP_create();
static int opt_set(int sockfd);

int
main(int argc, char* argv[])
{
    int sockfd = ICMP_create();
    int optset = 0;
    if(sockfd != -1)
        printf("create socket ICMP success!\n");
    else
        printf("create socket ICMP failed [%d]!\n", errno);

    optset = opt_set(sockfd);
    if(optset != -1)
        printf("sock set success\n");
    else
        printf("sock set failed\n");
        
    return 0;
}


static int ICMP_create()
{
    return socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
}

static int opt_set(int sockfd)
{
    int on =1;
    return setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on));    
}

 

ping(1)

标签:style   blog   color   io   ar   for   sp   div   on   

原文地址:http://www.cnblogs.com/unixshell/p/4050254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!