0

hey guys,
i am trying to build a chat server.....but whn the server is sending login ack the server starts receiving out of order packets.....login ack is send when a username and password matches... format for username and password is username pasword:

plz help me short out the prob.

thnks in advance....

Attachments
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

char username[4];

#define size 256

int registration(int *sockfd )
{
	printf("----Registration----\n");
	int i;
	char *msg, cmd[] = "REGDREQ:";
	char *lineptr;
	size_t nbytes;
	
	lineptr = (char *) malloc( nbytes );
	
	printf("Username and password:");

	if(getline( &lineptr , &nbytes , stdin) == -1)
	{
		puts("Error");
	}

	sscanf(username , "%s" , lineptr);
	
	printf("\n");

	msg = (char *) malloc( size );
	strcat(msg , cmd);
	strcat(msg , lineptr);
	printf("%s", msg);
	i = send(*sockfd , msg , size , 0);
	if(i == -1)
	{
		perror("Unable to send:");
		exit(0);
	}

	i = recv(*sockfd , msg , size , 0);
	if(i == -1)
	{
		perror("Unable to send:");
		exit(0);
	}
	printf("%s\n", msg);
	
	if(strcasestr(msg , "REGACK") != NULL)
		return 1;
	else
		return 0;
}

int login(int *sockfd)
{
	printf("----Login----");
	int i;
	char *msg, cmd[] = "LOGIN:";
	char *lineptr;
	size_t nbytes;
	
	lineptr = (char *) malloc( nbytes );
	
	printf("Username and password:");

	if(getline( &lineptr , &nbytes , stdin) == -1)
	{
		puts("Error");
		exit(0);
	}

	msg = (char *) malloc( size );
	strcat(msg , cmd);
	strcat(msg , lineptr);
	printf("%s\n", msg);
	i = send(*sockfd , msg , size , 0);
	if(i == -1)
	{
		perror("Unable to send:");
		exit(0);
	}

	i = recv(*sockfd , msg , size , 0);
	if(i == -1)
	{
		perror("Unable to send:");
		exit(0);
	}
	printf("%s\n", msg);

	if(strcasestr(msg , "ACK") != NULL)
		return 1;
	else
		return 0;
}

int main()
{
	int sockfd;
	char *ch ,ans[] = "y" , pubchat[30];
	size_t nbytes ;

	ch = (char *) malloc( nbytes );

	struct sockaddr_in serv_addr;

	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd == -1)
	{
		perror("Unable to creat socket:");
		exit(0);
	}

	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = inet_addr("10.117.111.11");
	serv_addr.sin_port = htons(52000);

	int i;
	i = connect(sockfd , (struct sockaddr *)&serv_addr , sizeof(serv_addr));
	if(i == -1)
	{
		perror("Unable to connect:");
		exit(0);
	}

	while(1)
	{
		printf("Do you want to login(y/n)");
		if(getline( &ch , &nbytes , stdin) == -1)
		{
			puts("Error");
			exit(0);
		}

		while(strcasestr(ch , "y") != NULL)
		{
			i = login(&sockfd);
			
			if( i == 0)
			{
				printf("Do you want to login(y/n)");
				if(getline( &ch , &nbytes , stdin) == -1)
				{
					puts("Error");
					exit(0);
				}
			}
			else if( i == 1)
			{
				printf("----User is now Logged in----");
				sprintf(pubchat , "xterm -e ./public %s",username);
				
				if(fork() == 0)
					system(pubchat);
				break;
			}
		}

		printf("Do you want to register(y/n)");
		if(getline( &ch , &nbytes , stdin) == -1)
		{
			puts("Error");
			exit(0);
		}

		while(strcasestr(ch , "y") != NULL)
		{
			registration(&sockfd);
		
			printf("Do you want to register(y/n)");
			if(getline( &ch , &nbytes , stdin) == -1)
			{
				puts("Error");
				exit(0);
			}
		}
	}
	//close(sockfd);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <malloc.h>
#include <signal.h>
#include <sys/types.h>
#include <stddef.h>
#include <sys/socket.h> 
#include <netinet/in.h>
#include <arpa/inet.h>

#define size 256

int sockfd , newsockfd;

struct active_users
{
	int sockfd;
	char *type;
	char *username;
	int ifactive;
};

