我想通过网络传输类型的double[],然后设法将它传输回接收端的double[]。我不太清楚该怎么做。我试图将接收到的字符串转换为char[],然后将所有字符解析为double[]。然而,这是行不通的,双份有不同的数据。我需要这样做,以便为opencv制定一个网络协议,以便轻松地传输Mat。
因此,数据就是这样发送的:
private void send_info(int row,int col, double[] data) {
//Convert data to String, separated by : to indicate change
//char[] sendit = data.toString().toCharArray();
out.println("INF:ROW:"+row+":COL"+":"+col+":"+data);
}这就是它的接收方式:
private void setInfo(String input) {
input = input.trim();
input=input.replace("INF:","");
String inputs[] = input.split(":");
System.out.println(inputs[1]);
int row = Integer.parseInt(inputs[1]);
int col = Integer.parseInt(inputs[3]);
//double[] data = magic(inputs[4]);
// What I need ^
frame.put(row,col,data);
}发布于 2016-09-16 01:30:32
根本不要改变他们的信仰。浪费时间和空间。直接做就行。发送double[] doubles
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
dos.writeInt(doubles.length); // send the array length
for (d : doubles)
{
dos.writeDouble(d);
}
dos.flush();改为:
DataInputStream din = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
double[] doubles = new double[dis.readInt()];
for (int i = i; i < doubles.length; i++)
{
doubles[i] = dis.readDouble();
}或者可以使用ObjectOutputStream.writeObject()和ObjectInputStream.readObject()同时写入和读取整个数组。或者您可以使用NIO和DoubleBuffer:left作为读者的练习。
发布于 2016-09-16 09:18:02
作为EJP’s answer的增编,这里有一个NIO解决方案:
发送
try(SocketChannel ch=SocketChannel.open(
new InetSocketAddress(InetAddress.getLocalHost(), 12345))) {
ByteBuffer buf=ByteBuffer.allocateDirect(doubles.length*Double.BYTES+Integer.BYTES);
buf.putInt(doubles.length).asDoubleBuffer().put(doubles);
buf.clear();
while(buf.hasRemaining()) ch.write(buf);
}接收
final int DEFAULT_BUFFER_SIZE = 4096;
try(ServerSocketChannel ss=ServerSocketChannel.open()
.bind(new InetSocketAddress(InetAddress.getLocalHost(), 12345));
SocketChannel ch=ss.accept()) {
ByteBuffer bb=ByteBuffer.allocateDirect(DEFAULT_BUFFER_SIZE);
bb.limit(Integer.BYTES);
while(bb.hasRemaining()) if(ch.read(bb)<0) throw new EOFException();
bb.flip();
int size=bb.getInt(), byteSize=size*Double.BYTES;
if(bb.capacity()<byteSize) bb=ByteBuffer.allocateDirect(byteSize);
else bb.clear().limit(byteSize);
while(bb.hasRemaining()) if(ch.read(bb)<0) throw new EOFException();
double[] doubles=new double[size];
bb.flip();
bb.asDoubleBuffer().get(doubles);
return doubles;
}显然,由于预先不知道的双数组长度,缓冲区管理在接收端变得更加复杂。
如果我们希望减少传输的数量,即避免只对前四个字节执行不同的I/O操作,则该方法将变得更加复杂:
final int DEFAULT_BUFFER_SIZE = 4096;
try(ServerSocketChannel ss=ServerSocketChannel.open()
.bind(new InetSocketAddress(InetAddress.getLocalHost(), 12345));
SocketChannel ch=ss.accept()) {
ByteBuffer bb=ByteBuffer.allocateDirect(DEFAULT_BUFFER_SIZE);
while(bb.position()<4) if(ch.read(bb)<0) throw new EOFException();
bb.flip();
int size=bb.getInt(), byteSize=size*Double.BYTES;
if(bb.remaining()<byteSize) {
if(bb.capacity()<byteSize) bb=ByteBuffer.allocateDirect(byteSize).put(bb);
else bb.compact().limit(byteSize);
while(bb.hasRemaining()) if(ch.read(bb)<0) throw new EOFException();
bb.flip();
}
else bb.limit(bb.position()+byteSize);
double[] doubles=new double[size];
bb.asDoubleBuffer().get(doubles);
return doubles;
}但请注意,该格式与用DataOutputStream在EJP’s solution中创建的格式相同,因此可以将NIO发送代码与旧的I/O接收代码…组合起来。
https://stackoverflow.com/questions/39522545
复制相似问题