Raised This Month: $51 Target: $400
 12% 

WinSock2 sendto() error #10013


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 08-02-2014 , 20:05   WinSock2 sendto() error #10013
Reply With Quote #1

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 argccharargv[])
{
    
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_INETSOCK_DGRAMIPPROTO_UDP)) == INVALID_SOCKET)
    {
        
std::cout << "socket() failed." << std::endl;
        return 
1;
    }

    
memset(&addrOther0sizeof(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] = {0000};
    
unsigned short iPort 0u;

    
std::cout << "========== Queried servers ==========" << std::endl;
    goto 
startQuery;
    while(
1)
    {
        if(
byteIp[0] == && byteIp[1] == && byteIp[2] == && byteIp[3] == && 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(sockMasterszBufferstrlen(szBuffer) + 2/*for 2 null chars*/0, (sockaddr*)&addrOthersizeof(addrOther)) == SOCKET_ERROR)
        {
            
std::cout << "sendto() failed with error code: " << WSAGetLastError() << "." << std::endl;
            return 
1;
        }

        
memset(szBuffer'\0'MAXLEN);

        if(
recvfrom(sockMasterszBufferMAXLEN0, (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(charszResponse)
{
    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.

Last edited by klippy; 08-02-2014 at 20:06.
klippy is offline
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 08-02-2014 , 21:59   Re: WinSock2 sendto() error #10013
Reply With Quote #2

PHP Code:
    ULONG addrHnd inet_addr("hl1master.steampowered.com");

    if (
addrHnd == INADDR_NONE)
        
MessageBox(NULL"Inet_Addr failed!""Inet_Addr failed!"MB_OK MB_ICONERROR); 
What do you think?

PHP Code:
PHOSTENT hostHnd gethostbyname("hl1master.steampowered.com");
// hostHnd->h_addr, hostHnd->h_length 
__________________

Last edited by claudiuhks; 08-02-2014 at 22:03.
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 08-03-2014 , 08:43   Re: WinSock2 sendto() error #10013
Reply With Quote #3

Quote:
Originally Posted by claudiuhks View Post
PHP Code:
    ULONG addrHnd inet_addr("hl1master.steampowered.com");

    if (
addrHnd == INADDR_NONE)
        
MessageBox(NULL"Inet_Addr failed!""Inet_Addr failed!"MB_OK MB_ICONERROR); 
What do you think?

PHP Code:
PHOSTENT hostHnd gethostbyname("hl1master.steampowered.com");
// hostHnd->h_addr, hostHnd->h_length 
Thank you for your reply!
Here is what I've done to check if it really works:
PHP Code:
if((addrOther.sin_addr.s_addr inet_addr("hl1master.steampowered.com")) == INADDR_NONE)
        
MessageBox(NULLL"inet_addr() failed!"L"inet_addr() failed!"MB_OK MB_ICONERROR); 
And it MessageBox'd me every time. I also pinged the masterserver from the command prompt, found out that VALVe's server is actually offline. So I found few working ones, and it works then, but I guess these ones weren't masterservers actually, but something other, because they gave me a random response buffer.

Quote:
Originally Posted by claudiuhks
PHP Code:
PHOSTENT hostHnd gethostbyname("hl1master.steampowered.com");
// hostHnd->h_addr, hostHnd->h_length 
What did you want me to do with this one? Output hostHnd->h_addr and hosHnd->h_length? If so, here's the output(keep in mind that this is VALVe's non-working masterserver):


I output it with this code:
PHP Code:
PHOSTENT hostHnd gethostbyname("hl1master.steampowered.com");
std::cout << "Len: " << hostHnd->h_length << " Address: " << hostHnd->h_addr << std::endl << std::endl
And what is gethostbyname() actually doing? Getting real IP address from a symbolic one(name)?
klippy is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 08-03-2014 , 15:02   Re: WinSock2 sendto() error #10013
Reply With Quote #4

Nevermind, I got it to work. So if anyone is interested, here's the code:
PHP Code:
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
#include <WinSock2.h>

#pragma comment(lib, "ws2_32.lib")

typedef unsigned char BYTE;

#define BUFLEN    5120


unsigned short BytesToShort(BYTE byte1BYTE byte2)
{
    static 
union
    
{
        
BYTE bytes[2];
        
unsigned short ushort;
    };

    
bytes[1] = byte1;
    
bytes[0] = byte2;

    return 
ushort;
}

/* Stream reading functions */
BYTE ReadByte(std::istringstreamstream)
{
    
char szBuffer[1];
    
stream.read(szBuffer1);
    return 
szBuffer[0];
}

unsigned short ReadShort(std::istringstreamstream)
{
    
char szBuffer[2];
    
stream.read(szBuffer2);
    return 
BytesToShort(szBuffer[0], szBuffer[1]);
}


int main(int argccharargv[])
{
    
int sockMaster;
    
WSADATA sockData;
    
sockaddr_in addrOther;
    
int sLen sizeof(addrOther);

    if(
WSAStartup(MAKEWORD(2,2), &sockData) != 0)
    {
        
std::cout << "WSAStartup() failed." << std::endl;
        
WSACleanup();

        return 
1;
    }

    if((
sockMaster socket(AF_INETSOCK_DGRAMIPPROTO_UDP)) == INVALID_SOCKET)
    {
        
std::cout << "socket() failed." << std::endl;
        
WSACleanup();

        return 
1;
    }
    
DWORD timeoutVal 1000// 1000 miliseconds / one second
    
setsockopt(sockMasterSOL_SOCKETSO_RCVTIMEO, (const char*)&timeoutValsizeof(timeoutVal));

    
char szBuffer[BUFLEN];
    
std::string sInputsIpsPort;

    while(
1)
    {
        
std::cout << "Input IP:PORT of the MasterServer, or 0 to exit the program: ";
        
std::getline(std::cinsInput);
        if(
sInput.c_str()[0] == '0')
            break;

        
std::size_t portPos;
        if((
portPos sInput.find(':')) == std::string::npos)
        {
            
std::cout << "You did not input the port number." << std::endl << std::endl;
            continue;
        }
        
sIp sInput.substr(0portPos);
        
sPort sInput.substr(portPos 1std::string::npos);

        
memset(&addrOther0sizeof(addrOther));
        
addrOther.sin_family AF_INET;
        
addrOther.sin_port htons((unsigned short)atoi(sPort.c_str()));
        if((
addrOther.sin_addr.s_addr inet_addr(sIp.c_str())) == INADDR_NONE)
        {
            
std::cout << "The entered IP address is invalid." << std::endl << std::endl;
            continue;
        }
        
        
sprintf(szBuffer"\x31\xFF\x30.0.0.0:0\x00\x00");
        if(
sendto(sockMasterszBuffer130, (sockaddr*)&addrOthersizeof(addrOther)) == SOCKET_ERROR)
        {
            
std::cout << "sendto() failed with error code #" << WSAGetLastError() << "." << std::endl << std::endl;
            continue;
        }

        
memset(szBuffer'\0'BUFLEN);
        if(
recvfrom(sockMasterszBufferBUFLEN0, (struct sockaddr*)&addrOther, &sLen) == SOCKET_ERROR)
        {
            if(
WSAGetLastError() == 10060)
                
std::cout << "Error #10060: Request has timed out." << std::endl << std::endl;
            else
                
std::cout << "recvfrom() failed with error code #" << WSAGetLastError() << "." << std::endl << std::endl;

            continue;
        }

        
// Parse the response
        
char szServer[22];
        
BYTE byteIp[4] = {0000};
        
unsigned short iPort 0u;

        
std::vector<std::stringserversArray;
        
std::istringstream strBuffer(szBuffer);
        
strBuffer.seekg(6); // Skip the first 6 bytes (xFF xFF xFF xFF x66 x0A)

        
do
        {
            
byteIp[0] = ReadByte(strBuffer);
            
byteIp[1] = ReadByte(strBuffer);
            
byteIp[2] = ReadByte(strBuffer);
            
byteIp[3] = ReadByte(strBuffer);
            
iPort ReadShort(strBuffer);
            
            
sprintf(szServer"%d.%d.%d.%d:%u", (int)byteIp[0], (int)byteIp[1], (int)byteIp[2], (int)byteIp[3], iPort);
            
serversArray.push_back(std::string(szServer));
        }
        while((int)
strBuffer.tellg() != -1);
        
serversArray.pop_back(); // Remove the last one

        
std::cout << "========== Queried servers ==========" << std::endl;
        
std::vector<std::string>::iterator iter;
        for(
std::vector<std::string>::iterator iter serversArray.begin(); iter != serversArray.end(); ++iter)
            
std::cout << iter->c_str() << std::endl;

        
std::cout << "Queried " << serversArray.size() << " servers in total." << std::endl << std::endl;
    }


    
closesocket(sockMaster);
    
WSACleanup();

    return 
0;

Its purpose is to query the master server. I know it may be badly written or ugly, but I don't really care, it's mostly for testing purposes.

Last edited by klippy; 08-03-2014 at 15:22.
klippy is offline
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 08-03-2014 , 15:17   Re: WinSock2 sendto() error #10013
Reply With Quote #5

Thank you for sharing.
gethostbyname retrieves information from a name record.
Especially the IP address(es).

What is the Master Server IP address and its port?
__________________

Last edited by claudiuhks; 08-03-2014 at 15:36.
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
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 04:59.


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