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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:45:23+00:00 2026-06-04T08:45:23+00:00

new day, new problems. Somehow I find myself asking questions here almost every day,

  • 0

new day, new problems. Somehow I find myself asking questions here almost every day, btw. thanks for your support so far. I implemented the select to read from four file descriptors, but it does only read from one.

I have a test application sending packets on all four ports to the second machine, where the C program is running:

data_packet to port[13501]
data_packet to port[13502]
data_packet to port[13503]
data_packet to port[13504]
data_packet to port[13501]

and so on…

Using tcpdump I can see, that the test application is working correctly:

08:02:26.391843 IP 10.1.1.2.2000 > 10.1.1.40.13501: UDP, length: 28
08:02:27.391794 IP 10.1.1.2.2000 > 10.1.1.40.13502: UDP, length: 28
08:02:28.391820 IP 10.1.1.2.2000 > 10.1.1.40.13503: UDP, length: 28
08:02:29.391918 IP 10.1.1.2.2000 > 10.1.1.40.13504: UDP, length: 28

But my application just reads from one FD (socket), in the beginning I show all available file descriptors inside the fd_set:

thread 1 started, pc_packet_receiver 
thread 2 started, pc_packet_sender 
sock_fd[9]
sock_fd[10]
sock_fd[11]
sock_fd[12]

received on sock_fd[12] on ETH0 on port 13503
received on sock_fd[12] on ETH0 on port 13503
received on sock_fd[12] on ETH0 on port 13503
received on sock_fd[12] on ETH0 on port 13503
[ctrlC] sockets closed, threads stopped...

The implementation around the select looks like the following:

    FD_SET(sock_fd[i], &read_fds);
    fdmax = sock_fd[i];
}

for(i = 0; i < 4; i++) {
    printf("sock_fd[%d]\n",sock_fd[i]);
}

while(keepRunning) {

    bzero(&incoming_msg, MAX_PAYLOAD_LEN);
    bzero(&outgoing_msg, MAX_PAYLOAD_LEN);
    bzero(&peer, peer_len);

    readsocks = select(fdmax+1, &read_fds, NULL, NULL, NULL);

    if (readsocks < 0) {
        perror("select");
        exit(EXIT_FAILURE);
    } else if (readsocks == 0) {
        printf("nothing to read from\n");
        continue;
    }

    for(i = 0; i < 4; i++) {

        if(FD_ISSET(sock_fd[i], &read_fds)) {

            in_msg_len = recvfrom(sock_fd[i],
                incoming_msg,
                MAX_PAYLOAD_LEN,
                0,
                (struct sockaddr *) &client_addr,
                &sock_len_client);

            if (in_msg_len < 0) {
                perror("failed to receive data\n");
                exit(EXIT_FAILURE);
            }

            strncpy(outgoing_msg, incoming_msg, in_msg_len);

            if(getsockname(sock_fd[i], &peer, &peer_len) < 0) {
                perror("getsockname() failed");
                return (-1);
            }

            receiving_eth_port = (int)ntohs(peer.sin_port);

#if DBG_OUTPUT
            printf("received on sock_fd[%d] on ETH0 on port %d\n",   sock_fd[i], receiving_eth_port);
#endif

how can I achieve that select reads from all sockets…?

  • 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-06-04T08:45:24+00:00Added an answer on June 4, 2026 at 8:45 am

    select() alters your read_fds!

    After the first call to select(), read_fds may only have one fd bit set, and you’re hence checking only for this file descriptor in all remaining loops.

    The solution is to re-build read_fds every time before calling select(). This may sound tedious, but it’s common practice when dealing with select().

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

Sidebar

Related Questions

hi i'm using log4net for logging my website. Every day a new file is
I am new to programming (day 3) and I spent the last two days
Good day, After play new todo (new scala project) -> cd todo -> play
Good day! I'm new in using Java DB (Derby). I want to embed it
I want to insert row for each day to add new values, and I
I've been trying all day with no luck to regex replace the new line
I was going through Wikipedia and found out that New Zealand skipped a day
I've defined the following route for a simple blog. routes.MapRoute( Blog, blog/{year}/{month}/{day}, new {
Okay i'm new to asp mvc2 and i'm experiencing some problems with the htmlhelper
Argh, today is the day of stupid problems and me being an idiot. I

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.