[C] select() won't stop responding to read from client

  • Thread starter zeion
  • Start date
In summary, the conversation discusses a server that uses select to detect activity from connected clients and send a response message. However, the problem is that select is returning the same client fd every time, causing the server to continuously spam the client with messages. The individual has tried removing the client fd and using fflush, but it has not solved the issue. Another individual suggests that if data is not being read from the fd, then select is functioning correctly by indicating that there is data to be read.
  • #1
zeion
466
1
Hi,

So I have written a server that will detect activity by connected clients using select. I am able to detect when a client has typed something from their keyboard, and then it will send send a message to the client in response.

The problem is that select() seems to be returning the same client fd in every iteration. So I would type one character from the client, then the server non-stop spams the client with the response message because it thinks that the client has still typed something.

How do I make it so that select() will only send the response message once and not forever?
I've already tried to remove client FD from the fd list and fflush but they don't do jack.
 
Technology news on Phys.org
  • #2
Are you actually reading data from the fd, and all of it?

If not, then select is doing exactly what it's supposed to do: it's telling you there's data there to be read.
 

1. Why is my select() function not responding when trying to read from a client?

The select() function is a system call that allows a program to monitor multiple file descriptors, such as network sockets, for activity. It is commonly used in network programming to handle multiple connections simultaneously. The most common reason for select() not responding is that the file descriptor being monitored is not ready for a read operation. This could be due to a network issue, a misconfigured socket, or the client not sending any data.

2. How can I troubleshoot my select() function to determine the cause of the issue?

The first step in troubleshooting select() is to check the return value of the function. If it returns -1, it indicates an error and the errno variable can provide more information about the specific issue. Additionally, you can use debugging tools, such as strace or Wireshark, to trace the system calls and network traffic to identify any potential problems.

3. Is there a way to set a timeout for select() to prevent it from hanging indefinitely?

Yes, the select() function allows for a timeout parameter to be set. This will cause the function to return after the specified amount of time, even if there is no activity on the monitored file descriptor. Setting a timeout can be useful in preventing the program from hanging indefinitely and allows for additional error handling if needed.

4. Can select() block on multiple file descriptors at once?

Yes, select() can monitor multiple file descriptors at once using the fd_set data structure. This allows for efficient handling of multiple connections in a single thread. However, it is important to note that the select() function can only monitor a limited number of file descriptors, typically around 1024, so care must be taken when using it for a large number of connections.

5. How can I avoid using select() and still handle multiple connections in my program?

There are alternative methods for handling multiple connections without using select(). One option is to use a multi-threaded approach, where each connection is handled in a separate thread. Another option is to use a non-blocking I/O model, such as epoll or kqueue, which allows for efficient handling of multiple connections without having to use select(). The best approach will depend on the specific needs and requirements of your program.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Computing and Technology
Replies
15
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
12K
Replies
7
Views
3K
  • Quantum Interpretations and Foundations
3
Replies
79
Views
5K
  • Sci-Fi Writing and World Building
Replies
6
Views
661
  • Computing and Technology
Replies
24
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Feedback and Announcements
Replies
9
Views
1K
Back
Top