سؤال

I have used C++ & Winsock2 to create both server and client applications. It currently handles multiple client connections by creating separate threads.

Two clients connect to the server. After both have connected, I need to send a message ONLY to the first client which connected, then wait until a response has been received, send a separate message to the second client. The trouble is, I don't know how I can target the first client which connected. The code I have at the moment accepts two connections but the message is sent to client 2.

Can someone please give me so ideas on how I can use Send() to a specific client? Thanks

Code which accepts the connections and starts the new threads

        SOCKET TempSock = SOCKET_ERROR;                 // create a socket called Tempsock and assign it the value of SOCKET_ERROR
        while (TempSock == SOCKET_ERROR && numCC !=2)   // Until a client has connected, wait for client connections
        {
            cout << "Waiting for clients to connect...\n\n";

            while ((ClientSocket = accept(Socket, NULL, NULL))) 
            {
            // Create a new thread for the accepted client (also pass the accepted client socket).
            unsigned threadID;
            HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, &ClientSession, (void*)ClientSocket, 0, &threadID);
            }
        }

ClientSession()

    unsigned __stdcall ClientSession(void *data)
    {
        SOCKET ClientSocket = (SOCKET)data;

        numCC ++; // increment the number of connected clients

        cout << "Clients Connected: " << numCC << endl << endl; // output number of clients currently connected to the server

        if (numCC <2) 
            {
                cout << "Waiting for additional clients to connect...\n\n";
            }   

        if (numCC ==2)
        {
        SendRender();       // ONLY TO CLIENT 1???????????


            // wait for client render to complete and receive Done message back
            memset(bufferReply, 0, 999);                                // set the memory of the buffer
            int inDataLength = recv(ClientSocket,bufferReply,1000,0);   // receive data from the server and store in the buffer
            response = bufferReply;                                     // assign contents of buffer to string var 'message'

            cout << response << ". " << "Client 1 Render Cycle complete.\n\n";

            SendRender(); // ONLY TO CLIENT 2????????????
        }
        return 0;
    }

Sendrender() function (sends render command to the client)

    int SendRender()
    {
        // Create message to send to client which will initialise rendering
            char *szMessage = "Render";

            // Send the Render message to the first client
            iSendResult = send(ClientSocket, szMessage, strlen(szMessage), 0);      // HOW TO SEND ONLY TO CLIENT 1???
            if (iSendResult == SOCKET_ERROR) 
        {
                // Display error if unable to send message
                cout << "Failed to send message to Client " << numCC << ": ", WSAGetLastError();
                closesocket(Socket);
                WSACleanup();
                return 1;
        }
            // notify user that Render command has been sent
            cout << "Render command sent to Client " << numCC << endl << endl;

            return 0;
    }
هل كانت مفيدة؟

المحلول

You can provide both a wait function and a control function to the thread by adding a WaitForSingleObject (or WaitForMultipleObjects) call. Those API calls suspend the thread until some other thread sets an event handle. The API return value tells you which event handle was set, which you can use to determine which action to take.

Use a different event handle for each thread. To pass it in to a thread you will need a struct that contains both the event handle and the socket handle you are passing now. Passing a pointer to this struct into the thread is a way to, in effect, pass two parameters.

Your main thread will need to use CreateEvent to initialize the thread handles. Then after both sockets are connected it would set one event (SetEvent), triggering the first thread.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top