void * thread1(void *);
void loginack( int * , int  , char * );
void regdack(int * , int  , char * , char * , char *);

void r_close(int signo)
{
	printf("----In r_close()----\n");
	close(newsockfd);
	close(sockfd);
	exit(0);
}

int found(char *username)
{
	int errorno = 0;
	char uname[4] , pswd[6];
	FILE *fp;
	fp = fopen("REG_USER.dat", "r");
	while(fscanf(fp , "%s" , uname) != EOF)
	{
		if(strcmp(username , uname) == 0)
		{
			errorno = 1;
			break;
		}
		fscanf(fp , "%s" , pswd);		
	}
	fclose(fp);
	return errorno;
}

int match(char *username , char *password)
{
	printf("----Matching Database----\n");
	int errorno = 401;
	char uname[4] , pswd[6];
	FILE *fp;
	fp = fopen("REG_USER.dat", "r");
	while(fscanf(fp , "%s %s" , uname , pswd) != EOF)
	{	
		printf("%s %s %d\n", uname , pswd , errorno);
		if(strcmp(username , uname) == 0 )
		{
			if(strcmp(password , pswd) == 0)
			{
				printf("----Match found----\n");
				errorno = 101;
				break;
			}
			
			else if(strcmp(password , pswd) != 0)
			{
				printf("----Password wrong----\n");
				errorno = 402;
				break;
			}
		}
	}
	fclose(fp);
	printf("----End of matching Database----%d\n", errorno);
	return errorno; 
}

int regdreq(int *errorno , char *username , char *password)
{
	FILE *fp;
	char seperator[] = " ";
	int len;
	len = strlen(username);

	if(len >3)
	{
		*errorno = 10;
		return 0;
	}

	len = strlen(password);
	if(len >6)
	{
		*errorno = 15;
		return 0;	
	}

	*errorno = found(username);

	if(*errorno == 1)
	{
		*errorno = 401;
		return 0;
	}
	
	if(*errorno == 0)
	{
		fp = fopen("REG_USER.dat", "a+");
	
		fprintf(fp , "%s" , username);
		fprintf(fp , "%s" , seperator);
		fprintf(fp , "%s" , password);
		fclose(fp);

		*errorno = 100;
		return 0;
	}
}

void *thread(void *arg)
{
	int sockfd1;
	char *data;

	sockfd1 = newsockfd;
	data = (char *) malloc(size );

	data = (char *) arg ;

	int i , errno;
	while(1)
	{
		if(strcasestr(data , "REGDREQ") != NULL)
		{
			printf("%s\n" , data);
			char *token, *username, *password;
			char delimiter[] = ": ";
			int i ;
	
			token = strtok(data , delimiter);
			username = strtok(NULL , delimiter);
			password = strtok(NULL , delimiter);
	
			while(token != NULL)
				token  = strtok(NULL , delimiter);

			regdreq(&errno , username , password);
			regdack(&sockfd1 , errno , data , username , password);
			//printf("I am here");
		}

		if(strcasestr(data , "LOGIN") != NULL)
		{
			printf("%s\n" , data);
			char *token, *username, *password;
			char delimiter[] = ": ";
	
			token = strtok(data , delimiter);
			username = strtok(NULL , delimiter);
			password = strtok(NULL , delimiter);

			while(token != NULL)
				token  = strtok(NULL , delimiter);
			errno = match(username , password);
			if(errno == 101)
			{
				sprintf(data , "101:ACK:");
				i = send(sockfd1 , data , size , 0);
				if(i == -1)
				{
					perror("Unable to send:");
					exit(0);
				}
			}
			else
				loginack( &sockfd1 , errno , data );
			
			/*i = send(sockfd1 , data , size , 0);
			if(i == -1)
			{
				perror("Unable to send:");
				exit(0);
			}*/
		}

		printf("1I am here\n");
		i = recv(sockfd1 , data , size , 0);
		if(i == -1)
		{
			perror("Unable to receive");
			exit(1);
		}

		sleep(5);
		printf("Received message is %s\n",data);
		printf("I am here\n");
	}
}

