Jump to content

Problems to send a buffer over a socket


STRPG

Recommended Posts

I am trying to develop a homebrew for nds that will need client-server packet transfers. I started using Palib and/or Nflib and I wasn't able to transfer info so

(After many tries) I decided to modify the httpget example to find a way to transfer just a few bytes, the problem is that the following code works:

 

#include <nds.h>
#include <dswifi9.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>

void tosrv(char* url);

int main(void) {

consoleDemoInit();

if(!Wifi_InitDefault(WFC_CONNECT)) {
	iprintf("Failed to connect!");
} else {
	iprintf("Connected\n\n");
	tosrv("xmultivers.servegame.com");	}

while(1) {
	swiWaitForVBlank();
}

return 0;
}

void tosrv(char* url) {

const char * request_text = 
	"			  AB\n	   "
	"							  "
	"									  ";

struct hostent * myhost = gethostbyname( url );

int my_socket;
my_socket = socket( AF_INET, SOCK_STREAM, 0 );

struct sockaddr_in sain;
sain.sin_family = AF_INET;
sain.sin_port = htons(1078);
sain.sin_addr.s_addr= *( (unsigned long *)(myhost->h_addr_list[0]) );
connect( my_socket,(struct sockaddr *)&sain, sizeof(sain) );

 send( my_socket, request_text, strlen(request_text), 0 );
 iprintf( "Sent\n" );
 shutdown(my_socket,0); 
 closesocket(my_socket);
 iprintf ( "Finished\n" );
}

 

 

But this other one doesn't work.

 

#include <nds.h>
#include <dswifi9.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>

void tosrv(char* url);

int main(void) {

consoleDemoInit();

if(!Wifi_InitDefault(WFC_CONNECT)) {
	iprintf("Failed to connect!");
} else {
	iprintf("Connected\n\n");
	tosrv("xmultivers.servegame.com");	}

while(1) {
	swiWaitForVBlank();
}

return 0;
}

void tosrv(char* url) {

const char * request_text = "AB";
struct hostent * myhost = gethostbyname( url );

int my_socket;
my_socket = socket( AF_INET, SOCK_STREAM, 0 );

struct sockaddr_in sain;
sain.sin_family = AF_INET;
sain.sin_port = htons(1078);
sain.sin_addr.s_addr= *( (unsigned long *)(myhost->h_addr_list[0]) );
connect( my_socket,(struct sockaddr *)&sain, sizeof(sain) );

 send( my_socket, request_text, strlen(request_text), 0 );
 iprintf( "Sent\n" );
 shutdown(my_socket,0); 
 closesocket(my_socket);
 iprintf ( "Finished\n" );
}

 

The server is writen in perl and works like a charm, the first code sends the packet (using too many bytes), the second code just opens the socket (the server registers a client connecting but doesn't get the packet). Gethostbyname resolves to a local ip address (by resolving a no-ip dns), I searched info about the minimal bytes that can be send with send() but didn't find any info.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...