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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:29:38+00:00 2026-05-14T04:29:38+00:00

I have a simple program to check if a port is open, but I

  • 0

I have a simple program to check if a port is open, but I want to shorten the timeout length on the socket connection because the default is far too long. I’m not sure how to do this though. Here’s the code:

#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char **argv) {
    u_short port;                /* user specified port number */
    char addr[1023];             /* will be a copy of the address entered by u */
    struct sockaddr_in address;  /* the libc network address data structure */
    short int sock = -1;         /* file descriptor for the network socket */

    if (argc != 3) {
        fprintf(stderr, "Usage %s <port_num> <address>", argv[0]);
        return EXIT_FAILURE;
    }

    address.sin_addr.s_addr = inet_addr(argv[2]); /* assign the address */
    address.sin_port = htons(atoi(argv[2]));            /* translate int2port num */

    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (connect(sock,(struct sockaddr *)&address,sizeof(address)) == 0) {
        printf("%i is open\n", port);
    }  
    close(sock);
    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-05-14T04:29:39+00:00Added an answer on May 14, 2026 at 4:29 am

    This article might help:

    Connect with timeout (or another use for select() )

    Looks like you put the socket into non-blocking mode until you’ve connected, and then put it back into blocking mode once the connection’s established.

    void connect_w_to(void) { 
      int res; 
      struct sockaddr_in addr; 
      long arg; 
      fd_set myset; 
      struct timeval tv; 
      int valopt; 
      socklen_t lon; 
    
      // Create socket 
      soc = socket(AF_INET, SOCK_STREAM, 0); 
      if (soc < 0) { 
         fprintf(stderr, "Error creating socket (%d %s)\n", errno, strerror(errno)); 
         exit(0); 
      } 
    
      addr.sin_family = AF_INET; 
      addr.sin_port = htons(2000); 
      addr.sin_addr.s_addr = inet_addr("192.168.0.1"); 
    
      // Set non-blocking 
      if( (arg = fcntl(soc, F_GETFL, NULL)) < 0) { 
         fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno)); 
         exit(0); 
      } 
      arg |= O_NONBLOCK; 
      if( fcntl(soc, F_SETFL, arg) < 0) { 
         fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno)); 
         exit(0); 
      } 
      // Trying to connect with timeout 
      res = connect(soc, (struct sockaddr *)&addr, sizeof(addr)); 
      if (res < 0) { 
         if (errno == EINPROGRESS) { 
            fprintf(stderr, "EINPROGRESS in connect() - selecting\n"); 
            do { 
               tv.tv_sec = 15; 
               tv.tv_usec = 0; 
               FD_ZERO(&myset); 
               FD_SET(soc, &myset); 
               res = select(soc+1, NULL, &myset, NULL, &tv); 
               if (res < 0 && errno != EINTR) { 
                  fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno)); 
                  exit(0); 
               } 
               else if (res > 0) { 
                  // Socket selected for write 
                  lon = sizeof(int); 
                  if (getsockopt(soc, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) < 0) { 
                     fprintf(stderr, "Error in getsockopt() %d - %s\n", errno, strerror(errno)); 
                     exit(0); 
                  } 
                  // Check the value returned... 
                  if (valopt) { 
                     fprintf(stderr, "Error in delayed connection() %d - %s\n", valopt, strerror(valopt) 
    ); 
                     exit(0); 
                  } 
                  break; 
               } 
               else { 
                  fprintf(stderr, "Timeout in select() - Cancelling!\n"); 
                  exit(0); 
               } 
            } while (1); 
         } 
         else { 
            fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno)); 
            exit(0); 
         } 
      } 
      // Set to blocking mode again... 
      if( (arg = fcntl(soc, F_GETFL, NULL)) < 0) { 
         fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno)); 
         exit(0); 
      } 
      arg &= (~O_NONBLOCK); 
      if( fcntl(soc, F_SETFL, arg) < 0) { 
         fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno)); 
         exit(0); 
      } 
      // I hope that is all 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple stock check program, and I have a Table
I have created a simple program that i want to replace some chars with
i want to create a simple program that i have a array of strings
Say I have simple program that emulates a board game with a number of
I have Simple java program named MainController.java. Wehn I try to compile it from
I have a simple program that creates a thread, loops twenty times and then
I have avery simple program which uses MediaPLayer. I am new to java and
I have a simple program that reads data from a PNG into a 2D
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return
Below, I have a simple program which uses the CImg library (http://cimg.sourceforge.net/) which iterates

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.