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 9238711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:49:31+00:00 2026-06-18T07:49:31+00:00

I have an odd problem with a UDP server I’m working on. The very

  • 0

I have an odd problem with a UDP server I’m working on. The very first udp packet received has no information on the source of the packet. Subsequent udp packets all appear to be fine and correctly display the ip address from which the packet was received. I have no clue what is causing this behavior, probably some stupid mistake, or some obscure bug. I’m using on a Linux machine running Debian.

fd_set master;
fd_set read_fds;
int fdmax;
int i;
int bytes_sent;
int bytes_recv;
socklen_t addr_len;
struct sockaddr_storage their_addr;

// provides users information needed to connect
serv_info *server_info;
server_info = (serv_info*) serv_config;

// Create UDP listener socket
int info_sock = createDGRAMSocket(NULL, server_info->port, 1);
char buffer[1024];
int len;
int send_response;

FD_SET(info_sock, &master);
fdmax = info_sock;
bytes_recv = recvfrom(i, buffer, sizeof(buffer), 0, (struct sockaddr *)&their_addr, &addr_len);
printf("Info started \n");
while (running) {
    read_fds = master;
    select(fdmax+1, &read_fds, NULL, NULL, NULL);
    for (i = 0; i <= fdmax; i++) {
        if (FD_ISSET(i, &read_fds)) {
            bytes_recv = recvfrom(i, buffer, sizeof(buffer), 0, (struct sockaddr *)&their_addr, &addr_len);
            printf("length %u: %s\n", bytes_recv, buffer);
            send_response = 0;

            switch (buffer[0]) {
                // Handle different packet types

            }

            struct sockaddr_in *sin = (struct sockaddr_in *)&their_addr;
            unsigned char *ip = (unsigned char *)&sin->sin_addr.s_addr;
            printf("IP: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);

            if (send_response) {
                bytes_sent = sendto(info_sock, buffer, len, 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr_storage));
                if (bytes_sent < 0) {
                    printf("[ERROR] Packet Send Failed %d (%s) %d\n", bytes_sent, buffer, len);
                }
            }
        }
    }
};
close(info_sock);
  • 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-18T07:49:32+00:00Added an answer on June 18, 2026 at 7:49 am

    You need to initialize addr_len to sizeof(their_addr). According to the man page:

    The argument addrlen is a value-result argument, which the caller should initialize
    before the call to the size of the buffer associated with src_addr
    , and
    modified on return to indicate the actual size of the source address. The returned
    address is truncated if the buffer provided is too small; in this case, addrlen will
    return a value greater than was supplied to the call.

    Since you aren’t initializing addr_len it seems to be taking on a value of 0 (this is highly undefined behavior). In this case, recvfrom() will not fill in the their_addr buffer, but as the man page indicates addr_len will return a value greater than was supplied to the call. So after the first call addr_len is taking on a value that is allowing the next calls to recvfrom() to properly fill in the their_addr buffer. Relying on this is unsafe though.

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

Sidebar

Related Questions

I have this very odd problem with Java (using Eclipse as IDE). I'm working
I have odd problem: After starting server I got this error: undefined local variable
I have a very odd problem. A class property is mysteriously reset between method
I have an odd problem that has me completely stumped. Unfortunately I have a
So, I have an odd problem. I'm working with an SQLite database through javascript
I'm using PuTTY to log into a Debian server. I have this odd problem
I have an odd problem. There are two databases. One has all products in
I have a very odd problem with unobtrusive validation. I have a model with
I have a very odd problem with javascript. My code is rather long so
I have a very odd problem with the following code. For some reason I

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.