Raised This Month: $32 Target: $400
 8% 

Server Queries from Localhost


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Shooting King
RAAASENGAN
Join Date: Mar 2012
Location: India
Old 07-23-2014 , 10:53   Server Queries from Localhost
Reply With Quote #1

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 charszBuffer)
{
    
int ijiPrinted 0;

    for( 
0leni++ )
    {
        
printf"%02X "szBuffer[i] );
        
iPrinted++;

        if( 
iPrinted == 16 || == len-1)
        {
            for( 
0< ((16-iPrinted)+2)*3j++ ) printf" " );

            for( 
i-iPrinted+1<= ij++ )
            {
                
printf"%c", (isprint(szBuffer[j])?szBuffer[j]:'.') );
            }
            
printf"\n" );
            
iPrinted 0;
        }
    }
    
printf"\n" );
}

int __cdecl main(int argcchar **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( &hintssizeof(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 != ) {
        
printf("getaddrinfo failed with error: %d\n"iResult);
        
WSACleanup();
        return 
1;
    }
    
    
// Attempt to connect to an address until one succeeds
    
for(ptr=resultptr != NULL ;ptr=ptr->ai_next) {

        
// Create a SOCKET for connecting to server
        
ConnectSocket socket(ptr->ai_familyptr->ai_socktype
            
ptr->ai_protocol);
        if (
ConnectSocket == INVALID_SOCKET) {
            
printf("socket failed with error: %ld\n"WSAGetLastError());
            
WSACleanup();
            return 
1;
        }
        
        
int ReceiveTimeout 6000;
        
setsockopt(ConnectSocketSOL_SOCKETSO_RCVTIMEO, (char*)&ReceiveTimeoutsizeof(int));
        
// Connect to server.
        
        
iResult connectConnectSocketptr->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 sendConnectSocketsendbuf, (int)strlen(sendbuf), );
    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(ConnectSocketSD_SEND);
    if (
iResult == SOCKET_ERROR) {
        
printf("shutdown failed with error: %d\n"WSAGetLastError());
        
closesocket(ConnectSocket);
        
WSACleanup();
        return 
1;
    }
    
int total 0;
    do{
        
iResult recv(ConnectSocketrecvbufrecvbuflen0);
        if ( 
iResult )
        {
            
dump_bufferrecvbuflen, (unsigned char*)recvbuf );
            
printf("Bytes received: %d\n"iResult);
        }
        else if ( 
iResult == )
            
printf("Connection closed\n");
        else
            
printf("recv failed with error: %d\n"WSAGetLastError());
    } while( 
iResult );
    
    
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;

__________________
As every time said, don't ever UNDERESTIMATE me.

Donate - Here
Shooting King is online now
Neeeeeeeeeel.-
Some Guy Yellin'
Join Date: Jul 2010
Location: Argentina
Old 07-23-2014 , 12:03   Re: Server Queries from Localhost
Reply With Quote #2

Did you tried with 127.0.0.1? If it doesn't work, try with your public address.
__________________
Neeeeeeeeeel.- is offline
Send a message via Skype™ to Neeeeeeeeeel.-
Shooting King
RAAASENGAN
Join Date: Mar 2012
Location: India
Old 07-23-2014 , 12:36   Re: Server Queries from Localhost
Reply With Quote #3

Quote:
Originally Posted by Neeeeeeeeeel.- View Post
Did you tried with 127.0.0.1? If it doesn't work, try with your public address.
Already tied 127.0.0.1, localhost, 192.168.x.x and Global IP. fyi, i am behind a NAT.
__________________
As every time said, don't ever UNDERESTIMATE me.

Donate - Here
Shooting King is online now
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 07-24-2014 , 03:42   Re: Server Queries from Localhost
Reply With Quote #4

Did you check AMX Mod X Sockets module's source?
__________________
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
Shooting King
RAAASENGAN
Join Date: Mar 2012
Location: India
Old 07-24-2014 , 09:52   Re: Server Queries from Localhost
Reply With Quote #5

Quote:
Originally Posted by claudiuhks View Post
Did you check AMX Mod X Sockets module's source?
Yes, seen, but i did't get any good out of it. Can tell me where is my mistake ?
__________________
As every time said, don't ever UNDERESTIMATE me.

Donate - Here
Shooting King is online now
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 07-24-2014 , 13:21   Re: Server Queries from Localhost
Reply With Quote #6

Quote:
Originally Posted by Shooting King View Post
Yes, seen, but i did't get any good out of it. Can tell me where is my mistake ?
I can't right now because I've got no studies regarding C/ C++ but what I can say is that I recommend IPPROTO_UDP to be changed to IPPROTO_IP, AF_UNSPEC to AF_INET and regarding WSAStartup function ain't MAKEWORD(1u, 1u) version enough?

Also are you sure you want to use DGRAM Socket type? What about STREAM?

That's the way I use Sockets.

PHP Code:
#pragma comment (lib, "ws2_32.lib") // link ws2_32.lib to Program,
    // this is designed for Internet connection in Program

// include winsock2.h file, this is designed for Windows and Internet applications
#include <WinSock2.h>

// when program starts
VOID main(SIZE_TPCHAR argsList[])
{
    
// startup Internet in program
    
WSADATA dataHndl;

    
WSAStartup(MAKEWORD(1u1u), &dataHndl);

    
// create new socket for DGRAM and IP protocol
    
SOCKET sockHndl socket(AF_INETSOCK_DGRAMIPPROTO_IP);

    
// retrieve IPAddress of target
    
SOCKADDR_IN srvHndl;

    
ULONG addrHndl inet_addr(argsList[1u]);

    
// copy IPAddress to srvHndl
    
memcpy(&srvHndl.sin_addr, &addrHndlsizeof addrHndl);

    
// set family as AF_INET and port as the numerical port htons'ed
    
srvHndl.sin_family AF_INET;
    
srvHndl.sin_port htons(atoi(argsList[2u]));

    
// connect to target
    
connect(sockHndl, (PSOCKADDR)&srvHndlsizeof srvHndl);

// .......

__________________

Last edited by claudiuhks; 07-24-2014 at 13:26.
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
Shooting King
RAAASENGAN
Join Date: Mar 2012
Location: India
Old 07-24-2014 , 13:35   Re: Server Queries from Localhost
Reply With Quote #7

Quote:
Originally Posted by claudiuhks View Post
I can't right now because I've got no studies regarding C/ C++ but what I can say is that I recommend IPPROTO_UDP to be changed to IPPROTO_IP, AF_UNSPEC to AF_INET and regarding WSAStartup function ain't MAKEWORD(1u, 1u) version enough?

Also are you sure you want to use DGRAM Socket type? What about STREAM?
See This.
There is no IPPROTO_IP. There is a choice for transport layer only.
I think AF_UNSPEC can be used for all types of interfaces ?
Hl Queries use UDP, UDP can only be used with SOCK_DGRAM ?
Latest version is 2.2 according to MSDN : |
__________________
As every time said, don't ever UNDERESTIMATE me.

Donate - Here
Shooting King is online now
Shooting King
RAAASENGAN
Join Date: Mar 2012
Location: India
Old 08-09-2014 , 13:38   Re: Server Queries from Localhost
Reply With Quote #8

Some one has to know
__________________
As every time said, don't ever UNDERESTIMATE me.

Donate - Here
Shooting King is online now
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 23:02.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode