#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;
}
}
Davpk escribió:Buenas, no he trabajado mucho con sockets, pero creo que el problema está en que en el código de DS intentas conectarte a la ip local de la consola y por ese motivo no obtienes respuesta del servidor en el PC. Creo que para que te funcione lo que tendrias que hacer es cambiar la ip "127.0.0.1" por la ip de red de tu pc (normalmente 192.168.0.x o 192.168.1.x, donde x puede ser cualquier numero de 1 a 255).
Un Saludo
keda escribió:Lo de poner 127.0.0.1 era porque estaba puesta mi IP real y no quería ponerla xD
De todas formas, probé también con la IP de la red local (192.168.X.X) y nada, así que ahí se ha quedado la cosa. Estoy probando los sockets en php, y aunque tampoco consigo mucho, consigo más que con los de C xD
Gracias por las respuestas ^^
keda escribió:Los puertos estaban abiertos y mapeados (creo q se dice así) en el router, así que por puertos no creo que fuese :S