Example #1
0
int main(int argc, char** argv){
	int sockListen, errorIndicator;
	int maxClientFd;
	int i, j;
	struct sockaddr_in myaddr;
	struct sockaddr addrBind;
	struct in_addr inAddr;
	fd_set fdSetRead, fdSetWrite;
	struct timeval timeout = { 60, 0 };

	int h;
	int port;

	if (argc < 4 || argc > 5){
		printf(ILLEGAL_ARGS);
		exit(1);
	}

	for (j = 1; j < 4; j++){
		sscanf(argv[j], "%d", &h);
		game.heaps[j - 1] = h;
	}
	
	if (argc == 5) sscanf(argv[4], "%d", &port);
	else port = DEFAULT_PORT;

	game.valid = 1;
	game.win = -1;
	//game.numOfPlayers = p;
	game.msg = 0;
	//game.moveCount = 0;


	// Set listner. accepting only in main loop
	sockListen = socket(AF_INET, SOCK_STREAM, 0);
	checkForNegativeValue(sockListen, "socket", sockListen);
	addrBind.sa_family = AF_INET;
	myaddr.sin_family = AF_INET;
	myaddr.sin_port = htons(port);
	inAddr.s_addr = htonl(INADDR_ANY);
	myaddr.sin_addr = inAddr;
	errorIndicator = myBind(sockListen, &myaddr, sizeof(addrBind));
	checkForNegativeValue(errorIndicator, "bind", sockListen);
	errorIndicator = listen(sockListen, 2);
	checkForNegativeValue(errorIndicator, "listen", sockListen);

	while (1){
		// clear set and add listner
		maxClientFd = sockListen;
		FD_ZERO(&fdSetRead);
		FD_ZERO(&fdSetWrite);
		FD_SET(sockListen, &fdSetRead);

		// add all clients to fdSetRead
		for (i = 0; i < conPlayers; i++){
			FD_SET(ClientsQueue[i].fd, &fdSetRead);
			if (strlen(ClientsQueue[i].writeBuf) > 0){
				FD_SET(ClientsQueue[i].fd, &fdSetWrite);
			}
			if (ClientsQueue[i].fd > maxClientFd) maxClientFd = ClientsQueue[i].fd; 
		}

		// TODO: need to add timeout
		select(maxClientFd + 1, &fdSetRead, &fdSetWrite, NULL, &timeout);
		if (FD_ISSET(sockListen, &fdSetRead)){
			int fdCurr = accept(sockListen, (struct sockaddr*)NULL, NULL);
			checkForNegativeValue(fdCurr, "accept", fdCurr);
			if (fdCurr >= 0){
				if ((conPlayers) == 2) SendCantConnectToClient(fdCurr);
				else addClientToQueue(fdCurr);
			}
		}

		// Service all the sockets with input pending.
		for (i = 0; i < conPlayers; ++i){
			if (FD_ISSET(ClientsQueue[i].fd, &fdSetRead)){
				errorIndicator = receiveFromClient(i);
				if (errorIndicator < 0){
					close(ClientsQueue[i].fd);
					notifyOnDisconnectionToPlayer(i);
				}
				else if (errorIndicator == 1){
					handleReadBuf(i);
				}

			}

			if (FD_ISSET(ClientsQueue[i].fd, &fdSetWrite)){
				errorIndicator = sendToClient(i);
				if (errorIndicator < 0){
					close(ClientsQueue[i].fd);
					notifyOnDisconnectionToPlayer(i);
				}
			}
		}
	}
}
Example #2
0
int main(int argc, char** argv){
	int sockListen, errorIndicator;
	int maxClientFd;
	int i;
	struct sockaddr_in myaddr;
	struct sockaddr addrBind;
	struct in_addr inAddr;
	/*struct socklen_t *addrlen;*/
	/*char buf[MSG_SIZE];*/
	fd_set fdSetRead, fdSetWrite;

	int M, port, p;
	/*struct clientMsg clientMove;*/

	/*Region input Check*/

	if(argc<=3 || argc>=6){
		//printf("Illegal arguments\n");
		exit(1);
	}

	sscanf(argv[1],"%d",&p);

	/*//printf("argv[1] %s\n", argv[2]);*/
	sscanf(argv[2],"%d",&M);

	if( strcmp(argv[3],"0") ==0 ){
		game.isMisere =0;
	}
	else if(strcmp(argv[3],"1") ==0 ){
		game.isMisere=1;
	}
	else{
		//printf("Illegal arguments. Misere should be 0/1\n");
		exit(1);
	}

	if(argc==5){
		sscanf(argv[4],"%d",&port);
	}
	else{
		port =6325;
	}

	game.heapA = M;
	game.heapB = M;
	game.heapC = M;
	game.heapD = M;
	game.valid=1;
	game.win = -1;
	game.numOfPlayers = p;
	game.msg = 0;
	game.moveCount = 0;

	
	/*//printf("Set all arguments, start server\n");*/

	// Set listner. accepting only in main loop

	sockListen = socket(AF_INET, SOCK_STREAM, 0);
	checkForNegativeValue(sockListen, "socket", sockListen);
	/*//printf("Succesfully got a socket number: %d\n", sockListen);*/
	addrBind.sa_family = AF_INET;
	myaddr.sin_family = AF_INET;
	myaddr.sin_port = htons(port);
	inAddr.s_addr = htonl( INADDR_ANY );
	myaddr.sin_addr = inAddr;
	errorIndicator=myBind(sockListen, &myaddr, sizeof(addrBind));
	checkForNegativeValue(errorIndicator, "bind", sockListen);
	/*//printf("Succesfully binded %d\n", sock);*/

	errorIndicator=listen(sockListen, 9);
	checkForNegativeValue(errorIndicator, "listen", sockListen);
	/*//printf("Succesfully started listening: %d\n", sock);*/
	
	while(1){
		// clear set and add listner
		
		maxClientFd = sockListen;
		FD_ZERO(&fdSetRead);
		FD_ZERO(&fdSetWrite);
		FD_SET(sockListen, &fdSetRead);
		//printf("listening socket is:%d\n",sockListen);

		// add all clients to fdSetRead
		//printf("clients to add. players:%d, viewers:%d\n",conPlayers,conViewers);
		for(i=0 ; i< conViewers + conPlayers ; i++){
			//printf("Adding fd:%d to read\n",ClientsQueue[i].fd);
			FD_SET(ClientsQueue[i].fd, &fdSetRead);
			if(strlen(ClientsQueue[i].writeBuf) > 0){
				//printf("Adding fd:%d to write\n",ClientsQueue[i].fd);
				FD_SET(ClientsQueue[i].fd, &fdSetWrite);
			}
			else{
				//printf("ClientsQueue[i].writeBuf = %s\n",ClientsQueue[i].writeBuf);
			}
			if(ClientsQueue[i].fd > maxClientFd) { maxClientFd = ClientsQueue[i].fd; }
		}

		// TODO: need to add timeout
		//printf("Select...\n");
		select(maxClientFd+1, &fdSetRead, &fdSetWrite, NULL, NULL);
		//printf("Exit select...fdReady = %d\n",fdready);

		// //printf("%s...\n",strerror(errno));
		if (FD_ISSET (sockListen, &fdSetRead))
      		{
      			 //printf("Reading from sock listen\n");
      			 int fdCurr = accept(sockListen, (struct sockaddr*)NULL, NULL );
      			 checkForNegativeValue(fdCurr, "accept", fdCurr);

				 if(fdCurr >= 0){
				 	//printf("Got a valid FD after accept, fd:%d\n",fdCurr);
				 	if( (conViewers + conPlayers) == 9){
					// too much connected clients. sending "can't connect" to client
					SendCantConnectToClient(fdCurr);
					}
					else if(conPlayers == p){
						// max amount of players. new client is a viewer
						//printf("new client is a viewer: fd:%d\n",fdCurr);
						addClientToQueue(fdCurr, 0);
					}
					else{
						// new client is a player
						//printf("new client is a player: fd:%d\n",fdCurr);
						addClientToQueue(fdCurr, 1);
					}
				 }
			}

		/* Service all the sockets with input pending. */
     	for (i = 0; i < conPlayers+conViewers; ++i){
     		//printf("checking: i=%d, fd=%d\n", i, ClientsQueue[i].fd);
	      	if (FD_ISSET (ClientsQueue[i].fd, &fdSetRead)){
	      		//printf("sock %d is ready for read\n", ClientsQueue[i].fd);
	      		errorIndicator = receiveFromClient(i);
	      		if(errorIndicator < 0){
	 				close(ClientsQueue[i].fd);
					delClientFromQueue(ClientsQueue[i].fd);
	 			}
	 			else if(errorIndicator == 1){
	 				//printf("handling received data\n"); 
	 				handleReadBuf(i);
	 			}

	      	}

	      	if (FD_ISSET (ClientsQueue[i].fd, &fdSetWrite)){
	      		//printf("sock %d is ready for write\n", ClientsQueue[i].fd);
	      		errorIndicator = sendToClient(i);
	      		if(errorIndicator < 0){
					close(ClientsQueue[i].fd);
					delClientFromQueue(ClientsQueue[i].fd);
	      		}
	      	}
      }
	}
}