I am using this winsock2 code for getting Server A2S_Info Query. But when i try to get Queries from my own server (at localhost) it gives me a timeout :C
PHP Code:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define DEFAULT_BUFLEN 256
void dump_buffer(int len, const unsigned char* szBuffer)
{
int i, j, iPrinted = 0;
for( i = 0; i < len; i++ )
{
printf( "%02X ", szBuffer[i] );
iPrinted++;
if( iPrinted == 16 || i == len-1)
{
for( j = 0; j < ((16-iPrinted)+2)*3; j++ ) printf( " " );
for( j = i-iPrinted+1; j <= i; j++ )
{
printf( "%c", (isprint(szBuffer[j])?szBuffer[j]:'.') );
}
printf( "\n" );
iPrinted = 0;
}
}
printf( "\n" );
}
int __cdecl main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
char *sendbuf = "\xff\xff\xff\xffTSource Engine Query\x00";
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
// Resolve the server address and port
iResult = getaddrinfo("localhost", "27032", &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
int ReceiveTimeout = 6000;
setsockopt(ConnectSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&ReceiveTimeout, sizeof(int));
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
// Send an initial buffer
iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
printf("Bytes Sent: %ld\n", iResult);
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
int total = 0;
do{
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
{
dump_buffer( recvbuflen, (unsigned char*)recvbuf );
printf("Bytes received: %d\n", iResult);
}
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError());
} while( iResult > 0 );
printf( "Exiting ... \n" );
/*cout << endl << endl;
cout << "Size of Short : " << sizeof (short) << endl;
cout << "Size of int : " << sizeof (int) << endl;
cout << "Size of long : " << sizeof (long) << endl;
cout << "Size of longlong : " << sizeof (long long) << endl;
cout << "Size of char : " << sizeof (char) << endl;
cout << "Size of float : " << sizeof (float) << endl;
cout << std::hex << recvbuffer << endl;
for( int i = 0; i <= total; i++ )
{
//if( recvbuffer[i] != '\xfffffffe' )
// cout << std::hex << std::setw(2) << (int)recvbuffer[i] << std::setw(7) << recvbuffer[i] << " ";
//else
cout << std::setw(9) << recvbuffer[i] << " ";
if( i % 7 == 0 ) cout << endl;
}
printf( "Header - %x\n", recvbuffer[0] );
printf( "Address - %s\n", &recvbuffer[1] );
for( int i = 1; i <= (int)strlen( &recvbuffer[1] ); i++ )
{
cout << std::hex << (int)recvbuffer[i-1] << " ";
if( i % 10 == 0 ) cout << endl;
}
int temp = strlen(&recvbuffer[1])+1;
printf( "Name - %s\n", &recvbuffer[temp] );
temp += strlen(&recvbuffer[temp])+1;
printf( "Map - %s\n", &recvbuffer[temp] );
temp += strlen(&recvbuffer[temp])+1;
printf( "Folder - %s\n", &recvbuffer[temp] );
temp += strlen(&recvbuffer[temp])+1;
printf( "Game - %s\n", &recvbuffer[temp] );
temp++;
printf( "Players - %c\n", 48+recvbuffer[temp] );
temp++;
printf( "MaxPlayers - %c\n", 48+recvbuffer[temp] );
temp++;
printf( "Protocol - %c\n", 48+recvbuffer[temp] );
temp++;
printf( "Server Type - %c\n", recvbuffer[temp] );
temp++;
printf( "Envirnoment - %c\n", recvbuffer[temp] );
temp++;
printf( "Visibility - %c\n", recvbuffer[temp] );
temp++;
printf( "Mod - %c\n", 48+recvbuffer[temp] );*/
closesocket(ConnectSocket);
WSACleanup();
system( "pause" );
return 0;
}