Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-26-2011, 04:18 PM   #1
LQ Newbie
Registered: Jan 2009
Distribution: Slackware, Ubuntu netbook remix, Windows 7
Posts: 17

Rep: Reputation: 0
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?
Old 02-26-2011, 06:21 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
Old 02-26-2011, 06:29 PM   #3
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
1 members found this post helpful.
Old 03-01-2011, 08:26 AM   #4
LQ Newbie
Registered: Jan 2009
Distribution: Slackware, Ubuntu netbook remix, Windows 7
Posts: 17

Original Poster
Rep: Reputation: 0
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.


java, serialization, sockets

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] UDP packets not seen with wireshark when sending too fast mibo Programming 1 09-04-2009 03:31 AM
segmentation fault while sending UDP packets! bizoo Programming 5 07-17-2009 03:33 AM
Udp packets dropped while sending in RHEL4 chinoy Linux - Networking 1 08-21-2008 10:07 PM
How to specify source port when sending UDP packet socialjazz Programming 4 09-19-2006 08:15 PM
Sending files over udp sockets in C raszagal Programming 9 11-27-2004 11:49 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:05 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration