It's not hard to read so don't be to scared of the loop
Code:
int main(int argc, char *argv[])
{
fd_set masterdesc;
fd_set readescrip;
int fd;
struct sockaddr_in address;
struct timeval seconds;
int simpleSocket;
int clientSocket;
int intiger, yes = 1;
seconds.tv_sec = 0; /* seconds for select to wait for connections */
seconds.tv_usec = 1000; /* microseconds for select to wait for connections
* there are 1,000 microseconds in a millisecond
* there are 1,000 milliseconds in a second
* thus there are 1,000,000 Microseconds in one
* second, and there are 60 seconds in one minute */
FD_ZERO(&masterdesc);
FD_ZERO(&readescrip);
printf("ragnarok: process id #%i", getpid());
if ((simpleSocket=socket(AF_INET, SOCK_STREAM,0)) == -1)
fatal("creating simpleSocket, AF_INET SOCK_STREAM failed");
fcntl(simpleSocket, F_SETFL, O_NONBLOCK);
setsockopt(simpleSocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(simpleSocket,(struct sockaddr *)&address,sizeof(address)) == -1)
{
fatal("binding simpleSocket, AF_INET SOCK_STREAM failed");
close(simpleSocket);
}
if (listen(simpleSocket, 1000) == -1)
{
fatal("listening to simpleSocket, AF_INET SOCK_STREAM failed");
close(simpleSocket);
}
FD_SET(simpleSocket, &masterdesc);
while (!ragnarok_shutdown)
{
readescrip = masterdesc;
if (select(FD_SETSIZE, &readescrip, NULL, NULL, NULL) == -1)
nonfatal("selecting file descriptor");
clientSocket = accept(simpleSocket, NULL, NULL);
FD_SET(clientSocket, &readescrip);
/*I think I want to request from users without
stopping this god forsakin game loop. */
welcome_message(cliantSocket);
prompt_for_username(clientSocket);
/*I think I want to request from users without
stopping this god forsakin game loop. */
if (read(clientSocket, request, sizeof(request)) < 0)
{
close(clientSocket);
FD_CLR(clientSocket, &readescrip);
printf("ragnarok: read(clientSocket, request, sizeof(request)) < 0");
printf("ragnarok: client on descriptor #%i disconnected\n", clientSocket);
}
}
for (intiger = 0; intiger < FD_SETSIZE; intiger++)
if (FD_ISSET(intiger, &readescrip))
{
close(intiger);
FD_CLR(intiger, &readescrip);
printf("client on descriptor#%i disconnected\n", intiger);
}
close(simpleSocket);
exit(EXIT_SUCCESS);
}