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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:58:33+00:00 2026-06-12T04:58:33+00:00

My application has opened an UDP socket that is bound to INADDR_ANY to listen

  • 0

My application has opened an UDP socket that is bound to INADDR_ANY to listen to packets on all the interfaces my server has. I’m sending out replies through the same socket.

However, while sending a reply from the server, default IP is chosen by the IP layer of linux depending upon which interface is chosen for packet to going out. The IP associated with this interface may not be the destination address with which this UDP server got a query from a client. Thus source IP of the reply from server becomes different from the destination IP with which the query came. The client may be uncomfortable with such a reply.

Following link gives the behavior of INADDR_ANY with UDP:
http://www.cs.cmu.edu/~srini/15-441/F01.full/www/assignments/P2/htmlsim_split/node18.html

How can I change this default behavior and use a particular interface IP in the source address? That is more control on the application code to decide what will be the source address. Also it make sense that source address in the reply be same as the destination address with which the query came.

  • 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-12T04:58:34+00:00Added an answer on June 12, 2026 at 4:58 am

    Assuming you have multiple interfaces (one of which has the correct ip) of course you can bind to an interface for outgoing response. Take a look at SO_BINDTODEVICE socket option.

    int bind_sock2inf(int sock, char *interface_name)
    {
        int status = -1;
        struct ifreq ifr;
    
        memset(&ifr, 0, sizeof(ifr));
        snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), interface_name);
        if ( (status = setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE,
                    (void *)&ifr, sizeof(ifr))) < 0) {
            log_debug(4, "Failed binding to interface named %s", inf_name);
        }
        else log_debug(3, "Binding to interface %s", inf_name);
    
        return status;
    }
    

    Now your outgoing request should automatically use the ip address attached to this interface. The only down side is that you stop receiving messages on any other interface for this socket.

    Possible work arounds are:

    1. Use a separate socket for listening which is not bound to any interface and another one for sending and bind to whatever interface you need before sending.

    2. Bind to interface before sending a message and bind to “” again which clears the previous bind immediately after sending. However, you might loose any packets received during this time frame which your socket was bound to interface say eth0 and packets arrived at eth1.

    Also you can simply use bind() for associating a source ip for an outgoing packet.

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

Sidebar

Related Questions

I discovered almost accidentally that my machine was sending and receiving UDP packets to
Our application has a server-node that delegates work to numerous worker-nodes that are remote
How can I detect if the user has just downloaded the application and opened
Our application has many controls that are created dynamically. For example, a navigation pane
I wrote a menu application that has no persistent window or standard menu. When
I have created an TCPip server application. The application has one global TADOConnection. This
I'm creating an Android application (Android 2.3.3) that has a header, a footer and
The application has a contextmenu opened and I want to close/dismiss it without using
I am working with a team on an application that has a report generator
I believe I have the code for determining if my application has been opened

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.