示例#1
0
void regis(int sockFd)
{
	/**
		Asks client for id and password to register with.
	**/
	//printf("In regis.\n");
	char sendMsg[MAXLINE], recvMsg[MAXLINE];
	int n;

	while(1)
	{
		// server asking for credentials
		//printf("In regis\n");
		//printf("server asking for credentials\n");
		
		if((n = read(sockFd, recvMsg, MAXLINE)) < 0)
			printError('r');
		if(n == 0)
			printError('t');
		if(strlen(recvMsg) == 0)
		{
			fputs("Server not responding. Closing connection.", stdout);
			exit(1);
		}
		printf("\nServer : \n%s\n", recvMsg);
		
		printf("\nClient : \n");
		fgets(sendMsg, MAXLINE, stdin);

		// providing credentials to server
		//printf("providing credentials to server\n");
		if((n = write(sockFd, sendMsg, MAXLINE)) < 0)
			printError('w');
		if(n == 0)
			printError('t');

		// response from the server
		//printf("response from the server\n");
		if((n = read(sockFd, recvMsg, MAXLINE)) < 0)
			printError('r');
		if(n == 0)
			printError('t');
		
		stringLower(recvMsg);
		printf("\nServer : \n%s\n", recvMsg);
		//printf("%d", strcmp("success", recvMsg));
		if(strcmp("registered successfully", recvMsg) == 0)
		{
		//	printf("Returning to start client\n");
			return;
		}
	}
}
long estimateImageContent(char *html, int *pNumberOfImages) {
	char *cursor = html;
	int numPixels = 0;

	*pNumberOfImages = 0;

	stringLower(html);

	//check if there is a background tag
	if (strstr(cursor, "background") != NULL) {
		*pNumberOfImages += 1;
	}

	//count img tags in hmtl and total pixel values
	while ((cursor = strstr(cursor, "<img")) != NULL) {
		cursor++;
		numPixels += dimInPixels(cursor);
		*pNumberOfImages += 1;
	}
	return numPixels;
}
示例#3
0
int startCommunication(int sockFd)
{
	/**
		Start the client communication with the server.
		Return: 1, when the user has successfully logged out.
	**/
	//printf("In startCommunication.\n");
	char sendMsg[MAXLINE], recvMsg[MAXLINE], discard[MAXLINE], temp[MAXLINE];
	int n, logout = 0, retVal = 0, i = 0, crash;

	// server providing options
	if((n = read(sockFd, recvMsg, MAXLINE)) < 0)
		printError('r');
	printf("\nServer :\n%s\n", recvMsg);

	r = rand()%10;		// these values are generated only once, otherwise
	crash = rand()%10;  // they disrupt the normal flow of the communication

	while(1)
	{
		// choosing an option
		printf("%d %d %d ", rndm, r, crash);
		fputs("\nClient : \n", stdout);
		fgets(sendMsg, MAXLINE, stdin);
		if((n = write(sockFd, sendMsg, MAXLINE)) < 0)
			printError('w');
		if(n == 0)
			printError('t');
		stringLower(sendMsg);
		
		if(strcmp("unexpected response", sendMsg) == EQUAL)
		{
			while(n = read(sockFd, recvMsg, MAXLINE))
			{
				printf("\nServer : \n%s\n", recvMsg);
			}
			if(n == 0)
				printError('t');
			return 1;
		}

		if(crash == SERVERCRASH) // simulate server crash : done
		{
			bzero(&temp, MAXLINE);
			bzero(&discard, MAXLINE);
			sprintf(discard, "%d", r);
			strcat(discard, sendMsg);
			retVal = poll(&fd, 1, TIMEOUT);
			if(retVal == 0)
			{
				fputs("Timeout\n", stdout);
				sleep(TIMEOUT/1000);
				i = 0;
				while(i++ < RETRIES)
				{
					fputs("Retrying. Sending request again.\n", stdout);
					n = write(sockFd, discard, MAXLINE);
					retVal = poll(&fd, 1, TIMEOUT);
					if(retVal)
						if((n = read(sockFd, temp, MAXLINE)) == 0)
							printError('t');
					sleep(TIMEOUT/1000);
				}
				if(strlen(temp) > 0)
					fputs(temp, stdout);
				fputs("Server seems unreachable.\n", stdout);
				sleep(1);
				fputs("Logging out\n", stdout);
				return 1;
			}
		}

		if(rndm == r) // simulate timeout and server and client out of sync
		{
			r = 11;
			retVal = poll(&fd, 1, TIMEOUT);
			if(retVal == 0)
			{
				fputs("Timeout\n", stdout);
				sleep(1);
				i = 0;
				while(i++ < RETRIES)
				{
					fputs("Retrying. Sending again.\n", stdout);
					n = write(sockFd, sendMsg, MAXLINE);
					retVal = poll(&fd, 1, TIMEOUT);
					if(retVal)
						if((n = read(sockFd, recvMsg, MAXLINE)) == 0)
							printError('t');
					sleep(1);
				}
			}
			else
				if((n = read(sockFd, recvMsg, MAXLINE)) < 0)
					printError('r');
				if(n == 0)
					printError('t');
		}
		else
		{
			if((n = read(sockFd, recvMsg, MAXLINE)) < 0)
				printError('r');
			if(n == 0)
				printError('t');
		}
		printf("\nServer : \n%s", recvMsg);
		stringLower(recvMsg);
		if(strcmp("logout successful.", recvMsg) == 0)
		{
			logout = 1;
			return logout;
		}
		
	}
}
示例#4
0
bool stringToBool(std::string sourceString)
{
  stringLower(sourceString);

  return ((sourceString == "true") || (sourceString != "0"));
}