Problem connecting DS to a C++ Server

Post Reply
eglomer
Posts: 4
Joined: Thu Sep 24, 2009 3:48 pm

Problem connecting DS to a C++ Server

Post by eglomer » Thu Sep 24, 2009 3:56 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: Select all

    #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!

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Problem connecting DS to a C++ Server

Post by elhobbs » Fri Sep 25, 2009 5:12 pm

I am not sure that the loopback address is implemented on the ds. can you try to connect to a different computer?

dheart88
Posts: 44
Joined: Sun Dec 13, 2009 3:00 pm

Re: Problem connecting DS to a C++ Server

Post by dheart88 » Tue Jan 26, 2010 11:19 pm

Can you pls upload the code? the link isn't working anymore.. I need some example code.

User avatar
Izhido
Posts: 107
Joined: Fri Oct 19, 2007 10:57 pm
Location: Costa Rica
Contact:

Re: Problem connecting DS to a C++ Server

Post by Izhido » Wed Jan 27, 2010 3:29 pm

A more appropiate question might be: why 127.0.0.1 ? Do you realize that attempting to open a socket to that address is basically attempting to connect with *yourself* (the DS, in this case) ?

As such, the code you posted won't connect, ever, since you're opening a connection to yourself at port 40000 when you didn't actually created a host (a "server"?) in your DS, listening to port 40000...

So, as elhobbs correctly pointed out, you would do better finding out what's the real address of your "C++ server", then using it on your program.

Let us know how did that work, ok? :)

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests