Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9170565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:02:29+00:00 2026-06-17T16:02:29+00:00

Compiler: Code::Blocks(GNU GCC) Platform: Windows(x86) Includes: winsock.h winsock2.h (ws2_32 is linked as well) I

  • 0
  • Compiler: Code::Blocks(GNU GCC)
  • Platform: Windows(x86)
  • Includes: winsock.h winsock2.h (ws2_32 is linked as well)

I am currently trying to write a program that will read a text file containing a list of IP-Addresses and ping each IP-Addresses respectively. If the host responds to the ping then the host’s IP-Address will be copied to a seconded file specified by the user. Unfourtantly this is the first time I have used C’s socket library and I cannot find a good tutorial about how to ping using C. From what I understand of the couple tutorials I did find. I need to included a ICMP header, which is a struct containg the ICMP type, code, and checksum, in a IP datagram. But I have no idea how to go about doing so, should I declare the struct myself or is it declared in a header file? I am assuming that it is in a header but the tutorials contradicted each other about exactly where it is declared. I tired including icmp.h and netinet/icmp.h but my compiler complained that they don’t exist so I created my own struct.

    struct echo_request
    {
        char type; // Type
        char code; // Code
        short checksum; // Checksum
        short id; // Identification
        short seq; // Sequence
        int time; // Time
        char data[16]; // Data
    };

I thought that I might be able to get away with it but I wasn’t even able to compile my program because my compiler says that in_cksum()(checksum generator) is undefined.

To sum up my questions, what header files should I include, how do I create a ping packet, am I using the correct checksum generator function, should a ping be directed to port 80, and should the socket I use be RAW or DGRAM?

This is what I have so far, please note that I have purposely left out error checking.

    int socket_descriptor = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);

    struct sockaddr_in address; //Initialize address struct
    memset(&address, 0, sizeof(address)); //Clear address struct

    //Declare address
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr(ipaddress);
    address.sin_port = htons(80);

    //Bind socket to address
    bind(socket_descriptor, (struct sockaddr *)&address, sizeof(address));

    //Create packet
    struct echo_request packet; //See above for declaration of struct
    memset(packet.data, 1, 16);
    packet.type = 8; //ECHO_REQUEST
    packet.code = 0;
    packet.time = gettime();
    packet.checksum = 0;
    packet.checksum = in_cksum(packet, sizeof(packet));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:02:30+00:00Added an answer on June 17, 2026 at 4:02 pm

    If you don’t have to implement the ping from scratch and you want only Windows solution, I’d second Anton’s suggestion for IcmpSendEcho. If you have to implement ping, look at how POCO ICMP package is implemented. It is portable code and it runs fine on Windows.

    In regards to the specific questions, here are the answers:

    what header files should I include

    #include <winsock2.h>
    

    how do I create a ping packet

    See ICMPv4PacketImpl::initPacket() for an example of IPv4 packet.

    am I using the correct checksum generator function

    Not for windows. See ICMPPacketImpl::checksum() for an example of checksum function.

    should a ping be directed to port 80

    No. There’s no such thing as port when it comes to ICMP. See Does ICMP use a specific port?

    should the socket I use be RAW or DGRAM

    It should be RAW.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Compiler: Code::Blocks(GNU GCC) Platform: Windows(x86) Update: I have solved the problem by using chdir()
OS:Windows Vista(x86) Compiler: Code::Blocks I am currently writing a program that opens a specified
I'm using the Code::Blocks IDE with the GNU GCC compiler. struct test { char
I am using Code::Blocks 10.05, and the GNU GCC Compiler. Basically, I ran into
I'm using code blocks with gnu gcc compiler. But when I try to compile
I'm compiling this code with GNU GCC Compiler in Code Blocks but for some
I'm trying to get sqlite3 c-api to compile under windows with code:blocks/gcc mingw compiler
I am using Code-Blocks with the mingw GCC compiler for windows to code my
I am running C in Code:Blocks IDE with the compiler GNU GCC . And
I have been using the gcc compiler with code::blocks ide, and have noticed there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.