Thread: multi-threaded chess using winsock

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    8

    multi-threaded chess using winsock

    I've been working on a small network based chess application. I managed to create a server that can handle multiple connections, however I don't know how to send data from one client to another through the server. With this setup, it only send the message to one client.
    I can also send you the solution file. Any help would be appreciated. Thanks

    Here is the partial Server implementation;
    Basically it creates new thread for each new client;

    Code:
    //function to handle our Socket on its own thread.
    //param- SOCKET* that is connected to a client
    DWORD WINAPI HandleSocket(void* param)
    {
    string test;
    
    SOCKET s = (SOCKET)param;
    User temp;
    temp._socket = (SOCKET)param;
    temp._inGame = false;
    userlist.add(&temp);
    
    std::cout<<"connection"<<endl;
    int bytesread = 0;  
    int byteswrite=0;
    
    while(true)
    {
        //receive
        bytesread = recv(s, reinterpret_cast<char*>(test.c_str()), BUF_LEN, 0);
    
        //error check   
        if(bytesread == SOCKET_ERROR)
        {
            std::cout << WSAGetLastError();
            //shutdown and close on error
            shutdown(s, SD_BOTH);
            closesocket(s);
            return 0;
        }
    
    
        //check for socket being closed by the client
        if(bytesread == 0)
        {
            //shutdown our socket, it closed
            shutdown(s, SD_BOTH);
            closesocket(s);
            return 0;
        }
    
        byteswrite = send(s, "test" , 255 , 0);
        if(byteswrite == SOCKET_ERROR)
        {
            std::cout << WSAGetLastError();
            //shutdown and close on error
            shutdown(s, SD_BOTH);
            closesocket(s);
            return 0;
        }
    
        test.clear();
     }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > bytesread = recv(s, reinterpret_cast<char*>(test.c_str()), BUF_LEN, 0);
    Two things wrong here
    1. c_str() is supposed to be 'const', which means you shouldn't be modifying it.
    2. How do you know you have BUF_LEN of it anyway?

    > byteswrite = send(s, "test" , 255 , 0);
    Also, this sends 250 bytes of garbage, assuming it doesn't first run off the end of memory and generate a segfault.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    thanks for the answer I corrected those parts, but still don't know how to send a data from one client to another client through the server.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You do a recv() from one client, and a send() to the other client.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    yes I know that, but the problem is recv() blocks the code, so its impossible to loop between different client sockets. it keeps waiting on the first client socket, which makes it impossible to receive data from others.

  6. #6
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Quote Originally Posted by akurdas View Post
    yes I know that, but the problem is recv() blocks the code, so its impossible to loop between different client sockets. it keeps waiting on the first client socket, which makes it impossible to receive data from others.
    Look into either threads or making the socket asynchronous(using select).

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    I've been working on it since yesterday, but no result.
    All the files are inside the attachment maybe someone can point me to the right direction. All I need to do is sending data from one client to another.
    Attached Files Attached Files

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Rather than post your completed game (to date), which has a lot of stuff in it which is of no interest to us, write a separate simple test program where you can focus on experimenting with just this one issue.

    A server that just waits for connections, and echoes everything it receives to all connected clients.
    A client (which you run several instances of) which send a single string (and perhaps a process ID) to the server, and echoes anything the server sends back.

    When you understand how all this works, then apply that idea to your real code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    I know I have to communicate with all of the other client threads connected. What I don't know and need help with is how to communicate with all of the client threads to send the message out

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    30
    Each user gets a new thread??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi-threaded mayhem
    By IceDane in forum C Programming
    Replies: 8
    Last Post: 03-24-2008, 08:44 AM
  2. Going from single-threaded to multi-threaded
    By Dino in forum C Programming
    Replies: 11
    Last Post: 03-23-2008, 01:14 PM
  3. gdb problem in multi-threaded app
    By IfYouSaySo in forum Linux Programming
    Replies: 1
    Last Post: 10-12-2006, 08:22 PM
  4. Multi-Threaded Sockets
    By XSquared in forum Networking/Device Communication
    Replies: 9
    Last Post: 08-28-2003, 09:54 PM
  5. Single-threaded Winsock
    By SMurf in forum Windows Programming
    Replies: 10
    Last Post: 07-02-2003, 04:14 PM

Tags for this Thread