Answer the question
In order to leave comments, you need to log in
How to get ip-addresses of all running servers via UDP?
Understanding winsock and UDP. I am writing on one PC, there is no local network. On the client I send a broadcast request:
sockaddr_in broadcast;
// разрешение на широковещательный запрос
int enableBroadcast = 1;
setsockopt(ClientSocket, SOL_SOCKET, SO_BROADCAST, (char*)&enableBroadcast, sizeof(enableBroadcast));
broadcast.sin_family = AF_INET;
broadcast.sin_port = htons(Port);
broadcast.sin_addr.s_addr = INADDR_BROADCAST;
// определенное сообщение,на которое сервер ответит информацией о себе
char testmessage[] = "test\n";
sendto(ClientSocket, testmessage,sizeof(testmessage), 0, (sockaddr*)&broadcast, sizeof(broadcast));
// принимаю информацию о сервере
char infoAboutServer[1024];
ZeroMemory(infoAboutServer, 1024);
recvfrom(ClientSocket, infoAboutServer, 1024, 0, (SOCKADDR*)&server, &sizeOfserver);
char RecvBuf[1024];
while(1){
ZeroMemory(RecvBuf, BufLen);
// принимаем сообщения
recvfrom(sListen, RecvBuf, 1024, 0, (SOCKADDR*)&SenderAddr, &SenderAddrSize);
// если это сообщение-запрос информации то отправляем в ответ информацию
if (RecvBuf == "test") {
SenderAddr.sin_addr.s_addr = inet_addr(inet_ntoa(SenderAddr.sin_addr));
sendto(sListen, infoMessage.c_str(), infoMessage.size(), 0, (SOCKADDR*)&SenderAddr, SenderAddrSize);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question