Dswifi works but fails after 7 attempts.

ashairey
Posts: 10
Joined: Fri Jan 27, 2012 2:33 pm

Dswifi works but fails after 7 attempts.

Post by ashairey » Fri Jan 27, 2012 2:47 pm

Hello,

I have made a working server on a pc accepting tcp calls. I've tested the server with a client on another PC and i am able to connect many, many times. I have also made a client on the DS using the httpget example from the latest devkitarm code.
This ds client connects en works just fine. But, sadly there will be a but in the end :-)
I can make 7 calls with the DS client and then it hangs. If i keep the server running and closes the ds client then after restarting the ds client i can make again 7 connections. This makes me believe that the problem is at the DS side. However i really can see what's wrong here. Any help will be highly appreciated.

Code: Select all

void DoRequest(const char* url, const char* action, const char* dv, const char* cmd) {
//---------------------------------------------------------------------------------
    // Let's send a simple HTTP request to a server and print the results!
	char request_text[220];
	strcpy (request_text,"action=");
	strcat (request_text, action);
	strcat (request_text,"&dv=");
	strcat (request_text, dv);
	strcat (request_text,"&cmd=");
	strcat (request_text, cmd);
	puts (request_text);

    // Find the IP address of the server, with gethostbyname
    struct hostent * myhost = gethostbyname( url );
    if(debug) iprintf("Found IP Address!\n");
 
    // Create a TCP socket
    int my_socket;
    my_socket = socket( AF_INET, SOCK_STREAM, 0 );
    if(debug) iprintf("Created Socket!\n");

    // Tell the socket to connect to the IP address we found, on port 80 (HTTP)
    struct sockaddr_in sain;
    sain.sin_family = AF_INET;
    sain.sin_port = htons(13000);
    sain.sin_addr.s_addr= *( (unsigned long *)(myhost->h_addr_list[0]) );
    connect( my_socket,(struct sockaddr *)&sain, sizeof(sain) );
    if(debug) iprintf("Connected to server!\n");

    // send our request
    send( my_socket, request_text, strlen(request_text), 0 );
    if(debug) iprintf("Sent our request!\n");

    // Print incoming data
    if(debug) iprintf("Printing incoming data:\n");

    int recvd_len;
  
	char incoming_buffer[256];

    while( ( recvd_len = recv( my_socket, incoming_buffer, 255, 0 ) ) != 0 ) { 
		// if recv returns 0, the socket has been closed.
        if(recvd_len>0) { // data was received!
            incoming_buffer[recvd_len] = 0; // null-terminate
            if(debug) iprintf(incoming_buffer);
		}
	}

 	iprintf("Other side closed connection!\n\n");

	shutdown(my_socket,0); // good practice to shutdown the socket.
	closesocket(my_socket); // remove the socket.
		
}

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Dswifi works but fails after 7 attempts.

Post by mtheall » Mon Jan 30, 2012 5:13 pm

You might want to space out your communications with a swiWaitForVBlank(); I find that if you try to send or recv data too quickly, the wifi lib is prone to locking up. 1440 bytes per frame seems to be a magic number for me.

ashairey
Posts: 10
Joined: Fri Jan 27, 2012 2:33 pm

Re: Dswifi works but fails after 7 attempts.

Post by ashairey » Mon Jan 30, 2012 6:52 pm

I've went back to the example and tried that one but it too has the same trouble. sgtair point out at me that this might be a socket problem and indeed this part

Code: Select all

my_socket = socket( AF_INET, SOCK_STREAM, 0 );
will lock up the code because there are no free sockets left. If i wait 10-15 seconds between calls i get to 9 calls or a few more. However that is far to unstable for me to be workable. Is there a way to force the code to release the socket?

About the number of byte send and recieve that seems not to influence much. I send 46 bytes and recieve about the same. Doubling these numbers didn't lower the number of successfull tries of 6 that i can do.

ashairey
Posts: 10
Joined: Fri Jan 27, 2012 2:33 pm

Re: Dswifi works but fails after 7 attempts.

Post by ashairey » Mon Jan 30, 2012 7:22 pm

Some additional information

If i check the socket number with

Code: Select all

my_socket = socket( AF_INET, SOCK_STREAM, 0 );
    iprintf("Created Socket %i\n", my_socket);
then i see the number go up to 6 and when it's 7 the program stalls.
What i expected to see is that this number will get lower or 0 over time because

Code: Select all

	shutdown(my_socket,0); // good practice to shutdown the socket.
	closesocket(my_socket); // remove the socket.
will free the sockets again.
I placed also many swiWaitForVBlank(); between the statements to give the code time to resolve but no success.

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

Re: Dswifi works but fails after 7 attempts.

Post by elhobbs » Mon Jan 30, 2012 8:03 pm

you should be checking the return value from calls to "send". send is not guarenteed to transmit all of the requested data - you will need to loop untill all of the data is sent - the return value is the number of bytes sent. on the ds this important because dswifi has a very small transmission window - only one packet in transit at a time.

ashairey
Posts: 10
Joined: Fri Jan 27, 2012 2:33 pm

Re: Dswifi works but fails after 7 attempts.

Post by ashairey » Mon Jan 30, 2012 8:49 pm

@Elhobbs
I start the httpGet function by hand (after pressing a key on de NDS) so there is lots of time between calls and i see that each call is proceesed completly by printing the resopnse on screen. The code won't reach the send anymore on the 7th call. About the data send, this is max 46 bytes.

I see my_socket = socket( AF_INET, SOCK_STREAM, 0 ) counting up till 7 and then becomes -1 due no more sockets available. Even if i put this in a loop to wait for a free socket i never see that a previous socket gets released with shutdown(my_socket,0); and closesocket(my_socket);

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Dswifi works but fails after 7 attempts.

Post by mtheall » Mon Jan 30, 2012 9:12 pm

Maybe you should try shutdown(my_socket, SHUT_RDWR) instead. Also, you should check the return values from shutdown() and closesocket(). Maybe they will give some insight.

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Dswifi works but fails after 7 attempts.

Post by mtheall » Mon Jan 30, 2012 9:31 pm

Okay I lie, SHUT_RDWR does nothing. That parameter is completely ignored in the code. Also, it looks like the code allows for up to 32 sockets.

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

Re: Dswifi works but fails after 7 attempts.

Post by elhobbs » Mon Jan 30, 2012 10:14 pm

you could try forceclosesocket instead of closesocket

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Dswifi works but fails after 7 attempts.

Post by mtheall » Mon Jan 30, 2012 10:46 pm

I changed your code to use the code tag instead of the quote tag.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest