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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:27:08+00:00 2026-05-30T15:27:08+00:00

I have a problem where I create two UDP sockets, bind them to the

  • 0

I have a problem where I create two UDP sockets, bind them to the loopback address with port 0 (requesting the stack to assign an ephemeral port). My understanding is that both sockets should be on different ports. In the code example below, both sockets are reported to be on the same IP address and port.

#include <stdio.h>
#include <arpa/inet.h>

int main(int, char**)
{
    int fd1 = ::socket(AF_INET, SOCK_DGRAM, 0);
    if (fd1 < 0)
    {
        perror("fd1 socket()");
        return -1;
    }
    int fd2 = ::socket(AF_INET, SOCK_DGRAM, 0);
    if (fd2 < 0)
    {
        perror("fd2 socket()");
        return -1;
    }

    // Set SO_REUSEADDR for both sockets
    int reuse = 1;
    if (::setsockopt(fd1, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        perror("fd1 SO_REUSEADDR failed");
        return -1;
    }
    if (::setsockopt(fd2, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        perror("fd2 SO_REUSEADDR failed");
        return -1;
    }

    sockaddr_storage storage;
    socklen_t addrlen = sizeof(storage);
    sockaddr_in& addr = reinterpret_cast<sockaddr_in&>(storage);
    addr.sin_family = AF_INET;
    addr.sin_port = 1234;
    addr.sin_port = 0;
    if (::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) <= 0)
    {
        perror("Failed to create address 127.0.0.1");
        return -1;
    }
    sockaddr* pAddr = reinterpret_cast<sockaddr*>(&storage);

    if (::bind(fd1, pAddr, addrlen) < 0)
    {
        perror("bind fd1 failed");
        return -1;
    }

    // Get the local address for fd1
    addrlen = sizeof(storage);
    if (::getsockname(fd1, pAddr, &addrlen))
    {
        perror("getsockname for fd1 failed");
        return -1;
    }
    char straddr[INET_ADDRSTRLEN];
    if (!inet_ntop(AF_INET, &addr.sin_addr, straddr, sizeof(straddr)))
    {
        perror("inet_ntop for fd1 failed");
        return -1;
    }
    printf("fd1=%d addr=%s:%d\n", fd1, straddr, addr.sin_port);

    if (::bind(fd2, pAddr, addrlen) < 0)
    {
        perror("bind fd2 failed");
        return -1;
    }

    // Get the local address for fd2
    addrlen = sizeof(storage);
    if (::getsockname(fd2, pAddr, &addrlen))
    {
        perror("getsockname for fd2 failed");
        return -1;
    }
    if (!inet_ntop(AF_INET, &addr.sin_addr, straddr, sizeof(straddr)))
    {
        perror("inet_ntop for fd2 failed");
        return -1;
    }
    printf("fd2=%d addr=%s:%d\n", fd2, straddr, addr.sin_port);

    return 0;
}

This code gives the following output …

fd1=4 addr=127.0.0.1:1933
fd2=5 addr=127.0.0.1:1933

I need both sockets on the same (local) IP address, but different ports. Can anyone explain why both sockets share the same port? Can anyone suggest a fix?

  • 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-30T15:27:10+00:00Added an answer on May 30, 2026 at 3:27 pm

    That is the expected behavior for SO_REUSEADDR on a UDP socket. Remove that setting to return to normal allocation rules.

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

Sidebar

Related Questions

I have this problem. I create two instaces of my classes in main window.
I'm having a problem with mixing managed and unmanaged code. I have created two
I have a problem with the Create View in the SimpleRepository example in Subsonic
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem with Qt::Tool flag. When I create new widget with Qt::Tool flag
I have a problem... I create a MFMailComposeViewController and it shows up. However, neither
I have a problem with my WPF program. I'm trying to create an object
i have very simple problem. I need to create model, that represent element of
Here's my problem: I have do create a menu/list of actions (which would be
I'm using Rational Application Developer to create some web services. The problem I have

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.