473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Question about poll() on Linux

I'm try to use poll() to handle network connections on Linux. As a test, I create a simple loop that polls a socket so that everytime the client sends data, the server responds.

My problem is that I can't seem to perform error checking correctly. If I suddenly disconnect the client, I'd expect the server to return an error of some sort. But in fact, it doesn't, and simply loops forever rapidly.

Here is the loop for the server:

Expand|Select|Wrap|Line Numbers
  1. if ((client_fd = accept(server_sock, (sockaddr*) &m_addr, (socklen_t*) &addr_length)) != -1) {
  2.      pollfd pfd;
  3.      pfd.fd = client_fd;
  4.      pfd.events = POLLIN;
  5.      pfd.revents = 0;
  6.      int timeout = -1;
  7.      int retval;
  8.  
  9.     while (true) {
  10.           if ((retval = poll(&pfd, 1, timeout)) > 0) {
  11.                if (pfd.revents & POLLERR) {
  12.                     printf("An error occurred.\n");
  13.                     break;
  14.                }
  15.                if (pfd.revents & POLLHUP) {
  16.                     printf("A hang up occurred.\n");
  17.                     break;
  18.                }
  19.                if (pfd.revents & POLLIN) {
  20.                     if (recv(client_fd, buffer, MAX_RECV, 0) != -1) {
  21.                          printf("Received: %s\n", buffer);
  22.                          *buffer = '\0';
  23.                     }
  24.                     else perror("recv");
  25.                }
  26.           }
  27.           else {
  28.                perror("poll");
  29.                break;
  30.           }
  31.      }
  32. }
And the client simply connects to the server, and repeatedly sends a string, like this:

Expand|Select|Wrap|Line Numbers
  1. char* req = "Hello from the client";
  2. int len = strlen(req);
  3.  
  4. while (true) {
  5.      if (send(client_fd, req, len, 0) == -1) perror("send");
  6.      sleep(5);
  7. }
This works, but if I terminate the client process, the server process launches into an endless loop rather than handling the error.
Feb 11 '07 #1
1 2639
RedSon
5,000 Expert 4TB
First, your client's connection to the server was successful so it makes sense that you wouldn't receive an error condition because the server still thinks its connected. So what you have to do is design some kind of timeout for your server. If you don't get any data from the client after, say, 10 seconds then time out. Looks like your timeout is -1 which probably means INFINITY, so you would want to set that to 10 or if its milliseconds do like 5000 or something.
Feb 12 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: JDJones | last post by:
I'd like to put up two polls on my web site on a single page. I will be using a free remotely hosted poll service. That part is easy. But I want to remove the two individual <a href> links that...
30
by: Erwin Moller | last post by:
Hi group, Just curious what IDEs are popular nowadays. What do you expect from an IDE? Debugging? Do you use your IDE for debugging, or do you work like me? (Just use some smart-placed...
0
by: Chris | last post by:
Assuming the following XML: <poll> <title>Test Poll</title> <date>07/30/2003</date> <instructions>Please respond to the following poll:</instructions> <questions> <question> <label>What is...
3
by: Angus Comber | last post by:
Hello I am attempting to build a set of classes which handle telephony. Basically the handling of phone calls. My design currently has two main classes - one dealing with the device and the...
14
by: web.dev | last post by:
Hello C.L.J., What I'm curious is, when you are developing javascript, what tools/editors/IDEs do you use? Or are you the kind of person that uses "whatever gets the job done" type? Here are...
2
by: beliavsky | last post by:
Linux Journal annually polls its readers on questions such as their favorite programming language. In the 2005 poll, Python is 2nd, its highest ranking ever. Below are the results by year. I wish...
3
by: jm | last post by:
I have a client and a server (of course). The listener, from the code I got on Microsoft (MSDN) uses a While (true) to wait on accepting a socket. When the message sent to the listener...
8
by: eggie5 | last post by:
I keep getting an error for line 7, what's wrong with this? from django.db import models class Poll(models.Model): question = models.CharField(max_length=200) pub_date =...
1
by: Jean-Paul Calderone | last post by:
On Tue, 6 May 2008 08:36:28 -0400, inhahe <inhahe@gmail.comwrote: If you use Twisted, then you can use I/O Completion Ports, which are even better than WSAPoll, and your code will also work with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.