On 7/27/06, Norbert François <norbertlike@xxxxxxxxx> wrote:
Hi list,
Norbert,
I'm a beginner in C (sockets) programming, so please, excuse my stupid question. I'm trying to do a servent (server+client) for a p2p connexion. When I dissociate the client and the server, everything's all right. The Server is waiting for (pending) a connexion and when I connect on it, I've the behaviour I want. But now, I tried to merge the server & client together. When I start my program, the server is pending and the other part of my program isn't executed :( (the server is pending in an "accept" state). I tried to do some fork(), but it was useless (in fact, a new server is restarted and I get a bind error). How can I solve this problem (easily, if possible) ?
1. A process always blocks in a call to accept() waiting for socket descriptors to be fetched from the queue of incomplete connection requests. Usually, when accept() returns, another process is forked or some thread created to serve the client. 2. The bind() error you're probably observing is the famous "address already in use" thing. You can solve the issue by setting the SO_REUSEADDR socket: int on = 1; setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); with s being the socket descriptor to set the option for. The socket option must be set before the call to bind(). There are some good documents on the Internet dealing design of concurrent servers and I'd highly recommend to get your hands on W. R. Stevens' classic "Unix Network Programming" which is considered to be one of the most comprehensive guides on network programming with the sockets API. It's worth every penny. \Steve - : send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html