Welcome! Log In Create A New Profile


I really need help!!!

Posted by pokeglobe 
I really need help!!!
September 26, 2009 02:04AM
I have been working on a LibwiiGui App for a LONG time. At least 8 hours a day non stop.
I am so close to getting this. I am just having an issue with downloading files.

I cannot download ANYTHING but HTML files. It is getting really annoying.

This is the code (WorldWiideWeb Edited a bit)

static void *xfb = NULL;
static GXRModeObj *rmode = NULL;
static volatile u8 _reset = 0;
const char*defaulturl = "Websitehere.com";

  This struct stores information about the HTTP response. For more information,
  see RFC 1945 [www.w3.org]
struct httpresponse{
	float version;
	int response_code;
	char *text;
	char *date;
	char *modified;
	char *server;
	size_t content_length;
	char *content_type;
	char *charset;

void init();
int displayInetFile(const char *url);
void printResponse(struct httpresponse response);
 * Downloads and displays a file from the internet.
 * Returns 0 on success, -1 on error.

int displayInetFile(const char *url){
	struct httpresponse response;
	char *filepath = strchr(url, '/');
	char *hostname = strndup(url, filepath - url);
	printf("hostname is %s and filepath is %s\n", hostname, filepath);
	struct hostent *host = net_gethostbyname(hostname);/*gets the host information by domain name*/
	struct sockaddr_in server;
	s32 socket = net_socket(AF_INET, SOCK_STREAM, IPPROTO_IP);/*creates a socket*/
	if(host == NULL){
		printf("Oh crap, can't find %s\n", hostname);
		return -1;
	memset(&server, 0, sizeof(server));/*clears out the sockaddr_in structure, just saw this in an example and not sure if it's needed*/
	server.sin_family = AF_INET;/*sets the socket type family to IPv4*/
	server.sin_port = htons(80);/*uses port 80 for normal HTTP*/
	memcpy(&server.sin_addr, host->h_addr_list[0], host->h_length);/*copies the host address into the sockaddr_in structure*/
	if(net_connect(socket, (struct sockaddr *)&server, sizeof(server))){
		printf("Darn, failed to connect\n");
		return -1;
		printf("Successfully connected!\n");
	int len = strlen("GET  HTTP/1.0\r\n\r\n")+strlen(filepath);/*Find the length of the string needed to store the HTTP request*/
	char *getstring = (char *)malloc(len);
	sprintf(getstring,"GET %s HTTP/1.0\r\n\r\n", filepath);/*the minimum request necessary to get back a page, the format is GET file HTTP/version\r\n\r\n basically*/

	int sent = net_write(socket, getstring, len);/*writes the request to the socket*/
	printf("sent %d of %d bytes\n", sent, len);
	int bufferlen = 1025;/*creates a buffer for receiving*/
	char *buf = (char *)malloc(bufferlen);/*creates a buffer for receiving*/
	unsigned int received = 0;
	int read = 0;
	response.text = (char *)malloc(sizeof(char)*32);
	FILE *fp;
	fp = fopen("sd:/temp.wad", "w");
	char *line = (char *)malloc(bufferlen);
	char *linebegin, *lineend;
	bool dataStarted = false;
	int headerlength = 0;
	while((read = net_read(socket,buf, bufferlen-1))>0){/*while we have more data, read it into the buffer and print it out*/
		buf[read]='\0';/*null terminate the amount read, not sure if necessary but better safe than sorry*/
		linebegin = buf;
		while((lineend = strchr(linebegin, '\n'))!=NULL){
			memset(line, '\0', 1025);
			strncpy(line, linebegin, lineend-linebegin);
				if(!strncmp(line, "HTTP/", 5)){
					sscanf(line, "HTTP/%f %d %s\n", &(response.version), &(response.response_code), response.text);
				}else if(!strncmp(line, "Content-Length", 14)){
					sscanf(line, "Content-Length: %d", &response.content_length);
				}else if(!strncmp(line, "Content-Type", 12)){
					char *space = strchr(line, ' ');
					char *sc = strchr(space, ';');
					char *eq = strchr(sc, '=');
					response.content_type = strndup((space+1), sc-space-1);
					response.charset = strndup((eq + 1), lineend - eq-1);
				}else if(!strncmp(line, "Last-Modified", 13)){
					char *space = strchr(line, ' ');
					response.modified = strndup(space,lineend - space);
				}else if(!strcmp(line, "\r")){
					printf("end of http header\n");
					dataStarted = true;
					headerlength = lineend - buf + 1;
				printf("%s\n", line);
				fprintf(fp, "%s\n", line);
			linebegin = lineend + 1;
		printf("Reached EOF\n");
		printf("Read error\n");
	if(received == response.content_length){
		printf("Received %d bytes, everything seems to be in order.\n\n", received);
		printf("Received %d of %d bytes, something went wrong.\n\n", received, response.content_length);
	//	return -1;
	//return 0;

void init(){
	/*this stuff is all from the libogc wii template*/
	rmode = VIDEO_GetPreferredMode(NULL);
	xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
	if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();

	if (!fatInitDefault()) {
		printf("fatInitDefault failure: terminating\n");

	printf("Wii HTTP Test\n\nAttempting to initialize network\n");
	/*tries to initialize network*/
	char *myip = (char *)malloc(16*sizeof(char));
	if(if_config(myip, NULL, NULL, true)){
		printf("Failed to initialize network. Goodbye.");
	printf("Network initialized. Wii's IP is %s.\n", myip);


void printResponse(struct httpresponse response){
	printf("\tHTTP/%1.1f %d %s\n", response.version, response.response_code, response.text);
	printf("\t%d bytes long\tModified: %s\n", response.content_length, response.modified);
	printf("\tContent Type: %s; charset %s\n\n", response.content_type, response.charset);

Keep in mind it works fine with HTML and PHP Files. I want it to download .bin/.mp3/etc.

I have tried uploading to free hosts.. Private servers... Both either give the 404 not found Page in temp.txt or I get
Exception DSI ...

I just want a download file code that WORKS with c++
The sample Http From HBB Doesn't work with this.

If someone could help me out it would be GREATLY Appreciated and I would gladly credit you when the app is done.
Re: I really need help!!!
September 26, 2009 02:20AM
if you are downloading a wad... make sure your chosen web server serves it right... in a normal setup your web server will not know anything about an arbitrary extension so it will not serve it... you should configure a mime type in your web server for the file to be served...
Re: I really need help!!!
September 26, 2009 02:33AM
Well I have no idea how to go about doing that. You know where I can find a tutorial or something?
Re: I really need help!!!
September 26, 2009 03:16AM
You go to Mime types in your hosting control panel / your server's settings or so and add a mime type for the file extension you are interested...

such as,

.app application/octet-stream

Sorry, there is no general way to do this...

ps : here is a sample file for you to test your app, [www.tepetaklak.com] (file is a small gif file with the extension changed to app, in my configuration I have .app extension served as application/octet-stream
Re: I really need help!!!
September 26, 2009 03:24AM
When adding the Mime type what do I put for Type?

I'll test my app with that file thanks :D

Edit: It's a no go.. Exeption DSI :(

Any other ideas?

Edited 1 time(s). Last edit at 09/26/2009 03:26AM by pokeglobe.
Re: I really need help!!!
September 26, 2009 06:20AM
It will download from other sites no problem. (.txt) but I copied the exact same .txt but it wouldn't read it from my server.
What's the deal?
Re: I really need help!!!
September 26, 2009 08:53AM
you can cheat and just change the file extension on your web server. i have an app that updates itself by downloading a wad and installing it. but i had the trouble with a wad file not downloading right from my server. i just rename the .wad to .file or .txt on my server and download it. then have my wii app rename it to .wad and everything works correctly. i didn't mess with any mime or anything like that.
Re: I really need help!!!
September 26, 2009 07:19PM
OMG I had the EXACT same idea. But it wont download .txt either. I tried a test with like one line of text and DSI error.
I can download from randome sites on the internet with google's allinurl: /rules.txt

Is my server just crap or something?
Re: I really need help!!!
September 27, 2009 09:51PM
I solved the problem myself! :D

Thanks anyways guys :)

Edited 1 time(s). Last edit at 09/28/2009 04:42AM by pokeglobe.
Sorry, only registered users may post in this forum.

Click here to login