Thread: Concurrent chat, Sockets

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    30

    Concurrent chat, Sockets

    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.
    Last edited by jakemott; 11-29-2008 at 09:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming chat client, need some help (sockets & threads)
    By lalilulelo17 in forum Linux Programming
    Replies: 1
    Last Post: 04-19-2008, 04:01 AM
  2. Requesting Java Chat Client Maintenence
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-02-2003, 01:57 PM
  3. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 11:49 AM
  4. SO close to finishing chat program...!
    By Nakeerb in forum C++ Programming
    Replies: 13
    Last Post: 10-26-2002, 12:24 PM
  5. Rough Portable Chat Design Sketch
    By ggs in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-27-2001, 07:44 AM