Original OP follows.
***
What I'm trying to do here is send the contents of the EEPROM/Flash (for my project, savehost) to a PC.
Here's a complete program that I think is the smallest reproduction of what I'm running into. Plug it into the arm9 example template as main.c, add -ldswifi9 to the libraries, and build. Use netcat to connect to port 1 on your DS (i.e. nc 1.2.3.4 1), which will kick off a neverending stream of bytes to your PC.
If you want to mirror the logic that I use in savehost right now, uncomment the vblanks and use the alternate to_send calculation.
What I'm running into is that maybe 80-90% of the time it seems to work okay, and quickly, but sometimes things choke up. I'll start getting alternating send() returns of 1024, then 396 (which add up to 1420, which seems to be the maximum bytes that can be sent in a single packet.) And when it chokes it goes really slow... and sometimes it hangs entirely.
I'm hoping that I'm merely something wrong with my use of send() and friends here. But I also think that I might be running into a problem with the TCP stack.
Code: Select all
#include <nds.h>
#include <dswifi9.h>
#include <netinet/in.h>
#include <stdio.h>
#define BUFSIZE 65536
#define PORT 1
#define min(a,b) (((a) < (b)) ? (a) : (b))
int main(void) {
int serv_sock, sock, conn_addrlen, i, sent, to_send;
struct sockaddr_in serv_addr, conn_addr;
uint32 addr;
char *buf, *buf_p;
consoleDemoInit();
iprintf("filling buffer\n");
buf = malloc(BUFSIZE);
for(i = 0; i < BUFSIZE; i ++) {
buf[i] = (char)(i & 0xff);
}
iprintf("associating\n");
Wifi_InitDefault(WFC_CONNECT);
addr = Wifi_GetIP();
iprintf("address is %d.%d.%d.%d\n", addr & 0xff, (addr >> 8) & 0xff,
(addr >> 16) & 0xff, (addr >> 24) & 0xff);
serv_sock = socket(AF_INET, SOCK_STREAM, 0);
if(serv_sock == -1) return -1;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(serv_sock,
(struct sockaddr *)&serv_addr,
sizeof(serv_addr))) return -1;
if(listen(serv_sock, 0)) return -1;
iprintf("listening\n");
conn_addrlen = sizeof(conn_addr);
sock = accept(serv_sock, (struct sockaddr *)&conn_addr,
&conn_addrlen);
if(sock == -1) return -1;
closesocket(serv_sock);
iprintf("sending\n");
while(1) {
buf_p = buf;
while((buf_p - buf) < BUFSIZE) {
to_send = BUFSIZE - (buf_p - buf);
/* to_send = min(BUFSIZE - (buf_p - buf), 1024); */
iprintf("%d bytes at %d: ", to_send, (buf_p - buf));
sent = send(sock, buf_p, to_send, 0);
if(sent < 1) {
iprintf("connection dropped\n");
return -1;
}
iprintf("sent %d\n", sent);
/* swiWaitForVBlank(); */
/* swiWaitForVBlank(); */
buf_p += sent;
}
}
}