예제 #1
0
int main(int argc, char** argv) {

    // Check if the port number was provided
    if(argc != 2) {
        printf("Proper usage: chatserver [port number]");
        exit(1);
    }
    else {
        portNo = atoi(argv[1]);
    }

    // Set up the TCP socket
    serverSock = setSocket(&portNo);

    // Bind the socket
    bindSocket();

    while(1) {
        // Establish a socket that listens for incoming reqests
        listenForReq();


        // Accept a client request
        isConnected = connectClient();


        // allocate memory to send and receive messages
        sendBuff = (char*) malloc(512);
        recBuff = (char*) malloc(512);
        quit = (char*) malloc(10);
        strcpy(quit, "\\quit\n");


        getClientHandle();


        while(1) {
            if(receiveMessage() == 0) {
                break;
            }
            if(sendMessage() == 0) {
                break;
            }
        }

    }

    close(serverSock);


    return 0;
}
예제 #2
0
main(int ac, char **av)
{
    int i;
    int on = 1;
    short port = -1;		/* host order. */
    int setFD = 0;
    struct sockaddr_in saddr;
    int acceptFD;
    clientHandle_t *clientHandle;
    int code;
    int addr_len;
    PROCESS pid;
    fd_set *rfds, *wfds, *efds;
    int sockFD;

    program = av[0];

    if (ac < 2)
	Usage();

/*    lwp_debug = 1; */

    signal(SIGIO, sigIO);


    for (i = 1; i < ac; i++) {
	if (!strcmp("-fd", av[i])) {
	    if (++i >= ac) {
		printf("Missing number for -fd option.\n");
		Usage();
	    }
	    setFD = atoi(av[i]);
	    if (setFD <= 2) {
		printf("%d: file descriptor must be at least 3.\n", setFD);
		Usage();
	    }
	} else {
	    if (port == -1) {
		port = atoi(av[i]);
		if (port <= 0) {
		    printf("%s: port must be at least 1\n", av[i]);
		    Usage();
		}
	    } else {
		printf("%s: Unknown argument.\n", av[i]);
	    }
	}
    }

    if (port == -1) {
	printf("Missing port.\n");
	Usage();
    }

    if (!setFD) {
	setFD = 31;
	printf("Using default socket of %d.\n", setFD);
    }

    OpenFDs(setFD);

    IOMGR_Initialize();

    /* Setup server processes */
    for (i = 0; i < MAX_THREADS; i++) {
	if (LWP_CreateProcess
	    (handleRequest, 32768, LWP_NORMAL_PRIORITY,
	     (char *)&clientHandles[i], "HandleRequestThread", &pid) < 0) {
	    printf("%s: Failed to start all LWP's\n", program);
	    exit(1);
	}
	clientHandles[i].ch_pid = pid;
    }


    sockFD = socket(AF_INET, SOCK_STREAM, 0);
    if (sockFD < 0) {
	perror("socket");
	exit(1);
    }
    Log("Using socket at file descriptor %d.\n", sockFD);

    if (setsockopt(sockFD, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))
	< 0) {
	perror("setsockopt: ");
	exit(1);
    }

    memset((void *)&saddr, 0, sizeof(saddr));

    saddr.sin_family = AF_INET;
    saddr.sin_port = ntohs(port);
    saddr.sin_addr.s_addr = htonl(INADDR_ANY);

    if (bind(sockFD, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
	perror("bind: ");
	exit(1);
    }


    rfds = IOMGR_AllocFDSet();
    wfds = IOMGR_AllocFDSet();
    efds = IOMGR_AllocFDSet();
    if (!rfds || !wfds || !efds) {
	printf("main: Could not alloc fd_set's.\n");
	exit(1);
    }

    listen(sockFD, 100);


    while (1) {
	FD_ZERO(rfds);
	FD_ZERO(wfds);
	FD_ZERO(efds);
	FD_SET(sockFD, rfds);
	FD_SET(sockFD, efds);

	Log("Main - going to select.\n");
	code =
	    IOMGR_Select(sockFD + 1, rfds, wfds, efds, (struct timeval *)0);
	switch (code) {
	case 0:		/* Timer, can't happen here. */
	case -1:
	case -2:
	    Log("Oops! select returns %d!\n", code);
	    abort();
	default:
	    if (FD_ISSET(sockFD, efds)) {
		recvOOB(sockFD);
		assertNullFDSet(sockFD, rfds);
		assertNullFDSet(-1, wfds);
		assertNullFDSet(sockFD, efds);
	    }
	    if (FD_ISSET(sockFD, rfds)) {
		while (nThreads > MAX_THREADS) {
		    IOMGR_Sleep(1);
		}

		clientHandle = getClientHandle();

		addr_len = sizeof(clientHandle->ch_addr);
		clientHandle->ch_fd = accept(sockFD, (struct sockaddr *)
					     &clientHandle->ch_addr,
					     &addr_len);
		if (clientHandle->ch_fd < 0) {
		    perror("accept: ");
		    exit(1);
		}

		Log("Main - signalling LWP 0x%x\n", &clientHandle->ch_state);
		LWP_NoYieldSignal(&clientHandle->ch_state);
		assertNullFDSet(sockFD, rfds);
		assertNullFDSet(-1, wfds);
		assertNullFDSet(-1, efds);
		break;
	    }
	    Die(1, "(main) No data to read.\n");
	}
    }
}