Advertisement

threads and select()

Started by October 29, 2004 10:21 AM
3 comments, last by Caramelo 20 years, 3 months ago
Suppose on a Server, I have two, three or more client session threads.Each one has a non-blocking socket that communicates with the server and another non-blocking socket that is bind and listens by performing select() to check for incoming connection on another local server port i.e. all the client sessions use select() to check whether a connections occurs on a server-side port. It looks as the select() on more than one session would not work. Basicly I need to know if this is supposed to work. Thank you !

.

More than one thread can be in select() at the same time. However, more than one thread should not select() on the same socket at the same time (that'd be a design mistake).
enum Bool { True, False, FileNotFound };
Advertisement
ok, right now I just need to make it work and not care about performance problems. The thing is that I need to implement port-forwarding and with this model I guess I have problems with it, since there are two or more threads (on the server side) that perform listen() on a socket and I use select() to check whether a connection occurs.
In the "Network Programming for Microsoft Windows, Second Edition!" it is said:
Quote:

By default, a socket cannot be bound to a local address that is already in use; however, occasionally it is necessary to reuse an address this way. A connection is uniquely identified by the combination of its local and remote addresses. As long as the address you are connecting to is unique in the slightest respect (such as a different port number in TCP/IP), the binding will be allowed.

The only exception is for a listening socket. Two separate sockets cannot bind to the same local interface (and port, in the case of TCP/IP) to await incoming connections. If two sockets are actively listening on the same port, the behavior is undefined as to which socket will receive notification of an incoming connection. The SO_REUSEADDR option is most useful in TCP when a server shuts down or exits abnormally so that the local address and port are in the TIME_WAIT state, which prevents any other sockets from binding to that port. By setting this option, the server can listen on the same local interface and port when it is restarted.

Since I am fairly new to network programming, I'm a bit confused as how to solve this.Any suggestions are welcome.

.

Basically, you will have to design the system to only use one socket actually being in a listening state on a certain interface/port combination. To do this, you have to make sure to only call accept() in a central thread. accept() will return a new socket descriptor on success that you can hand to the actual worker thread.

However, if you're using select() anyway, there's little to no need for using multiple threads. The main use for select() ist to work on several blocking sockets in a single thread.
hm, this won't result very good to my 'independent running threads' structure, because I will possibly have to do some thread locking.
Anyway, I wasn't going to use select() initialy, I was thinking about WSAEventSelect Model, but I guess that would lead to the same problem too.
I've downloaded a couple of commercial demo port-forwarding applications and I just wonder how they got it working !?

.

This topic is closed to new replies.

Advertisement