LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 02-26-2011, 04:18 PM   #1
Mupple
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:

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

int data = bin.read();
System.out.print(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:

Code:
int data[][] = bin.read();
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
paulsm4
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
paulsm4
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:

http://java.sun.com/developer/techni...s/ALT/sockets/

Code:
// Code Sample 8: SerializedObject.java 
import java.io.*;
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:
// Code Sample 9: ArrayClient.java 
import java.io.*;
import java.net.*;

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];
        so1.setArray(dataset1);
        so2.setArray(dataset2);
        // write the objects to the server
        oos.writeObject(so1);
        oos.writeObject(so2);
        oos.flush();
        // 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] + "   ");
        }
        oos.close();
        ois.close();
      } catch(Exception e) {
        System.out.println(e.getMessage());
      }
   }
}
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
Mupple
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.
 
  


Reply

Tags
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


All times are GMT -5. The time now is 01:34 PM.

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