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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:34:33+00:00 2026-05-27T02:34:33+00:00

i try to do a kind-of chat over TCP sockets. The server side is

  • 0

i try to do a kind-of chat over TCP sockets. The server side is implemented in php and (should) work fine.

No i’ve encountered a problem on the client side (implemented in c [c99]):

i’d like to listen (read) for “new bytes” from the server and i also would like to send (write) “some bytes” to the server whenever the user did enter new data.

Now the question:
-> how do i listen for incoming traffic (read) and still be able to call every 1 seconds a callback where i can check for new bytes to send to the server?
I know that i have to use select (system call) but i do not have a idea how to handle the timeout etc. select is also blocking for a certain time.

Thanks for any example.

  • 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-27T02:34:33+00:00Added an answer on May 27, 2026 at 2:34 am

    There is a function called select that can be used for this. It can check if there is new input on a socket to read, and it has a timeout so it can be used for your timer as well.

    It exists on one form or other on all major operating systems. Just do a Google search for socket select <your operating system> example and you will get a boat-load of results.


    The last argument to select is used for the timeout. It’s a pointer to a struct timeval, a structure which contains fields to set the timeout.

    If this pointer is passed as NULL then there is no timeout, and select can block indefinitely.

    To set a timeout, you need to set the tv_sec field of the timeval structure to the number of seconds, and the tv_usec field to the number of microseconds (must be less than one million). You can have a timeout of zero, i.e. just a quick poll, by setting these fields to zero.

    If select returns zero, then there was a timeout.

    Example, with a 1.5 second timeout:

    for (;;)
    {
        fd_set readset;
        int maxfd;
    
        /* Initialize and set `readset` and `maxfd` */
    
        struct timeval tv;
        tv.tv_sec  = 1;
        tv.tv_usec = 500000;
    
        int res = select(maxfd + 1, &readset, NULL, NULL, &tv);
    
        if (res == -1)
            perror("select");  /* Error */
        else if (res == 0)
        {
            /* Timeout, do something */
        }
        else
        {
            /* A socket in `readset` is readable */
        }
    }
    

    If select returns before the timeout, the timeval structure is modified to contain the time remaining of the timeout.

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

Sidebar

Related Questions

This is kind of a oddball problem so I will try to describe the
i have a big problem. i try to implement a kind of speedometer. i
Ive got some kind of a problem here. When I try this function, it
I am kind of new to php. I try to make an app in
I'm kind of a noob when it comes to windows API. I try to
I am not sure it's a good idea to try to create different kind
Overflow :3 I've got a kind of hard-case question. So I'll try clearly explain
I have a client server application writter in C# .NET using Sockets. I often
This question is kind-of crappy because I try to get around some limitations: Current
This question is kind of lengthy but I try to provide you with the

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.