Member Avatar for begame

Hey there!
So I have am writing a multi-chat client for my college course. I have completed most of it, however there is a problem which I am unable to identify. When I conect multiple clients to the server only the the last client to connect is able to send messages. All the other ones can receive but cannot send a message. When they do (for some wacky reason) the server prints the message output on the screen.

pid_t pid;
	pid=fork();
	if(pid<0){
		fprintf(stderr,"Fork Failed\n");
	}
	else if(pid==0){
		while(1){
			bzero(buffer,256);
			n = read(newsockfd[temp],buffer,255);

			if (n < 0){
				error("ERROR reading from socket\n");
			}
			else{
				printf("Received message %s\n",buffer);
				int j=(int)(buffer[0]-'0');
				buffer[0]='0'+temp;
				printf("Message to %d\n",j);
				n = write(newsockfd[j],buffer,strlen(buffer));
				if (n < 0) error("ERROR writing to socket");
				else printf("Message Sent\n");

			}

		}

Thanks in advance. If someone is interested in helping I'll post more about the program itself.

Regards,
Digvijay

Member Avatar for begame

[EDIT] I have explored more. And the issue seems like this: The first client can only send a message to himself. The 2nd client can send message to the 1st and himself. And the 3rd client can send message to the 1st, the 2nd and to himself. So if the 2nd client tries to send a message to the 3rd then the message is displayed in the server console. And it never reached the 3rd client. Kindly help me folks!

I am posting the code for the entire RECURSIVE server function:

void rServer(int sockfd,struct sockaddr_in serv_addr,int i,int ** newsockfd){
	int n,temp;
	temp=i;
	printf("Listening for %d\n",temp);
	listen(sockfd,5);
	char buffer[256];
	clilen[temp] = sizeof(cli_addr[temp]);
	*(newsockfd[temp]) = accept(sockfd,(struct sockaddr *) &(cli_addr[temp]),&(clilen[temp]));
	if (*(newsockfd[temp]) < 0)
		error("ERROR on accept\n");
	printf("%d client request accepted\n",temp);
	/*display_ca();
	CliAddrAdd(cli_addr,newsockfd);
	display_ca();*/
	pid_t pid;
	pid=fork();
	if(pid<0){
		fprintf(stderr,"Fork Failed\n");
	}
	else if(pid==0){
		while(1){
			bzero(buffer,256);
			n = read(*(newsockfd[temp]),buffer,255);

			if (n < 0){
				error("ERROR reading from socket\n");
			}
			else{
				printf("Received message %s\n",buffer);
				int j=(int)(buffer[0]-'0');
				buffer[0]='0'+temp;
				/*display_ns();
				nextSendAdd(buffer);
				display_ns();*/
				printf("Message to %d\n",j);
				n = write(*(newsockfd[j]),buffer,strlen(buffer));
				if (n < 0) error("ERROR writing to socket");
				else printf("Message Sent\n");

			}

		}
	}
	else{
		rServer(sockfd,serv_addr,++i,newsockfd);
	}
}

Hey there!
So I have am writing a multi-chat client for my college course. I have completed most of it, however there is a problem which I am unable to identify. When I conect multiple clients to the server only the the last client to connect is able to send messages. All the other ones can receive but cannot send a message. When they do (for some wacky reason) the server prints the message output on the screen.

pid_t pid;
	pid=fork();
	if(pid<0){
		fprintf(stderr,"Fork Failed\n");
	}
	else if(pid==0){
		while(1){
			bzero(buffer,256);
			n = read(newsockfd[temp],buffer,255);

			if (n < 0){
				error("ERROR reading from socket\n");
			}
			else{
				printf("Received message %s\n",buffer);
				int j=(int)(buffer[0]-'0');
				buffer[0]='0'+temp;
				printf("Message to %d\n",j);
				n = write(newsockfd[j],buffer,strlen(buffer));
				if (n < 0) error("ERROR writing to socket");
				else printf("Message Sent\n");

			}

		}

Thanks in advance. If someone is interested in helping I'll post more about the program itself.

Regards,
Digvijay

I am new but I prefer multi-thread, but not multi-process. :)

Member Avatar for begame

Hehe, I forgot fork was used to create different processes. And all this while i was thinking why i couldn't get consistency across the global variable. So i just created threads. And everything it working fine. It works great!

Digvijay
PS:I thought i should just post this to complete the thread.

[EDIT] I have explored more. And the issue seems like this: The first client can only send a message to himself. The 2nd client can send message to the 1st and himself. And the 3rd client can send message to the 1st, the 2nd and to himself. So if the 2nd client tries to send a message to the 3rd then the message is displayed in the server console. And it never reached the 3rd client. Kindly help me folks!

I am posting the code for the entire RECURSIVE server function:

void rServer(int sockfd,struct sockaddr_in serv_addr,int i,int ** newsockfd){
	int n,temp;
	temp=i;
	printf("Listening for %d\n",temp);
	listen(sockfd,5);
	char buffer[256];
	clilen[temp] = sizeof(cli_addr[temp]);
	*(newsockfd[temp]) = accept(sockfd,(struct sockaddr *) &(cli_addr[temp]),&(clilen[temp]));
	if (*(newsockfd[temp]) < 0)
		error("ERROR on accept\n");
	printf("%d client request accepted\n",temp);
	/*display_ca();
	CliAddrAdd(cli_addr,newsockfd);
	display_ca();*/
	pid_t pid;
	pid=fork();
	if(pid<0){
		fprintf(stderr,"Fork Failed\n");
	}
	else if(pid==0){
		while(1){
			bzero(buffer,256);
			n = read(*(newsockfd[temp]),buffer,255);

			if (n < 0){
				error("ERROR reading from socket\n");
			}
			else{
				printf("Received message %s\n",buffer);
				int j=(int)(buffer[0]-'0');
				buffer[0]='0'+temp;
				/*display_ns();
				nextSendAdd(buffer);
				display_ns();*/
				printf("Message to %d\n",j);
				n = write(*(newsockfd[j]),buffer,strlen(buffer));
				if (n < 0) error("ERROR writing to socket");
				else printf("Message Sent\n");

			}

		}
	}
	else{
		rServer(sockfd,serv_addr,++i,newsockfd);
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.