ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Distribution: Slackware, Ubuntu netbook remix, Windows 7
Posts: 17
Rep:
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?
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
// 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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.