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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:05:49+00:00 2026-06-04T20:05:49+00:00

I have a C function to check a host and its port, when I

  • 0

I have a C function to check a host and its port, when I use FQDN host name, the function return error like: connect() failed: connect time out, but if I use IP address instead, it seems ok, how to fix this?

Thanks.

#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>

int is_network_up(char *chkhost, unsigned short chkport) {
    int sock;
    struct sockaddr_in chksock;
    struct hostent *host = NULL;

    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
        syslog(LOG_ERR, "socket() creation error: %s", strerror(errno));
        return 0;
    }

    memset(&chksock, 0, sizeof(chksock));
    chksock.sin_family = AF_INET;
    chksock.sin_port = htons(chkport);

    /* get the server address */
    if (inet_pton(AF_INET, chkhost, &(chksock.sin_addr.s_addr)) <= 0) {
        if ((host = gethostbyname(chkhost)) == NULL) {
            syslog(LOG_ERR, "%s", hstrerror(h_errno));
            return 0;
        }

        memcpy(&(chksock.sin_addr.s_addr), &(host->h_addr_list[0]),
               sizeof(struct in_addr));
    }

    /* try to connect */
    if (connect(sock, (struct sockaddr *) &chksock, sizeof(chksock)) < 0) {
        syslog(LOG_ERR, "connect() failed: %s", strerror(errno));
        return 0;
    }

    close(sock);
    return 1;
}
  • 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-04T20:05:50+00:00Added an answer on June 4, 2026 at 8:05 pm

    inet_pton() is the wrong task for that. It only accepts numerical addresses.

    In former times, people used to use gethostbyname() for name resolution.

    But as we have 2012 meanwhile, this method is outdated for several years now, as it is still restricted to AF_INET.

    With the program below, you should achieve about the same and stay future compatible.

    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int is_network_up(char *chkhost, unsigned short chkport) {
        int sock = -1;
        struct addrinfo * res, *rp;
        int ret = 0;
        char sport[10];
        snprintf(sport, sizeof sport, "%d", chkport);
    
        struct addrinfo hints = { .ai_socktype=SOCK_STREAM };
    
        if (getaddrinfo(chkhost, sport, &hints, &res)) {
            perror("gai");
            return 0;
        }
    
        for (rp = res; rp && !ret; rp = rp->ai_next) {
            sock = socket(rp->ai_family, rp->ai_socktype,
                    rp->ai_protocol);
            if (sock == -1) continue;
            if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) {
                char node[200], service[100];
                getnameinfo(res->ai_addr, res->ai_addrlen, node, sizeof node, service, sizeof
    service, NI_NUMERICHOST);
    
                printf("Success on %s, %s\n", node, service);
                ret = 1;                  /* Success */
            }
            close(sock);
        }
        freeaddrinfo(res);
    
        return ret;
    }
    
    int main(int argc, char** argv) {
        if (argc > 1) {
            printf("%s: %d\n", argv[1], is_network_up(argv[1], 22));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used Javascript onlaod like this: function check() { var pic = new
I have created this function to check abecedarian with while loop (A word is
Does R6RS or Chez Scheme v7.9.4 have a library function to check if a
Hey, so I have created a function to check the DB for unique entries,
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have a function CheckMobile(MobNumber) and it will check all duplicate record of MobNumber
I have the following JavaScript I am using to save selected check boxes: function
I have the next code: js: $('[id^=check-]').change(function(){ var new_id = this.id.replace(/check/, 'new'), real_id =
I have a loop created with each, check this example: $('.foo').each(function(i){ //do stuff });
I have a HTML page, with html-mode enabled. I call function sgml-validate to check

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.