This is not really related to module coding, but is more related to general C++, hope that doesn't mean I can't post this here.
So, I am a beginner C++ programmer(not that I am new to programming), and I just made one little program using
WinSock2.h, right here:
PHP Code:
#include <iostream>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
typedef unsigned char BYTE;
#define MAXLEN 512
bool MasterServerResponse(char*);
int main(int argc, char* argv[])
{
int sockMaster;
WSADATA sockData;
sockaddr_in addrOther;
int sLen = sizeof(addrOther);
if(WSAStartup(MAKEWORD(2,2), &sockData) != 0)
{
std::cout << "WSAStartup failed." << std::endl;
return 1;
}
if((sockMaster = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET)
{
std::cout << "socket() failed." << std::endl;
return 1;
}
memset(&addrOther, 0, sizeof(addrOther));
addrOther.sin_family = AF_INET;
addrOther.sin_port = htons(27010);
addrOther.sin_addr.s_addr = inet_addr("hl1master.steampowered.com");
char szBuffer[MAXLEN];
BYTE byteIp[4] = {0, 0, 0, 0};
unsigned short iPort = 0u;
std::cout << "========== Queried servers ==========" << std::endl;
goto startQuery;
while(1)
{
if(byteIp[0] == 0 && byteIp[1] == 0 && byteIp[2] == 0 && byteIp[3] == 0 && iPort == 0)
break;
startQuery:
// Query Master Server
sprintf(szBuffer, "\x31\xFF%d.%d.%d.%d:%d\x00\x00", byteIp[0], byteIp[1], byteIp[2], byteIp[3], iPort);
if(sendto(sockMaster, szBuffer, strlen(szBuffer) + 2/*for 2 null chars*/, 0, (sockaddr*)&addrOther, sizeof(addrOther)) == SOCKET_ERROR)
{
std::cout << "sendto() failed with error code: " << WSAGetLastError() << "." << std::endl;
return 1;
}
memset(szBuffer, '\0', MAXLEN);
if(recvfrom(sockMaster, szBuffer, MAXLEN, 0, (struct sockaddr*)&addrOther, &sLen) == SOCKET_ERROR)
{
std::cout << "recvfrom() failed." << std::endl;
return 1;
}
// Parse the response
if(MasterServerResponse(szBuffer))
{
byteIp[0] = szBuffer[6];
byteIp[1] = szBuffer[7];
byteIp[2] = szBuffer[8];
byteIp[3] = szBuffer[9];
iPort = (szBuffer[10] << 8) | szBuffer[11];
}
std::cout << static_cast<int>(byteIp[0]) << "." << static_cast<int>(byteIp[1]) << "."
<< static_cast<int>(byteIp[2]) << "." << static_cast<int>(byteIp[3]) << ":" << iPort << std::endl;
}
closesocket(sockMaster);
WSACleanup();
return 0;
}
bool MasterServerResponse(char* szResponse)
{
return (szResponse[0] == 0xFF
&& szResponse[1] == 0xFF
&& szResponse[2] == 0xFF
&& szResponse[3] == 0xFF
&& szResponse[4] == 0x66
&& szResponse[5] == 0x0A);
}
If it has any mistakes or whatever, just ignore it for now if it is not related to the problem. So, everything works good until it comes to sendto() function, which always reports an error. I searched for a while now over Google, but can't find the solution(there are solutions out there, but none of them helped me actually).
If you have any idea why is this error happening, please tell me.
If you need anymore information than I gave here, just tell me and I will try to give you as much information as I can.