Valhalla Legends Forums Archive | C/C++ Programming | Winsock Problem

AuthorMessageTime
Spilled[DW]
I am having a problem connecting to the battle.net server. When i pass uswest.battle.net:6112 to my open function no connection is made but if i was to pass 63.241.83.8 then connection is successful.

[code]
int Connection::open(char *addr, short p)
{
    struct sockaddr_in s;
    struct hostent *hent;
   
    if(wSock != -1)
        closesocket(wSock);
       
    if ((s.sin_addr.s_addr = inet_addr(addr)) == -1)
    {
      hent = gethostbyname(addr);
      if (hent == NULL)
        return -1;
     
        s.sin_addr = *((struct in_addr *)hent->h_addr);
    }
    s.sin_port = htons(p);
    s.sin_family = AF_INET;

    wSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
   
    if(connect(wSock, (struct sockaddr *)&s, sizeof(struct sockaddr)))
        MessageBox( NULL, "CONNECTED","",MB_OK);
    else
        MessageBox(NULL, "FAILED TO CONNECT","",MB_OK);
[/code]

Now when i pass uswest.battle.net to this function it exits on this line of code:
[code]
      if (hent == NULL)
        return -1;
[/code]

Any ideas?
July 4, 2006, 9:16 PM
UserLoser
Are you calling WSAStartup anywhere?  And try doing if inet_addr() == INADDR_NONE and not -1
July 5, 2006, 5:39 AM
Spilled[DW]
[quote author=UserLoser link=topic=15336.msg155330#msg155330 date=1152077958]
Are you calling WSAStartup anywhere?  And try doing if inet_addr() == INADDR_NONE and not -1
[/quote]

Tried what you said:
[code]
  int Connection::open(char *addr, short p)
{
    WSADATA W;
    WSAStartup(MAKEWORD(2,0), &W);
    struct sockaddr_in s;
    struct hostent *hent;
   
    if(wSock != -1)
        closesocket(wSock);
       
    if ((s.sin_addr.s_addr = inet_addr(addr)) == INADDR_NONE )
    {
      hent = gethostbyname(addr);
      if(hent == NULL) return -1;
     
        s.sin_addr.s_addr = inet_addr(hent->h_addr);
    }
    s.sin_port = htons(p);
    s.sin_family = AF_INET;

    wSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
   
    if(connect(wSock, (struct sockaddr *)&s, sizeof(struct sockaddr)))
        MessageBox( NULL, "CONNECTED","",MB_OK);
    else
        MessageBox(NULL, "FAILED TO CONNECT","",MB_OK);

}[/code]

Now after making these changes, the socket will connect when i use uswest.battle.net and not 63.241.83.8

Any ideas guys? this was a problem with my last socket as well.
July 5, 2006, 6:14 AM
Win32
I don't recall DNS lookup's supporting the hostname:port notation. Also, gethostbyname() is out of date, use getaddrinfo().


-Matt
August 30, 2006, 6:47 PM
Maddox
Connect returns 0 on success...
September 1, 2006, 12:13 AM

Search