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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:46:28+00:00 2026-05-25T17:46:28+00:00

I have an existing multi-threaded application which uses blocking connect() call. However, I want

  • 0

I have an existing multi-threaded application which uses blocking connect() call.

However, I want to introduce connect timeout for application where if server does not respond to our query in x milliseconds, application will stop trying and give error.

However, I am not able to figure out how to do that using poll.

@caf ‘s non blocking connect using select has been of great help. But I have read that select in slow compared to poll, hence I want to use poll. Could you please tell me if that is true?

I am pasting his code from the post here

int main(int argc, char **argv) {
u_short port;                /* user specified port number */
char *addr;                  /* will be a pointer to the address */
struct sockaddr_in address;  /* the libc network address data structure */
short int sock = -1;         /* file descriptor for the network socket */
fd_set fdset;
struct timeval tv;

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

port = atoi(argv[1]);
addr = argv[2];

address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(addr); /* assign the address */
address.sin_port = htons(port);            /* translate int2port num */

sock = socket(AF_INET, SOCK_STREAM, 0);
fcntl(sock, F_SETFL, O_NONBLOCK);

connect(sock, (struct sockaddr *)&address, sizeof(address));

FD_ZERO(&fdset);
FD_SET(sock, &fdset);
tv.tv_sec = 10;             /* 10 second timeout */
tv.tv_usec = 0;

if (select(sock + 1, NULL, &fdset, NULL, &tv) == 1)
{
    int so_error;
    socklen_t len = sizeof so_error;

    getsockopt(sock, SOL_SOCKET, SO_ERROR, &so_error, &len);

    if (so_error == 0) {
        printf("%s:%d is open\n", addr, port);
    }
}

close(sock);
return 0;

}

Could you please help me to write similar functionality using poll.

I am on RHEL and using gcc 4.5.x version.

Update: For current code, how can change socket to blocking mode once app creates connections to server. I am not able to find a way to unset this O_NONBLOCK.
Update 2: fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL) & ~O_NONBLOCK);
One post has pointed that we can do this with above command. Didn’t get the login though.

  • 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-25T17:46:29+00:00Added an answer on May 25, 2026 at 5:46 pm

    The performance of select() and poll() relative to other system-dependent polling APIs is equivalent if there are only a few file descriptors that are being polled. They both scale poorly, but can perform just fine when the program is only listening to a few fds.

    An advantage of other system-specific polling APIs tends to be that they push associated state into the kernel so that each polling syscall does not require a copy of state from userspace to kernelspace. With only a few events, this copy and scan overhead is insignificant, and this benefit does not exist.

    Just stick with select() unless you need to scale to handling hundreds or more fds.

    If you really will be handling many fds, and you must support both Linux and Solaris, consider using a library like libevent or libev to abstract away the kernel-specific efficient wait APIs.

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

Sidebar

Related Questions

I have existing code that uses CMNewProfileSearch to find then iterate over the color
I have existing Linux shared object file (shared library) which has been stripped. I
I have successfully wired up a Facebook Connect application and everything is working fine.
I have an existing multi-instance SQL 2008 cluster, and I'm trying to add Analysis
I have an existing ASP.NET website which I would like to painlessly add CMS
We have a WinForms desktop application, which is heavily multithreaded. 3 threads run with
Reproducing the new array from existing array(multi array) If I have an array called
We have a typical multi-tier/layer architecture. Application + WCF Service + Repository/EF4/Database. We are
Back Story I am currently updating an existing web application to support a multi-tenant
I have existing java code and need to create Design Document based on that.

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.