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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:07:34+00:00 2026-06-05T19:07:34+00:00

I am trying to implement a simple client that connects to a give address.

  • 0

I am trying to implement a simple client that connects to a give address. You’ll notice that I’m printing the given address and the IP address and port after I put it in my sockaddr_in struct.
I get two different addresses even though it should be the same.

Here’s my code:

int createConnection(char * address, char * port) {
    cout << address << " " << port << endl;
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(atoi(port));
    inet_aton(address,&(server.sin_addr));
    memset(&(server.sin_zero), '\0', 8);
    if ((sockFd = socket(PF_INET,SOCK_STREAM,0)) == -1) {
        perror("client: socket");
    }
    if (connect(sockFd,(const sockaddr *)&server,sizeof(struct sockaddr_in)) == -1) {
        close(sockFd);
        perror("client: connect");
    }

    char s[INET_ADDRSTRLEN];
    inet_ntop(AF_INET, (struct sockaddr *)&server, s, sizeof s);
    printf("client: connecting to %s, %i\n", s, server.sin_port);
    printf("connected");
    return 0;
}

And the output is

132.65.151.39 3000
client: connecting to 2.0.11.184, 47115

My questions:

  1. Why do I see two different addresses?
  2. How come the connection does not fail?

What happens is that my program is blocked in recv() later on, so I think it didn’t connect to where I wanted it. Thanks!

  • 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-05T19:07:36+00:00Added an answer on June 5, 2026 at 7:07 pm

    It looks like you’re passing the wrong thing to inet_ntop():

    inet_ntop(AF_INET, (struct sockaddr *)&server, s, sizeof s);
    

    You should probably be passing .sin_addr:

    inet_ntop(AF_INET, &server.sin_addr, s, sizeof s);
    

    For the port, it prints wrong because you need to call ntohs() to convert back to host byte order:

    printf("client: connecting to %s, %i\n", s, ntohs(server.sin_port));
    

    The connection works, presumably because the address is set correctly, in actuality.

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

Sidebar

Related Questions

I'm trying to implement a simple web server on Linux that connects to the
I'm trying to implement a simple horizontal navigation menu that just shows a single
im trying to implement simple secured client server communiction using WCF. when im launching
I'm trying to implement a simple client-middleware-database architecture, where the client send the request
I'm trying to implement a simple HTTP server in Android that handles POST requests,
I am trying to implement a simple UDP client and server. Server should receive
I am trying to implement a very simple file transfer client in python using
I am trying to implement a simple client for a webservice, the only problem
I'm trying to implement a chat client for Android which connects to google talk
I've been trying to implement simple low level keyhook using JNI and all went

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.