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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:40:34+00:00 2026-05-29T06:40:34+00:00

I am new to socket programming and I’m trying to figure out how poll

  • 0

I am new to socket programming and I’m trying to figure out how poll works. So I made a little example program. The program seems to work as how I expect it to, but when I comment out the line that has int dummy the for loop only runs one iteration when it’s suppose to do ten. What I don’t understand is how that variable has anything to do with the for loop. The program is suppose to print “timeout” after 3.5 secs and print “return hit” if there is input available.

#include <stdio.h>
#include <poll.h>

int main(int argc, char *argv[]) {
    int a;
    int b;
    int c;
    char buf[10];
    int i;
    struct pollfd ufds[1];      
    ufds[0].fd = 0;
    ufds[0].events = POLLIN;
    int rv;
    int dummy;
    for(i=0; i < 10; i++) {
        printf("%i ", i);
        if((rv = poll(ufds, 2, 3500)) == -1) perror("select");
        else if (rv == 0) printf("Timeout occurred!\n");
        else if (ufds[0].revents & POLLIN) {
            printf("return hit\n");
            read(0, buf, 10);
        }   
        fflush(stdout); 
    }   
    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-29T06:40:34+00:00Added an answer on May 29, 2026 at 6:40 am
    if((rv = poll(ufds, 2, 3500)) == -1) perror("select");
                        ^
    

    You are telling poll you have 2 file descriptors (2 pollfd structures) but you only have one. That’s undefined behavior (you’re tricking poll to tread into unallocated memory). Change that argument to 1.

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

Sidebar

Related Questions

New to socket programming. Got a couple questions: My program is really inconsistent with
I am relatively new to socket programming. I am currently trying examples provided with
I am new to socket programming and trying to write a simple cmd line
I am quite new to socket programming. I was trying to make p2p communication
I'm new to C89, and trying to do some socket programming: void get(char *url)
I'm completely new to socket programming with C#, I'm trying to get two running
I'm kinda new to android socket programming. My android program simply connects to a
Fairly new to socket programming, but I've been assigned with a whopper of project.
I am fairly new to QDataStream and Socket programming, and what I want to
I am trying my hands on Socket programming in C# (.NET). I have referred

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.