저는 C++ & Winsock2를 사용하여 서버 및 클라이언트 응용 프로그램을 모두 만들었습니다. 현재 별도의 스레드를 만들어 여러 클라이언트 연결을 처리합니다.특정 winsock 클라이언트로 데이터를 보내고 선택하는 방법
두 개의 클라이언트가 서버에 연결됩니다. 양쪽 모두 연결 한 후에는 연결된 첫 번째 클라이언트에게만 메시지를 보내고 응답을받을 때까지 기다렸다가 두 번째 클라이언트에 별도의 메시지를 보냅니다. 문제는 내가 연결된 첫 번째 클라이언트를 어떻게 타겟팅 할 수 있는지 알 수 없습니다. 내가 지금 가지고있는 코드는 두 개의 연결을 허용 그러나 메시지가 클라이언트로 전송 2.
누군가가 내가 특정 클라이언트) (보내기 사용할 수있는 방법에 대한 아이디어, 그래서 내게 줄시겠습니까? 연결을 허용하고
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() 함수는 (클라이언트에 명령을 렌더링 전송)
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;
}
'SendRender()'는 (는) 귀하의 기능 중 하나입니까? 관련 코드를 보여줘야합니다. 전 세계적인 변수 문제가 될 수 있다고 생각합니다. 코드에 다른 논리 문제가 있습니다. 스레드에 "대기 중"이 전혀 없습니다. – Nbr44
첫 번째 인수로 적절한 파일 설명자를 사용하여 send()를 호출하는 것이 문제가되지 않습니까? 즉, 두 개의 연결은 각각 별도의 파일 설명자로 표시되므로 원하는 하나에서 send()를 호출하면됩니다. –
의견을 보내 주셔서 감사합니다. 미안, SendRender() 코드를 추가했습니다. -Nbr442 '대기 중'에 관해서는, 나는 Winsock에 익숙하지 않고 여러 스레드를 사용하고있어서, 여기에서 기다리는 것을 어떻게 구현해야하는지 모르겠습니다. -Jeremy send() 함수를 살펴 봤지만 첫 번째 인수가 적절하다는 것을 알 수 있지만 ClientSocket이 현재 사용되는 방식을 기반으로 구현하는 방법을 잘 모르겠습니다. 나는 다른 곳에서 멀티 스레드 코드를 가져 와서 나의 목적에 맞게 약간 수정했다. – Vault13