0

I'm writing a single threaded select server where a user can add listen sockets dynamically. The problem is that if someone wants to add a listen socket, the server won't select on that new socket until something happens on one of the other sockets.

One workaround I thought of was to add a fake socket to the list that I could signal whenever a new listen socket was added in order to force the server to add the new listen socket to the list. This solution seems a little "hacky" to me and I wanted to know if anyone else had any ideas.

marler8997
  • 119
  • 10
  • Are you running `accept()` separately from the `select()`? Why not accept connections in the `select()` as well, then there is no sync problem – Erik Ekman Jul 08 '13 at 18:44
  • I am accepting connections on the select...and there are no sync problems...I'm not sure what your comment has to do with my question? – marler8997 Jul 08 '13 at 19:16

3 Answers3

0

Yes, but in order to add another listen socket, I imagine you have to connect to the server first and then tell it to add another socket.

Also, you may want set a timeout value in the socket call. This way if the server is not busy responding to requests, it can upon timeout do some low priority activities such as delete stale files, etc.

Arun Taylor
  • 1,574
  • 8
  • 5
  • Actually I don't use any of the sockets on this thread to tell the server to add a listen socket. The timeout you mentioned really has nothing to do with my question...I'm looking for a way to pop a select call...I could use a timeout for this but The longer the timeout the longer it takes to add the listener, the shorter, the more a performance hit you have. In my case I could have very long periods of no activity, and then when I need to add the listen socket, I need it to start accepting immediately. It wouldn’t make sense for this server to constantly be polling for weeks for no reason… – marler8997 Jul 08 '13 at 16:58
  • There are three ways for select to pop/return. A signal is received, timer expires, or an event happens on one of the socket descriptor. If you prefer to use a signal, you could use HUP. How do you tell the server to add another socket (given that it is a single threaded server)? – Arun Taylor Jul 08 '13 at 18:46
  • It's actually a multi-threaded program, I just didn't want to include any unecessary details...but you mentioned...a signal is received? I wonder if there is a signal I haven't though of? Theres the Accept signal...DataAvailable signal...Write signal....? Oh an you mentioned HUP? What is that? – marler8997 Jul 08 '13 at 19:16
  • Under unix like OS, signal SIGHUP is often used for scenarios like the one that you have. I.e. once in a while you want the process to pick up some config info changes and continue running. e.g. kill -HUP `cat /var/run/inetd.pid`. You have to setup a signal handler function that gets called when 'kill -HUP ' is executed. The signal handler then re-reads the updated config file and continues running. On Windows, things may be a little different. If there is no user authentication, then I would be inclined to use a separate socket listening at a different 'admin only use' port number. – Arun Taylor Jul 08 '13 at 21:53
0

Your idea about the fake socket is not bad. I would rename it to control socket, because you could use it for sending different kind of commands or requests to the server. And it would be more real time than the polling with timeout of select.

Type of the control socket can also be different than the other socket. For example, it would be easier to use UDP or unix-domain socket to send control messages to local server program, than using TCP socket for that.

SKi
  • 8,007
  • 2
  • 26
  • 57
  • Ya, from what I've seen it looks like a special socket used to pop the select call may be the best option...thanks for helping confirm my thoughts... – marler8997 Jul 08 '13 at 19:18
0

You can use the self-pipe trick. Alex B has post a good answer about it, you really should read it.

Community
  • 1
  • 1
nouney
  • 4,363
  • 19
  • 31