Java - sending a 2d array over UDP
I'm currently writing a program which will eventually send two 2d arrays to another machine using UDP, and then back again.
To do this I'm converting to a byte array which as far as I'm aware is the only way possible. I'm sure the problem is on the receiving program which receives like this: Code:
DatagramSocket socket = new DatagramSocket(2000); Code:
int data[][] = bin.read(); |
Personally, if this is anything more than a "toy program" for personal curiousity, I'd suggest looking into using RMI.
But if you want to use a UDP stream ... ... then you need some way to tell the client how much data it receives: 1. Send a fixed-length (n bytes) block, then read exactly n bytes 2. Send a byte count, first, then the data 3. Parse the stream for some "end of data" delimiter As far as the syntax for reading your 1-D byte stream into a 2-D int array... you basically get to use a "for" loop ;) |
Hi -
About 30 seconds after I clicked "send", I found another approach: leverage Java's "Serializable" interface interface with I/O streams and sockets: http://java.sun.com/developer/techni...s/ALT/sockets/ Code:
// Code Sample 8: SerializedObject.java Code:
// Code Sample 9: ArrayClient.java |
I just wanted to say thanks, because this is an amazing find. I was googling and trying weird code for a good couple of hours but this is awesome.
and yeah, this is an experimental program rather than any sort of real-world solution. |
All times are GMT -5. The time now is 01:51 PM. |