int main()
{
	char msg[size];
	int i ;
	for(i = 0 ; i< size ; i++);
		msg[i] = '\0';

	socklen_t clilen;
	struct sockaddr_in serv_addr,cli_addr;
	
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd == -1)
	{
		perror("Unable to create socket:");
		exit(1); 
	}

	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = INADDR_ANY;
	serv_addr.sin_port = htons(52000);

	i = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
	if(i == -1)
	{
		perror("Unable to bind:");
		exit(1);
	}

	signal(SIGINT, r_close);
	//listen(sockfd, 4);

	while(1)
	{
		printf("----Listening----\n");
		listen(sockfd, 4);
		clilen = sizeof(cli_addr);
		newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
		if(newsockfd <= 0)
		{
			perror("Error in accept");
			exit(0);
		}
		
		i = recv(newsockfd , msg , size , 0);
		if(i == -1)
		{
			perror("Unable to receive");
			exit(1);
		}

		pthread_t  thid;

		i = pthread_create(&thid , NULL , thread , (void *) &msg);
		if(i == -1)
		{
			perror("Unable to create thread:");
			exit(0);
		}

		//pthread_join(thid , NULL);
	}
}

void regdack(int * sockfd , int errno , char *msg , char *username , char *password)
{
	int i;
	char *errmsg;
	errmsg = (char *) malloc (50 * sizeof(char));
	
	if(errno == 10)
	{
		errmsg = "Error no 10:Username too long:";
		sprintf(msg , "%s%s" , errmsg , username);
		printf("%s\n", msg);
		i = send(*sockfd , msg , size , 0);
		if(i == -1)
		{
			perror("Unable to send:");
			exit(0);
		}
	}

	if(errno == 15)
	{
		errmsg = "Error no 15:Password too long";
		sprintf(msg , "%s" , errmsg);
		printf("%s\n", msg);
		i = send(*sockfd , msg , size , 0);
		if(i == -1)
		{
			perror("Unable to send:");
			exit(0);
		}
	}

	if(errno == 401)
	{
		errmsg = "Error no 401:Username already exist";
		sprintf(msg , "%s%s" , username , errmsg);
		printf("%s\n", msg);
		i = send(*sockfd , msg , size , 0);
		if(i == -1)
		{
			perror("Unable to send:");
			exit(0);
		}
	}

	if(errno == 100)
	{
		errmsg = "REGDACK:";
		sprintf(msg , "%s%s" , errmsg , username);
		printf("%s\n", msg);
		i = send(*sockfd , msg , size , 0);
		if(i == -1)
		{
			perror("Unable to send:");
			exit(0);
		}
	}
}

void loginack(int *sockfd , int errno , char *msg )
{
	int i;
	char *errmsg;
	errmsg = (char *) malloc (50 * sizeof(char));
	
	if(errno == 401)
	{
		errmsg = "Error no 10:Username not found:";
		sprintf(msg , "%s" , errmsg);
		printf("%s\n", msg);
		i = send(*sockfd , msg , size , 0);
		if(i == -1)
		{
			perror("Unable to send:");
			exit(0);
		}
	}

	if(errno == 402)
	{
		errmsg = "Error no 402:Password Mismatch:";
		sprintf(msg , "%s" , errmsg);
		printf("%s\n", msg);
		i = send(*sockfd , msg , size , 0);
		if(i == -1)
		{
			perror("Unable to send:");
			exit(0);
		}
	}

	/*if(errno == 100)
	{
		errmsg = "ACK:";
		sprintf(msg , "%s" , errmsg);
		printf("%s\n", msg);
		i = send(*sockfd , msg , size , 0);
		if(i == -1)
		{
			perror("Unable to send:");
			exit(0);
		}
	}*/
	printf("----Acknowledgement sent----\n");
	return ;
}
2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by L7Sqr
0

I'd suggest you distill this a bit until you get things working. Then put the pieces together as they are verified. FOr instance, the fact that you have the following sequence of events

data = (char *) malloc(size );
data = (char *) arg ;

means to me that you have be editing this code in place for quite some time now (or you dont understand memory management and logical flow of execution). Try starting with just the parts that accept a connection (remove all threads for now) send a message to the server, respond, terminate connection. Once that is working, proceed to prompting for a login, passing that to the server, having the server check that and respond, then terminate the connection. And so on.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.