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

  • Home
  • SEARCH
  • 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 3221122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:50:24+00:00 2026-05-17T15:50:24+00:00

I’m getting UDP packets from the port continuously. Following is the log from wireshark.

  • 0

I’m getting UDP packets from the port continuously. Following is the log from wireshark.
How to receive those packets continuously using winsock programming. I tried but I can’t able to receive. After recvfrom() call it is not writing into a buffer.Give me idea, how to receive each packet in a buffer and write each packet into to a text file. Please help me. Thanks in advance…

Source IP is 192.168.13.25 & port no is 2780 (Source is a Hardware which will send UDP packets continuously)
Dest IP is 192.168.13.250 & port no is 45141(Destination is my PC)
In my code I’m binding to 192.168.13.250(PC) and port 2780(Hardware). Then I’m calling recvfrom(). Is there any mismatch in Ip & port??
so which IP & port no do I need to get from user for bind() and recvfrom()?

No  Time        Source          Destination     Proto  Info                                                                       
1   0.000000    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
2   0.000416    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
3   0.000846    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
4   0.001281    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
5   0.001716    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
6   0.002152    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
7   0.002589    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
8   0.003025    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141

Following is my code:

int main(void) {
    SOCKET recvSockID;
    WSADATA wsaData = {0};
    FILE *udp;
    FILE *fp ;

    struct sockaddr_in sock_addr;
    struct sockaddr_in cliAddr;

    static int recvData;
    int iResult = 0;
    int sock_len = sizeof(sock_addr);
    int sockCli_len = sizeof(cliAddr);
    int recvResult;
    static int iteration;

    fp = fopen("outOfSeq.txt","a");

    if((udp = fopen("udpData.txt","w")) == 0)
        printf("udpData.txt not opened\n");

    printf("\n Enter Destination IP Address : ");
    scanf_s("%s",inputData.destIPAddr,16);

    printf("\n Enter Destination port from which to receive data : "); 
    scanf_s("%d",&inputData.portNo,5);

    printf("\n Enter No.of iterations : "); 
    scanf_s("%d",&inputData.noIteration,2);

    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if(iResult < 0) {
        printf("windows socket startup error\n");
    } 

    recvSockID = socket(AF_INET, SOCK_DGRAM, 0);
    if(recvSockID < 0) {
        printf("Socket creation error\n");
        WSACleanup();
    }

    sock_addr.sin_family = AF_INET;
    sock_addr.sin_port = htons(inputData.portNo);
    sock_addr.sin_addr.s_addr = inet_addr(inputData.destIPAddr);
    //sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    if(bind(recvSockID, (struct sockaddr *)&sock_addr,
        sizeof(struct sockaddr)) < 0)
    {
        printf("bind() failed: %ld.\n", WSAGetLastError());
        closesocket(recvSockID);
        return 0;
    }

    memset(udpBuf, 0, sizeof(udpBuf));

    iteration = inputData.noIteration;
    recvData = 1;

    while (recvData) {
        printf("receiving data\n");
        recvResult =  recvfrom(recvSockID, udpBuf, sizeof(udpBuf),
            0, (struct sockaddr *)&cliAddr, &sockCli_len); 

        if (recvResult <= 0) {
            printf("recvResult = %d\n", recvResult);

            printf("Error Code: %d",WSAGetLastError());

            printf("Socket receive()- error\n");
            return 0;
            //break;
            //goto exit;
        } else
            printf("Socket receive()- success\n");

        printf("completed rx data\n");

        fwrite(udpBuf, sizeof(udpBuf), 1, udp);
        memset(udpBuf, 0, sizeof(udpBuf));

        if (iteration != 0) {
            iteration--;
            if (iteration <= 0)
                recvData = 0;
        }
    }

//exit:
    if(udp) {
        fclose(udp);
        udp = 0; 
    }

    //shutdown socket
    closesocket(recvSockID);    
    fclose(udp);

    return 0;
}
  • 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-05-17T15:50:24+00:00Added an answer on May 17, 2026 at 3:50 pm
    1. fwrite(udpBuf, sizeof(udpBuf), 1, udp)

      You should pass recvResult instead of sizeof(udpBuf).

    2. You may not be immediately seeing the bytes on your file due to file buffering. However, after the close you should see the data.

      If you want to disable buffering, use setvbuf(udp, NULL, _IONBF, 0), or as alternative, call fflush(udp) after each write operation.

    UPDATE:

    If you’re not receiving datagrams at all, it’s likely you’re binding the wrong address or port.

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

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am using jsonparser to parse data and images obtained from json response. When
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am using JSon response to parse title,date content and thumbnail images and place
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.