serverShutdown.c 1.5 KB
#include <stdio.h>      /* for printf() and fprintf() */
#include <sys/select.h> /* for select system call and related */
#include <sys/socket.h> /* for select system call and related */
#include <stdlib.h>     /* for exit */
#include <string.h>     /* for memset and stuff */
#include <unistd.h>     /* for getopt */
#include <errno.h>      /* for errno */

#include "include/appConfig.h"
#include "include/server.h"
#include "include/monitor.h"


void
serverClose(tVirtualItemServer * server)
{
    int i;

    for (i=3; i<=server->maxFd; i++) {
        if (FD_ISSET(i, &(server->socks))) {
            if (i == server->servSock) {
                close(server->servSock);
            } else {
                close(((server->clients)[i]).socket);
                free(((server->clients)[i]).buffer);
                FD_CLR(i, &(server->socks));
            }
        }
    }

    if (NULL != server->wHandle) {
        fclose(server->wHandle);
    }
}

void
serverShutdown(tVirtualItemServer * server)
{
    int i;

    for (i=3; i<=server->maxFd; i++) {
        if (FD_ISSET(i, &(server->socks))) {
            if (i == server->servSock) {
                shutdown(server->servSock, SHUT_RDWR);
                close(server->servSock);
            } else {
                writeBuffer(&(server->clients[i]), server->wHandle);
                clientClose(&((server->clients)[i]));
                FD_CLR(i, &(server->socks));
            }
        }
    }

    if (NULL != server->wHandle) {
        fclose(server->wHandle);
    }
}