-   Programming (
-   -   Java - sending a 2d array over UDP (

Mupple 02-26-2011 04:18 PM

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:


DatagramSocket socket = new DatagramSocket(2000);
DatagramPacket packet = new DatagramPacket(new byte[5000] , 5000);
ByteArrayInputStream bin = new ByteArrayInputStream(packet.getData());

int data =;

obviously it is having a problem as it is trying to receive as an int, rather than an int[][], but it won't compile if I try something like:


int data[][] =;
So I'm basically I'm a bit stuck. Does anyone have any ideas as to how to send/receive an array (preferably 2d) using UDP in java. Or can point me in the right direction?

paulsm4 02-26-2011 06:21 PM

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 ;)

paulsm4 02-26-2011 06:29 PM

Hi -

About 30 seconds after I clicked "send", I found another approach: leverage Java's "Serializable" interface interface with I/O streams and sockets:


// Code Sample 8:
import java.util.*;

public class SerializedObject implements Serializable {
  private int array[] = null;

  public SerializedObject() {

  public void setArray(int array[]) {
    this.array = array;

  public int[] getArray() {
    return array;


// Code Sample 9:

public class ArrayClient {
  public static void main(String argv[]) {
      ObjectOutputStream oos = null;
      ObjectInputStream ois = null;
      // two arrays
      int dataset1[] = {3, 3, 3, 3, 3, 3, 3};
      int dataset2[] = {5, 5, 5, 5, 5, 5, 5};
      try {
        // open a socket connection
        Socket socket = new Socket("YourMachineNameORipAddress", 4000);
        // open I/O streams for objects
        oos = new ObjectOutputStream(socket.getOutputStream());
        ois = new ObjectInputStream(socket.getInputStream());
        // create two serialized objects
        SerializedObject so1 = new SerializedObject();
        SerializedObject so2 = new SerializedObject();
        SerializedObject result = null;
        int outArray[] = new int[7];
        // write the objects to the server
        // read an object from the server
        result = (SerializedObject) ois.readObject();
        outArray = result.getArray();
        System.out.print("The new array is: ");
        // after unpacking the array, iterate through it
        for(int i=0;i<outArray.length;i++) {
          System.out.print(outArray[i] + "  ");
      } catch(Exception e) {

The same concept, of course, can easily be extended to any arbitrarily complex object - including a 2D int array :)

Mupple 03-01-2011 08:26 AM

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 06:05 AM.