c++ - How to select and send data to a specific winsock client -
i have used c++ & winsock2 create both server , client applications. handles multiple client connections creating separate threads.
two clients connect server. after both have connected, need send message first client connected, wait until response has been received, send separate message second client. trouble is, i don't know how can target first client connected. code have @ moment accepts 2 connections message sent client 2.
can please give me ideas on how can use send() specific client? thanks
code accepts connections , starts new threads
socket tempsock = socket_error; // create socket called tempsock , assign value of socket_error while (tempsock == socket_error && numcc !=2) // until client has connected, wait client connections { cout << "waiting clients connect...\n\n"; while ((clientsocket = accept(socket, null, null))) { // create new thread accepted client (also pass 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 number of connected clients cout << "clients connected: " << numcc << endl << endl; // output number of clients connected server if (numcc <2) { cout << "waiting additional clients connect...\n\n"; } if (numcc ==2) { sendrender(); // client 1??????????? // wait client render complete , receive done message memset(bufferreply, 0, 999); // set memory of buffer int indatalength = recv(clientsocket,bufferreply,1000,0); // receive data server , store in buffer response = bufferreply; // assign contents of buffer string var 'message' cout << response << ". " << "client 1 render cycle complete.\n\n"; sendrender(); // client 2???????????? } return 0; }
sendrender() function (sends render command client)
int sendrender() { // create message send client initialise rendering char *szmessage = "render"; // send render message first client isendresult = send(clientsocket, szmessage, strlen(szmessage), 0); // how send client 1??? if (isendresult == socket_error) { // display error if unable send message cout << "failed send message client " << numcc << ": ", wsagetlasterror(); closesocket(socket); wsacleanup(); return 1; } // notify user render command has been sent cout << "render command sent client " << numcc << endl << endl; return 0; }
you can provide both wait function , control function thread adding waitforsingleobject (or waitformultipleobjects) call. api calls suspend thread until other thread sets event handle. api return value tells event handle set, can use determine action take.
use different event handle each thread. pass in thread need struct contains both event handle , socket handle passing now. passing pointer struct thread way to, in effect, pass 2 parameters.
your main thread need use createevent initialize thread handles. after both sockets connected set 1 event (setevent), triggering first thread.
Comments
Post a Comment