• Welcome to Valhalla Legends Archive.
 

Winsock Problem

Started by Spilled, July 04, 2006, 04:16 PM

Previous topic - Next topic

Spilled

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.


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);


Now when i pass uswest.battle.net to this function it exits on this line of code:

      if (hent == NULL)
         return -1;


Any ideas?

UserLoser

Are you calling WSAStartup anywhere?  And try doing if inet_addr() == INADDR_NONE and not -1

Spilled

#2
Quote from: UserLoser on July 05, 2006, 12:39 AM
Are you calling WSAStartup anywhere?  And try doing if inet_addr() == INADDR_NONE and not -1

Tried what you said:

  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);

}


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.

Win32

I don't recall DNS lookup's supporting the hostname:port notation. Also, gethostbyname() is out of date, use getaddrinfo().


-Matt

Maddox

Connect returns 0 on success...
asdf.