Пример #1
0
int main(int argc, char *argv[]) {
	int echo_socket, time_socket;

	// Set up the echo socket
	echo_socket = setupServerSocket(PORT_ECHO);
	if(echo_socket < 0) {
		fprintf(stderr, "Failed to start echo server on port %d\n", PORT_ECHO);
		return EXIT_FAILURE;
	}

	// Set up the time socket
	time_socket = setupServerSocket(PORT_TIME);
	if(time_socket < 0) {
		fprintf(stderr, "Failed to start time server on port %d\n", PORT_TIME);
		return EXIT_FAILURE;
	}

	// Start up echo accept thread
	pthread_create(&echo_server_thread, NULL, echo_accept_thread, (void*)&echo_socket);
	
	// Start of time server accept thread
	pthread_create(&tod_server_thread, NULL, tod_accept_thread, (void*)&time_socket);

	// Wait for the accept threads to come back
	pthread_join(echo_server_thread, NULL);
	pthread_join(tod_server_thread, NULL);

	// Close open sockets
	close(echo_socket);
	close(time_socket);

	// Exit the server
	return EXIT_SUCCESS;
}
Пример #2
0
int main(int argc, char** argv)
{
    server = malloc(sizeof(struct sockaddr_in));
    int clientfd;
   
    //create share mem
    int MAX_CLIENTS = 1000;
    clientsID = shmget(IPC_PRIVATE, MAX_CLIENTS * sizeof(int), IPC_CREAT | 0666);
    numberOfConnectedClientsID = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666);
    
    //link to shared mem
    int* clients = shmat(clientsID, NULL, 0);
    int* numberOfConnectedClients = shmat(numberOfConnectedClientsID, NULL, 0);
    sem_t block;
    sem_init(&block, 0, 1);
    
    char* message = "hello";
    int pid;
    //setup the socket server
    setupServerSocket();
    
   while(1)
   {
    printf("Listening....\n");
    clientfd = accept(sockfd ,  (struct sockaddr *)server , &serverSize);
    printf("Client Connected.... %d\n", clientfd);
    
    //add this client to our array of clients
    sem_wait(&block);
    clients[(*numberOfConnectedClients)++] = clientfd;
    sem_post(&block);
    
    pid = fork();
    
    if(pid == 0)
    {
        break;
    }
   }
   
   //only do this if we are the child and have broken out of the infinite loop
   if(pid == 0)
   {
       childStuff(clientfd);
   }
}