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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-26-2011, 04:18 PM
|
#1
|
|
LQ Newbie
Registered: Jan 2009
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?
|
|
|
|
02-26-2011, 06:21 PM
|
#2
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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 
|
|
|
|
02-26-2011, 06:29 PM
|
#3
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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.
|
03-01-2011, 08:26 AM
|
#4
|
|
LQ Newbie
Registered: Jan 2009
Distribution: Slackware, Ubuntu netbook remix, Windows 7
Posts: 17
Original Poster
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:13 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|