Java 套接字发送和接收字节数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1176135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:32:55  来源:igfitidea点击:

Socket send and receive byte array

javaarrayssockets

提问by

In server, I have send a byte array to client through Java socket

在服务器中,我通过 Java 套接字向客户端发送了一个字节数组

byte[] message = ... ;

DataOutputStream dout = new DataOutputStream(client.getOutputStream());
dout.write(message);

How can I receive this byte array from client?

如何从客户端接收这个字节数组?

回答by Bombe

First, do not use DataOutputStreamunless it's really necessary. Second:

首先,DataOutputStream除非真的有必要,否则不要使用。第二:

Socket socket = new Socket("host", port);
OutputStream socketOutputStream = socket.getOutputStream();
socketOutputStream.write(message);

Of course this lacks any error checking but this should get you going. The JDK API Javadocis your friend and can help you a lot.

当然,这缺少任何错误检查,但这应该可以帮助您前进。在JDK API的Javadoc是你的朋友,可以帮助你很多。

回答by Brian Agnew

There is a JDK socket tutorial here, which covers both the server and client end. That looks exactly like what you want.

有一个JDK插座教程在这里,它涵盖了服务器和客户端两种。这看起来和你想要的完全一样。

(from that tutorial) This sets up to read from an echo server:

(来自该教程)这设置为从回显服务器读取:

    echoSocket = new Socket("taranis", 7);
    out = new PrintWriter(echoSocket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(
                                echoSocket.getInputStream()));

taking a stream of bytes and converts to strings via the reader and using a default encoding (not advisable, normally).

获取字节流并通过读取器转换为字符串并使用默认编码(通常不建议)。

Error handling and closing sockets/streams omitted from the above, but check the tutorial.

上面省略了错误处理和关闭套接字/流,但请查看教程。

回答by eckes

You need to either have the message be a fixed size, or you need to send the size or you need to use some separator characters.

您需要将消息设为固定大小,或者需要发送大小或需要使用一些分隔符。

This is the easiest case for a known size (100 bytes):

这是已知大小(100 字节)的最简单情况:

in = new DataInputStream(server.getInputStream());
byte[] message = new byte[100]; // the well known size
in.readFully(message);

In this case DataInputStreammakes sense as it offers readFully(). If you don't use it, you need to loop yourself until the expected number of bytes is read.

在这种情况下DataInputStream,它提供了有意义的readFully()。如果你不使用它,你需要自己循环,直到读取到预期的字节数。

回答by franta kocourek

Try this, it's working for me.

Sender:

试试这个,它对我有用。

发件人:

byte[] message = ...
Socket socket = ...
DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());

dOut.writeInt(message.length); // write length of the message
dOut.write(message);           // write the message


Receiver:


接收者:

Socket socket = ...
DataInputStream dIn = new DataInputStream(socket.getInputStream());

int length = dIn.readInt();                    // read length of incoming message
if(length>0) {
    byte[] message = new byte[length];
    dIn.readFully(message, 0, message.length); // read the message
}