Thread: Multithreaded server, where to start?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    31

    Multithreaded server, where to start?

    Hi all,

    I've written a pretty basic server. However now I've ran into a problem (and well I'm on an extremely short deadline so hopefully you guys can help me out).

    Basically, I can connect with one client. However because accept blocks and I am waiting for other connections I only recieve data once. Can someone, hell even psuedo code will be awesome, can just show me an example (if you could help with select() example that'd be sweet) of how I can accept multiple connections and still recieve data.

    I just don't have a clue about threads, well I do, but only theory, never used them. And so I have about another 5 hours of fumbling around with my code searching the web for examples and tutorials before I'm screwed well and truely..

    Thanks in advance, and even if it's not in time I'd definitely like to know how to complete this.

    [edit:] I'm not sure if it helps, code usually does I guess.. But this is what I'm doing in the server at the moment-
    Code:
    while(1){
        if(listen(this->listen_socket, 10) != SOCKET_ERROR){
            this->acceptconnection();
            this->recievedata();
            cout << "Connection accepted" << endl;
        }else{
            cout << "SOCKET_ERROR on listen_socket" << endl;
        }
    }
    [edit2:]Geez that formatting screwed up royally
    [edit again:] Also, have I got listen() in the wrong place there? I don't need it in the loop do I? I've been going for nearly 24 hours straight without barely any knowledge of winsock so sorry if that was a really stupid question.
    Last edited by Ichmael™; 02-25-2006 at 11:43 AM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    31
    Ok, so I've just kept working at it. So tired now, but oh so close. Stuck again though. I have the server creating threads now, and I have each socket kept track of in a class instance which in turn is in a vector to simplicity of managing all clients easily.

    But, now ... How do I listen for incoming messages on each client properly? Do I just have an infinite (well almost) loop in the threadproc checking for data? Is this acceptable?

    Also, perhaps someone can answer me this (I think it may have something to do with critical data or something I dunno anything about it really.

    Code:
    DWORD WINAPI listenroutine(){
       cout << "Listener thread started\n";
       for(;;){
          if(client.keeplistening){
              char *data = client.recievedata();
              if(data != NULL){
                   cout << "Data recieved: " << data << endl;
                   cout << "Resizing strmsgs\n";
                   char *tmp = (char*)malloc(sizeof(strmsgs));
                   strcpy(tmp, strmsgs);
                   strmsgs = (char*)realloc(strmsgs, sizeof(tmp)+sizeof(data)+1);//+1 for safety
                   strcpy(strmsgs, tmp);
                   cout << "concat a '\\n' to strmsgs \n";
                   strcat(strmsgs, "\n");
                   cout << "--- OK!\nconcat data to strmsgs\n";
                   strcat(strmsgs, data);
                   cout << "--- OK!\n";
                   SetWindowText(msgsbox,strmsgs);
                   UpdateWindow(msgsbox);
                   cout << client.keeplistening << endl;
               }else{
                   cout << "data: " << data << endl;
               }
           }else{
               SetEvent(hEvent);
           }
       }   
       cout << "Listener thread ended\n";
       return 0;
    }
    So, client is a class in global scope. If in my wndproc the window closes client.keepclosing = false, however this never seems to do anything in the threadproc.. I stuck that event in there to make the program wait for it to finish up before exiting as I was getting a segfault. I hope that was the correct thing to do. But I've never touched threads so I dunno once again.

    Any help at all would be awesome

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with multithreaded sockets program
    By ceclauson in forum C Programming
    Replies: 3
    Last Post: 12-13-2008, 04:15 PM
  2. Commerical MMORPG Developer Opp
    By Th3Guy in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 01-22-2007, 11:28 AM
  3. Server deadlocks, Nonblocking IO, or Timeout?
    By 0xception in forum C Programming
    Replies: 6
    Last Post: 01-03-2005, 12:49 PM
  4. Web Server in Router Network...
    By Aidman in forum Tech Board
    Replies: 15
    Last Post: 01-17-2003, 10:24 PM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM