gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Odd problems connecting to a web host from the DS.

#147847 - chatterbug89 - Sat Dec 29, 2007 1:22 am

EDIT2: After a few hours, it seems to be working again how it use to work on the DS. Though, this behavior still seems weird considering it worked on multiple computers just fine (all of my network).

I am working on a project for the Nintendo DS that communicates with a PHP script on my host. Things were working perfectly for the most part but now suddenly my program cannot communicate with my host at all.

First of all, before I continue, I should note I'm using PA_Lib and it's PA_GetHTTP function.

Now, here's what's really weird. Placing the script on my personal server and having the DS execute it works great, but it just hangs when executing the exact same script on my server (that use to work yesterday). Running the PHP script from my browser works perfectly on the other hand.

Another odd thing that I have had to deal with is PA_GetHTTP seems to return some "junk" about the length of the incomming data when receiving data from the host in question (which is Total Choice Hosting). However, this extra data doesn't seem to be present when getting pages from my personal server and this extra data never shows up when using a web browser.

I'm just completely stumped as to why my host would be giving me such weird problems. I may just end up finding somewhere else to host my PHP app that communicates with my project (my personal server's connection is a tad slow and unreliable...).

Here is the PA_Lib function in question....the problem most likly lies in it.

Code:

int PA_GetHTTP(char *buffer, char *adress)
{
    int sock;
    char serveur[256];
    char buffer3[256];
    int pos = get_HTTP_serveur(serveur,adress);
    PA_InitSocket(&sock,serveur,80,PA_NONBLOCKING_TCP);
    char buffer2[256];
    sprintf(buffer2, "GET %s HTTP/1.1\r\nhost: %s\r\nAccept: */*\r\n\r\n",adress+pos,serveur);
    send(sock,buffer2,256,0);
    strcpy(buffer,"");
    while(search_word(buffer3,"\r\n\r\n",0) == -1)
    {
       memset(buffer3,0,sizeof(buffer3));
       recv(sock,buffer3,256,0);
    }
    int poshtml = search_word(buffer3,"\r\n\r\n",0)+4;
    strcat(buffer,buffer3+poshtml);
    while(search_word(buffer,"</html>",0) == -1 && search_word(buffer,"</HTML>",0) == -1)
    {
    memset(buffer3,0,sizeof(buffer3));
    if(recv(sock,buffer3,256,0)<1)
       break;
    strcat(buffer,buffer3);
    }
    if(sock) closesocket(sock);
       //closesocket(sock);
   return 1;
}


Code:

int get_HTTP_serveur(char *buffer, char *buffer2)
{
    int i,depart=0;
    if(search_word(buffer2,"http://",0)!=-1)
       depart+=7;
    for(i = depart; buffer2[i] != '\0' && buffer2[i] != '/' && buffer2[i] != '\\'; i++)
    {
          buffer[i-depart]=buffer2[i];
    }
    buffer[i-depart]='\0';
return i;
}


Code:

int search_word(char *mot1, char *mot2, int depart){
   int i,j,erreur=1;
   for(i=depart;i<strlen(mot1);i++){
   
      if(mot1[i]==mot2[0]){
         erreur=0;
         
         for(j=0;j<strlen(mot2);j++)
            if(mot2[j]!=mot1[i+j])
               erreur=1;
               
         if(erreur==0)
            return i;
      }
   }
   return -1;
}


Code:

int PA_InitSocket(int *sock,char *host,int port,int mode)
{
     unsigned long ip;
     struct     sockaddr_in    servaddr;   
     *sock = socket(AF_INET, SOCK_STREAM, 0);
     if(IS_INETADDR(host))
       ip = PA_chartoip(host);
     else
       ip = *(unsigned long *)gethostbyname(host)->h_addr_list[0];
       
     servaddr.sin_family = AF_INET;
     servaddr.sin_port = htons(port);     
     servaddr.sin_addr.s_addr = ip;
     
     if(mode == PA_NORMAL_TCP)
     {
     if(connect(*sock, (struct sockaddr *) &servaddr, sizeof(servaddr))==0)
        return 1;
     }
     else if(mode == PA_NONBLOCKING_TCP)
     {
     if(connect(*sock, (struct sockaddr *) &servaddr, sizeof(servaddr))==0)
        {
        int i = 1;
        ioctl(*sock, FIONBIO, &i);
        return 1;
        }
     }
     
       
return 0;
}


Any suggestions, tips, or help would be appreciated. The site in question where the file is hosted is at cybernetresources.com.

EDIT: I have done numerous tests by just calling PA_GetHTTP and the bare-bones of what needs to be called to get things up and running. So...the problem shouldn't lie in my code at all. PA_GetHTTP simply hangs when it tries to get pages from my host.

#147875 - tepples - Sat Dec 29, 2007 5:41 pm

Your description of the response as "some 'junk' about the length of the incomming data" makes me think it has something to do with HTTP/1.1 chunked encoding, which HTTP servers use when they don't know how big the Content-Length will be. Does PA_GetHTTP support this?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#147879 - chatterbug89 - Sat Dec 29, 2007 6:09 pm

I used http://web-sniffer.net/ to check out what was being sent in the header of my site (and specifically, with the script I'm talking with), and it does seem to be chuked.

All PA_Gethttp seems to be doing is reading from the response untill it no longer finds \r\n\r\n in the input, which it must assume is the end of the header and the beginning of the content. So, after that, there is the chunk information, something like 5 Content bla bla bla 0. This isen't a huge deal, since all I have to do is clear out the first number(s) and spaces, and get rid of the end (which I have already done and it works how it should for me). I have no clue why it's looking for </html> (not having any of those tags in the response doesn't mess up anything from what I can see...adding them does mess up things though).

This doesn't explain the weird "dead periods" my server is giving me with just my DS every so often. I suppose it could have been a passing thing or my router was just acting up (though, I also tried it in an emulator supporting wifi and it didn't work during those times either...).

Anyways, I think we can safely conclude that PA_GetHTTP doesn't properly support chunking.

#147886 - thegamefreak0134 - Sat Dec 29, 2007 9:59 pm

What you might try is doing an HTTP 1.0 request, instead of a 1.1 request. Chunking support was added with the 1.1 standard, and I've found that doing an HTTP 1.0 request will cause the web server not to send data this way. However, depending on how PAlib is set up, you may have to use it's lower functions to send a request out this way. (Or you could just mod your copy of the function.) This will also cause the server to not default to sending compressed data, which has given me issues in the past.

-gamefreak
_________________
What if the hokey-pokey really is what it's all about?

[url=http:/www.darknovagames.com/index.php?action=recruit&clanid=1]Support Zeta on DarkNova![/url]