怎样将字节数组发送给特定的接收者

huangapple go评论82阅读模式
英文:

How to send bytearray to specific receiver

问题

以下是翻译好的部分:

我在Java中有一个TCP服务器和客户端。服务器可以向客户端发送命令,客户端随后将执行该命令,例如:将图像发送到服务器。

我使用字节数组发送数据,这部分已经正常工作。

但是假设我想要分别发送图像和文件。服务器应该如何知道哪个是正确的字节数组?或者如果我想要进行语音聊天(需要持续发送字节数组),同时又要单独发送图像呢?

这是我发送字节的代码:

Client.java

    public void writeBytes(byte[] bytes, Socket socket) throws IOException {
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        out.write(bytes);
        out.flush();
    }

这是我接收并将其转换为图像的代码:

Server.java

    public BufferedImage writeScreenshot(Socket socket, int length) throws IOException {
        DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
        byte[] buffer = new byte[length];
        in.readFully(buffer);

        return ImageIO.read(new ByteArrayInputStream(buffer));
    }
英文:

I have a TCP Server and Client in Java. The Server can send commands to the Client, the Client will then execute the command, for example: send an image to the Server.

Im sending the data with a bytearray and thats working.

But lets imagine, I want to send an image and a file separately. How would the Server supposed to know which is the right bytearray? Or if I want to make a VoiceChat (which needs to be sending bytearrays continously) and separately sending an image?

Thats my code send bytes:

Client.java

    public void writeBytes(byte[] bytes, Socket socket) throws IOException {
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        out.write(bytes);
        out.flush();
    }

Thats my code to receive and convert them to an Image:

Server.java

    public BufferedImage writeScreenshot(Socket socket, int length) throws IOException {
        DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
        byte[] buffer = new byte[length];
        in.readFully(buffer);

        return ImageIO.read(new ByteArrayInputStream(buffer));
    }

答案1

得分: 0

你需要设计一种用于通信的“协议”。协议定义了可以交换的消息以及它们在较低级别数据流中的表示方式。

一个快速简单的协议是,您首先发送要发送的数据的长度,然后发送数据:

DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeInt(bytes.length);
out.write(bytes);
out.flush();

接收方现在必须读取长度字段:

DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
int length = in.readInt();
byte[] buffer = new byte[length];
in.readFully(buffer);

当涉及到诸如语音聊天之类的应用时,协议必须变得更加复杂。每条消息必须具有元数据,比如它包含的数据类型:图像、语音或其他内容。此外,您可能不想从头开始设计此协议,而是希望使用已经存在的东西 - 例如实时流媒体协议(RTSP)。

英文:

You need to design a "protocol" for the communication. A protocol defines what are the messages that can be exchanged and how they are represented in the lower level data stream.

A quick and easy protocol is where you first send the length of the data you are going to send, and then the data:

    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    out.writeInt(bytes.length);
    out.write(bytes);
    out.flush();

The receiver now has to read the length field:

    DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
    int length = in.readInt()
    byte[] buffer = new byte[length];
    in.readFully(buffer);

When you get to applications like voice chat, the protocol has to get more complex. Each message has to have metadata, like what type of data it contains: image or voice or something else. Also you would likely not want to design this protocol from scratch, but use something that already exists - for example the real-time streaming protocol (RTSP).

huangapple
  • 本文由 发表于 2020年4月10日 20:23:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/61140171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定