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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:33:36+00:00 2026-06-17T12:33:36+00:00

I was writing this simple client on Ubuntu 12. It is simple C/C++ connect

  • 0

I was writing this simple client on Ubuntu 12. It is simple C/C++ connect to server code. It works connecting to localhost. But I can’t get it to connect to an outside server. Everytime I connect to a different host, it actually connects to ‘localhost’. I am thinking it might be the way my /etc/hosts file is configured but I don’t know.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <errno.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>

int connect(const std::string host, const std::string path) {
    const int port = 80;
    // Setup the msock
    int m_sock;
    sockaddr_in m_addr;
    memset(&m_addr, 0, sizeof(m_addr));
    m_sock = socket(AF_INET, SOCK_STREAM, 0);

    int on = 1;
    if (setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(on)) == -1) {
        return false;
    }

    // Connect //
    m_addr.sin_family = AF_INET;
    m_addr.sin_port = htons(port);
    int status = inet_pton(AF_INET, host.c_str(), &m_addr.sin_addr);

    cout << "Status After inet_pton: " << status << " # " << m_addr.sin_addr.s_addr << endl;
    if (errno == EAFNOSUPPORT) {
        return false;
    }
    status = ::connect(m_sock, (sockaddr *) &m_addr, sizeof(m_addr));
    if (status == -1) {
        return false;
    }
} // End of the function //

At this line: m_addr.sin_addr.s_addr

I get zero.

  • 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-17T12:33:36+00:00Added an answer on June 17, 2026 at 12:33 pm

    You should first resolve the IP address of your Domain name from DNS server then try to establish the connection .
    gethostbyname will give the list of IP address which resolved from the Domain name with the hostent structure . you can use it as follow :

    struct hostent *hent;
    hent = gethostbyname(host.c_str());
    

    Then move over the list of address hostent give you and test the connection,here is the hostent structure and gethostbyname definition :

    #include <netdb.h>
    struct hostent *gethostbyname(const char *name);
    
    struct hostent {
      char  *h_name;            /* official name of host */
      char **h_aliases;         /* alias list */
      int    h_addrtype;        /* host address type */
      int    h_length;          /* length of address */
      char **h_addr_list;       /* list of addresses */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While writing a simple server-client application, this question came in my mind. When someone
I'm writing simple server/client and trying to get client IP address and save it
Hi I am writing a simple client-server program. In this program I have to
This might be a simple answer, but I am having some issues writing this
This is a really simple program I'm writing here but came up to the
I am writing a simple batch file and i get this The syntax of
I'm writing a simple console client-server app using MSMQ. I'm attempting to run it
I have a simple client server program that I made but the main issue
I am writing a simple client program socket programming when compiling the code using
I'm working on writing a very simple client/server application as an excuse to start

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.