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

The Archive Base Latest Questions

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

Since there is no function in BSD sockets to get the IP address, I

  • 0

Since there is no function in BSD sockets to get the IP address, I did client/server program to establish the connection. One thread for each: server and client.

The IP address returned from “inet_ntoa” with localhost was 127.0.0.1.
But the network says my computer is this 10.0.0.7, and this address is what works.

How do I get the 10.0.0.7 address?
Thx

Here is my code:

DWORD WINAPI CIpAddressDlg::Thread_TcpServer(LPVOID iValue)
{
      CIpAddressDlg *pp = (CIpAddressDlg*)iValue;
      CString c;
      char buffer[128]; 
      int sinlen;
      struct sockaddr_in sin; 
      int s, h; 

      sin.sin_family = AF_INET; 
      sin.sin_addr.s_addr = INADDR_ANY;
      sin.sin_port = htons(4000);   // Port 

      s = socket(AF_INET, SOCK_STREAM,0);
      bind(s,(struct sockaddr*)&sin,sizeof(sin));
      listen(s,1);
      sinlen = sizeof(sin);      
      h=accept(s,(struct sockaddr*)&sin,&sinlen );

      //get IP address
      int len = sizeof sin;
      if(::getsockname(h,(struct sockaddr*)&sin,&len) == -1)
            pp->MessageBox("Error local host ip");

      c.Format("%d\nlocal addr %s:%u\n  errno: %d", sin.sin_addr, inet_ntoa(sin.sin_addr),ntohs(sin.sin_port), errno);
      pp->MessageBox(c);

      //verification of send
      recv(h,buffer,sizeof(buffer),0);
      pp->MessageBox(buffer);
      send(h,buffer,strlen(buffer),0);

      ::closesocket(s);
    return 0;
}

DWORD WINAPI CIpAddressDlg::Thread_TcpClient(LPVOID iValue)
{
    CIpAddressDlg *pp = (CIpAddressDlg*)iValue;
    CString c;
      char buffer[128]= "Hello world"; 
      struct sockaddr_in sin; 
      struct hostent *host; 
      int s; 

      host = gethostbyname("localhost");  

      memcpy(&(sin.sin_addr), host->h_addr,host->h_length); 
      sin.sin_family = host->h_addrtype; 
      sin.sin_port = htons(4000);

      s = socket(AF_INET, SOCK_STREAM,0);                       
      connect(s,  (struct sockaddr*)&sin,sizeof(sin));     

      send(s,buffer,strlen(buffer)+1,0); 
      recv(s,buffer,sizeof(buffer),0);

      ::closesocket(s);     

    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-06-05T05:33:36+00:00Added an answer on June 5, 2026 at 5:33 am

    There are easier ways to get the “private” / “LAN” IP of the machine your program is running on. Your solution is very ingenuitive though.

    I think the most straightforward is probably GetAdaptersAddresses. The example code on the MSDN page seems pretty thorough.

    But if that requires a newer compiler version, consider GetAdaptersInfo. See “Method Three” here and some sample code here.

    Also, it seems like you could also access the IP address via the WinSock API. See this example.

    Lastly, just as a sidenote, there is a function in BSD sockets to do exactly this (getifaddrs()), its just not ported to Windows.

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

Sidebar

Related Questions

I'm writing transpose function for 8x16bit vectors with SSE2 intrinsics. Since there are 8
Since there is no .resize() member function in C++ std::map I was wondering, how
Since there is no function to check whether a string is escaped before entering
In C++11, since there is a standard std::to_string() , I overload this function for
I did not want to post this question since there are so many posts
This is my onCreate function. Since there is lot of code inside onclickListener for
Since there is no way to join tables using Google App Engine datastore, I
Since there are no C# source codes for Wait, Pulse, PulseAll methods. Does anyone
Since there is no way that you can make the flash object transparent, there
Since there is no type in ruby, how do Ruby programmers make sure a

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.