Hey guys,
Im writing this client/server chat program. Its in C and runs on UNIX. Its multiproccessed using the fork() function. My problem is that I cant seem to get my child functions to communicate with each other. I have a global 2d array sockets[][] that holds the socket for each client. Here is my code. Its more of pseudo code because its really the concept thats getting me and not the coding of it. So don't look at it for syntax errors just at the concepts/execution. I can't seem to put my finger on whats happening exactly and I think that's where my mistake is.
SERVER.c
Code:#define BUFFSIZE 256
//global 2d array to hold client sockets
int sockets[BUFFSIZE][BUFFSIZE];
main(){
*code*
//code above sets up the socket for listening. i.e bind/listen
/*code below is a loop that constantly accepts new clients and forks() a new process for each new client. Also adding the new socket for each new client to the sockets[][] array.*/
while(1){
socket = accept();
sockets[0][index] = socket;
index++;
pid = fork();
if (pid == 0)
dostuff(socket);
}
}
dostuff(int socket){
//recvs a msg from the client of this child process
recv(socket, buff);
/*for loop that iterates through the sockets[][] and sends the recved buff from the client of this child process to every client in the sockets[][] array*/
for(i=0; i<BUFFSIZE;i++){
int sd;
//whenthere are no more sockets the for loop ends
if((sd = socket[0][i]) == 0)
break;
else
send(sd, buff);
}
}
the client code is kinda trivial. It just connects and send/recvs in a loop.
Any help is greatly appreciated.