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 > Problem connecting DS to a C++ Server

#170434 - eglomer - Thu Sep 24, 2009 4:02 pm

Hi!

I'm trying to connect my DS to a C++ Server running under linux (ubuntu 9.04), but I have problems with the Connect function (it don't connect).

I try with a linux c++ client, and i have no problems, but with DS is imposible... T_T

DS main:
Code:
  #include <nds.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fat.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <dswifi9.h>

    #define PORT 40000 // puerto al que vamos a conectar
    #define MAXDATASIZE 256
    #define NORMAL_TCP 0
    #define NONBLOCKING_TCP 1


    int EGL_ConnectIP(int *sock, const char *IP, int port, int mode);
    int EGL_InitWifi(int debug);


    int main (int argc, char *argv[]){
       // Variables
       int numbytes;
       char buf[MAXDATASIZE]="hello from DS";

       // Inits
       consoleDemoInit();
       while (EGL_InitWifi(1) == 0);

       int sock;
       if ( EGL_ConnectIP(&sock, "127.0.0.1", PORT, NONBLOCKING_TCP) ){
       printf ("Conectado\n");
       }else{
       return 0;
       }     

       // Send
       printf ("Enviando: ");
       send( sock, buf, strlen(buf), 0 );
       printf ("enviado...\n");

       // Receive
       printf ("Recibiendo: ");
       while ((numbytes=recv(sock, buf, MAXDATASIZE-1, 0)) != 0) {
          if (numbytes > 0){
             buf[numbytes] = '\0';
          }   
       }
       printf("recibido...\n\n%s\n\n",buf);

       printf ("Cerrando socket: ");
       shutdown(sock,0);
       closesocket(sock);
       printf ("cerrado...");

       return 0;
    }   


    //--------------------------------------------------------------------------------------------------------------------------------------------

    int EGL_ConnectIP(int *sock, const char *IP, int port, int mode){

    //--------------------------------------------------------------------------------------------------------------------------------------------
       int i = 0;
       struct sockaddr_in client;
         

       *sock = socket(AF_INET,SOCK_STREAM,0);
       if(*sock == -1 ){
          printf("Error al crear socket\n");
          return 0;
       }else printf("Socket creado\n");

       client.sin_addr.s_addr = inet_addr(IP);
       client.sin_family = AF_INET;
       client.sin_port = htons(port);

       if ( mode == NORMAL_TCP ){
          i = 0;
          printf ("Ajustado a NORMAL TCP\n");
       }else if ( mode == NONBLOCKING_TCP ){
          i = 1;
          printf ("Ajustado a NONBLOCKING TCP\n");
       }   
       ioctl(*sock, FIONBIO, &i); // set blocking or non-blocking

       if ( connect(*sock, (struct sockaddr *) &client, sizeof(client)) == 0 ) return 1;
       printf ("Imposible conectar al servidor\n");

       return 0;
    }



    //--------------------------------------------------------------------------------------------------------------------------------------------

    int EGL_InitWifi(int debug){

    //--------------------------------------------------------------------------------------------------------------------------------------------
       if(!Wifi_InitDefault(WFC_CONNECT)) {
          return 0;
       } else {
          if (debug == true){
             struct in_addr ip, gateway, mask, dns1, dns2;
             ip = Wifi_GetIPInfo(&gateway, &mask, &dns1, &dns2);
             iprintf("ip     : %s\n", inet_ntoa(ip) );
             iprintf("gateway: %s\n", inet_ntoa(gateway) );
             iprintf("mask   : %s\n", inet_ntoa(mask) );
             iprintf("dns1   : %s\n", inet_ntoa(dns1) );
             iprintf("dns2   : %s\n", inet_ntoa(dns2) );   
          }         
          return 1;   
       }
    }   


Client and Server source code for PC (linux with CodeBlocks): Download

Thank you!

#170445 - hacker013 - Sat Sep 26, 2009 1:01 pm

Code:
while(EGL_WifiInit(1) == 0)
must be something like this
Code:
if(EGL_WifiInit(1)){printf("Wifi Succeed");}else{printf("Wifi failed");


because you can only init wifi one time >.>
_________________
Website / Blog

Let the nds be with you.

#170447 - elhobbs - Sat Sep 26, 2009 3:13 pm

I do not think the loopback address is implemented in dswifi. can you connect to a separate computer?