LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   restricting incoming connections, using sockets (https://www.linuxquestions.org/questions/programming-9/restricting-incoming-connections-using-sockets-244483/)

SoulSkorpion 10-19-2004 01:05 AM

restricting incoming connections, using sockets
 
Hi.

I'm doing some sockets-based programming for a uni assignment. The code that handles the connecting, sending and receiving is working fine, since it's based on code from a previous assignment.

The problem is that this time my server program must use threads, each connecting client being handled by a seperate thread. Also, the number of threads is fixed at startup, and the threads must all be created at the beginning (rather than creating them as clients connect).

Anyway. I've got most of this working ok. The problem is that there is a very definite limit on the number of simultaneous connections, but I can't work out how to get the server to stop accepting connections once the limit's been reached, then resume accepting them once space becomes available.

The closest I've come is shutting down\closing the listen socket when the server's at max, but I can't seem to reopen the listen socket. Closing the listen socket seems to work, as far as blocking incoming connections goes, but nothing I can think of to resume listening is working. Mostly, the problem seems to be "address already in use". It's not working to simply try to listen on the same file desc again, nor to create and bind a new socket.

So... am I going about this completely the wrong way?

anoop_cv 10-19-2004 06:43 AM

I think what u are trying to do is the way to do it. The problem of "address already in use" can be solved by the use of "setsockopt" call, with an option to resuse the address.

sample usage is given below
int yes = 1;
setsockopt(sockfd, SOL_SOCKET,SO_REUSEADDR, &yes, sizeof(int));

dont forget to do the error checking ;-)

SoulSkorpion 10-20-2004 03:15 AM

That seems to be working! Thank you! :D


All times are GMT -5. The time now is 01:07 PM.