We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › UDP send datastream problem
Page Index Toggle Pages: 1
UDP send datastream problem (Read 562 times)
UDP send datastream problem
Feb 24th, 2009, 12:12pm
 
Hello,

this is my first topic..

Problem:

I need to send UDP packet (using UDP library) a custom data structure like that:

5 char 4byte int 4byte float

My code now is:

float V0;
V0 = 1.0;
int index;
index = 10;
udp.send( "DSEL0"+index+V0, ip, port );


but on UDP trace i see:
Send packet -> address:localhost/127.0.0.1, port:49000, length: 10

now I suppose that i send 10bytes but is not correct because on documentation i read that float and int are 4byte each and the correct size of this packet would be 5+4+4 = 13 byte

On server side due this the packet cant be read correctly.

Can someone help me?

cheers
Re: UDP send datastream problem
Reply #1 - Feb 24th, 2009, 12:44pm
 
You use + which is Java's string concatenation. If you have non-string in the expression, they are converted to string using default rules (or toString() mehtod).

So you don't send binary data but textual data, probably "DSEL0101.0" which is indeed 10 bytes long.

A quick look at the UDP reference shows there is a setBuffer() method to define the size of the buffer, and send() accepts a buffer argument, although it fails to indicate what is the type of this buffer. I guess it can be a byte[]. You need to fill the array with the Ascii codes of the string and the bytes of the integer and float (perhaps using bit arithmetic beware of byte order!).

If you code the server as well, you might want to use a simpler protocol. If it is out of your control, I fear the buffer building is the way to go.
Re: UDP send datastream problem
Reply #2 - Feb 24th, 2009, 1:02pm
 
Ok, so i need a procedure to transform a float value to 4byte raw data..

any suggestion?
Re: UDP send datastream problem
Reply #3 - Feb 24th, 2009, 1:36pm
 
Ah, a quick Google search shown a page pointing to ByteBuffer class. Looks like what you need (without reading the full page!).
You can use wrap() to get a ByteBuffer out of a byte array, and putChar, putInt and putFloat looks promising!
Page Index Toggle Pages: 1