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