Jump to content

TCP Sockets: sending data problems


PadrinatoR
 Share

Recommended Posts

Hi!

 

I'm writing an application that lets you to control Winamp from the DS.

 

I'm using NON-BLOCKING TCP sockets to send the information, but some rare thing happens :blink:

 

When I send data very quickly (for example, if you press Up quickly, the DS sends a lot of "volume up" messages), my server application (made on Visual Basic) receives a long message with all the quickly-sent messages:

"volume upvolume upvolume up".

 

Is this normal? Is there any way to avoid this? I want to receive a lot of "volume up" messages, not a long message, because the sending from DS takes place when I stop sending messages.

 

Thanks you in advance, and I'm sorry because I'm Spanish and my English is not very good haha

 

See ya!!

 

PD: Here is my code (I'm using PAlib and DSWifi from CVS 19-7-2006):

int main(int argc, char ** argv)
{
int socket;

PA_Init();    // Initializes PA_Lib
PA_InitVBL(); // Initializes a standard VBL

PA_InitText(1, 0);

PA_InitWifi();

PA_OutputText(1, 0, 0, "Connecting...");

if(PA_ConnectWifiWFC()){
 PA_OutputText(1, 0, 1, "Connected");
}else{
 PA_OutputText(1, 0, 1, "Cannot connect");
 while (1)
 {
	 PA_WaitForVBL();
 }
}

PA_InitSocket(&socket,"192.168.0.4",32123,PA_NONBLOCKING_TCP);

// Infinite loop to keep the program running
while (1)
{
 if(Pad.Held.Up) send(socket, "volume up", 9, 0);

 PA_WaitForVBL();
}
 
return 0;
} // End of main()

Edited by PadrinatoR
Link to comment
Share on other sites

Yes, this is indeed normal behavior - TCP is a stream protocol, so individual packets doen't matter; TCP only guarantees that the data you get out at the destination will be in the same order as you put it in at the source.

 

If you want to, you can create a packet-like format, many programs do this because they'd rather have packet-style data instead of everything mashed together. One simple way to do this, in your case, would just be to insert line breaks after every command, then process one line at a time on the server when receiving data.

 

Good luck,

-Stephen

Link to comment
Share on other sites

Thanks :blink:

 

I did that but I want a not retarded control, because I press A button, for example, 5 times, and data is not received until I don't stop sending messages, so the server app processes all the messages at the same time.

 

I think it would be better to use UDP sockets hehehe

 

Thank you again :clapping:

Link to comment
Share on other sites

  • 2 weeks later...

I'm looking for information about how sockets work internally, but I haven't found anything yet, so I have a question: I suppose that the TCP sockets send information after a period of time (that's the reason because I receive in my VB app a big packet when I send a lot of packets quickly, isn't it?), is there any way to change this period of time?

Link to comment
Share on other sites

The socket system in general does not make any requriements on how quickly data is sent, but in my library they are sent as soon as is possible (usually within about 39ms, often sooner)

So, if you're receiving stuff clumped together, it's either the wifi system being slow/unreliable and the wifi lib having to retransmit a lot, or it's possible your application is not receiving the data fast enough (due to some timing issue or something), and that's the reason for your data being clumped together.

As far as how sockets work - TCP is pretty simple, only 2 things you need to know.

(1) it's a "stream", bytes put in are taken out in the same order they're put in

(2) TCP sockets, as all network systems, are designed to send data as quickly as possible; so there isn't any sort of delay beyond a few milliseconds potentially, for optimization if multiple small groups of bytes are being sent.

 

-Stephen

Link to comment
Share on other sites

Thank you!!

I think it's not problem of receiving data, because I wrote another app using the same Winsock control that I used in the server application and I wrote something like this:

 

For i=1 to 50

WinsockCtrl.SendData "asdf"

Next

 

The sent data was received correctly, 50 separated messages "asdf".

 

Anyway, I changed TCP sockets by UDP sockets and all the messages are received correctly.

 

Thanks anyway :crybaby:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...