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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:24:48+00:00 2026-05-24T04:24:48+00:00

I use select() to receive data from stdin. The code is here: #include <stdio.h>

  • 0

I use select() to receive data from stdin.

The code is here:

#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
int main()
{
    fd_set rfds;
    struct timeval tv;
    int retval;
    char buf[100];

    FD_ZERO(&rfds);
    FD_SET(STDIN_FILENO, &rfds);

    tv.tv_sec = 5;
    tv.tv_usec = 0;

    retval = select(1,&rfds,0,0,&tv);

    if( retval == -1)
        perror("select reset\n");
    else if(retval == 0)
        printf("timeout\n");
    else
    {
        printf("data available\n");
        if(FD_ISSET(STDIN_FILENO, &rfds))
        {
           //int ret = recv(STDIN_FILENO, buf, sizeof(buf), 0); // ret get -1.
           int ret = read(STDIN_FILENO, buf, sizeof(buf));       // ret get correct data. 
           printf("buf: %s ret: %d\n", buf,ret);            
        }
    }    
    return 0;
}

In this code, recv() will always return -1, but read() can get correct data.

I find that read() is equivalent to recv() with a flags parameter of 0. Why then are the behaviors of recv() and read() not the same in my code?

  • 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-24T04:24:49+00:00Added an answer on May 24, 2026 at 4:24 am

    Because recv() is for use on sockets, not generic file descriptors like stdin. Sockets can be treated like any descriptor, but descriptors don’t necessarily have a socket behind them (and in the case of stdin, they don’t).

    In that particular piece of code, I suspect that if you check errno it will have a value of EINVAL or similar.

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

Sidebar

Related Questions

I can use: select * from sys.tables in mssql to show a list of
In SQL you can use SELECT * FROM INFORMATION_SCHEMA.TABLES etc to get information about
I need to select multiple records I use SELECT * FROM `table` WHERE `ID`
I need to get only 1 record from sql result. we use SELECT TOP
I have a Rails application that in the erb code, I use a select
I can use the following syntax in a Jet (mdb) query to select data
I have some existing code that retrieves data from a database using ADO.NET that
Here is my code. You have to kindly look does it suffer from 'same
I'm taking some information from data base and i want to use it in
I'm trying to use select on STDIN and a TCP socket in Ruby, but

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.