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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:13:33+00:00 2026-06-05T09:13:33+00:00

I am able to get the port returned, but the IP is not. Should

  • 0

I am able to get the port returned, but the IP is not. Should I be using something else other than getsockname? thx

        if ((sock = ::socket(AF_INET, SOCK_STREAM, 0)) == -1) {
            c.Format("Socket Error:  %d", GetLastError());
            tcpMESSAGE = 1;
            break;
        }


        server_addr.sin_family = AF_INET;         
        server_addr.sin_port = htons(4000); //port setting    
        server_addr.sin_addr.s_addr = INADDR_ANY; 

        if (::bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
            c.Format("Bind Error:  %d", GetLastError());
            tcpMESSAGE = 1;
            break;
        }

        if(1==1){
            int len = sizeof server_addr;
            if(::getsockname(sock,(struct sockaddr*)&server_addr,&len) == -1)
                MessageBox("Error local host ip");

            c.Format("local addr %s:%u\n  errno: %d",inet_ntoa(server_addr.sin_addr),ntohs(server_addr.sin_port), errno);
            MessageBox(c);
        }
  • 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-05T09:13:35+00:00Added an answer on June 5, 2026 at 9:13 am

    You are binding to INADDR_ANY, but you have not connected the socket to a peer yet, so there is no IP for getsockname() to return. After a connection has been established via connect() or accept(), then getsockname() can return the specific IP that is being used for that connection. Binding alone is not enough, unless you bind to a specific IP, which you are not doing.

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

Sidebar

Related Questions

not able to get this, can someone help for this LINQ query? select col1,
I am able to get the current date, but I don't know how to
I am not able to get the hg head or status for a given
I am able to get the output of a pdf using fpdf, the problem
I'm not able to get this persistence file correct... I do not find any
I am not able to get my jwysiwyg and Jhtmlarea text editors to work
As following i able to get usb com port names attached to 32bit win7OS
I am able to get the current page's url in jsf from back-end bean
I'm able to get the following results with this working query: $query = SELECT
I'm finally able to get an Access Token, now I'm very confused as how

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.