使用TCP连接获取字节数组 [英] Getting byte arrays using TCP connections

查看:420
本文介绍了使用TCP连接获取字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UDP 发送/接收数据,但现在我想切换到 TCP 以避免数据包丢失。

I was using UDP to send/receive data but I now want to switch to TCP to avoid packet loss.

我已经阅读了有关 TCP 的几本教程,并注意到它不是像 UDP , TCP 使用InputStream / OutputStream。

I've read several tutorials on TCP and noticed that instead of using DatagramPacket like UDP, TCP uses InputStream/OutputStream.

我们如何从DataInputStream中获取字节[],类似于以下内容:

How do we get the byte[] from DataInputStream, something that's similar to this:

byte[] receiveData = new byte[64000];
DatagramPacket receivePacket = new DatagramPacket(receiveData,receiveData.length); 
receiveData=receivePacket.getData();


推荐答案

答案分为两部分。处理与您的问题有关的2个独立问题。

The answer has 2 parts. Dealing with 2 separate problems your questions is related to.

1 。网络事实

TCP本质上是基于流的。即先发送字节[1000]然后发送字节[1200]与一次发送字节[2200]是无法区分的。通过网络实际发送的数据很可能是2个数据包,第一个是1400字节的数据包,第二个是800或1401和799,并且每次都可能有所不同。接收方无法知道发送方首先实际发送了1000个字节,然后又发送了1200个字节。这是设计使然在网络中。 Java与这个事实无关。

TCP is inherently stream based. i.e. Sending byte[1000] first and then byte[1200], is indistinguishable from sending byte[2200] once. What is actually send over the network can very likely be 2 packets, first being a packet with 1400 bytes and the second being 800, or 1401 and 799, and can vary each time. The receiver has no way to know the sender actually sent 1000 bytes first, and then sent 1200 bytes. This is by design in network. Java has nothing to do with this fact. And you can do nothing with it.

2 。 Java实现

在发送方。首先,您需要 OutputStream os = tcpsocket.getOutputStream(); 。然后,每次都需要 os.write(byteArray)。在接收方,您需要 InputStream是= tcpsocket.getInputStream(); 。然后,每次都需要 is.read(byteArray)。请注意,在接收方,将返回实际已填充的 byteArray 的数量。它可以是1到 byteArray 的容量之间的任何数字,并且与发送方的实际发送方式无关。

On the sender side. First, you need OutputStream os = tcpsocket.getOutputStream();. And then, each time, you need os.write(byteArray). On The receiver side, you need InputStream is = tcpsocket.getInputStream();. And then, each time, you need is.read(byteArray). Note that on the receiver side, how much of the byteArray is actually filled will be returned. It may be any number between 1 and the capacity of the byteArray, and is irrelevant to how the sender actually sent it.

为简化任务,您可以使用 DataInputStream is = new DataInputStream(tcpsocket.getInputStream()); 开头,并在每次需要阅读某些内容时使用 is.readFully(byteArray)。这样,可以保证 byteArray 将始终被填充。

To ease the task, you may use DataInputStream is = new DataInputStream(tcpsocket.getInputStream()); at the beginning, and use is.readFully(byteArray) each time you need to read something. This way, it can be guaranteed that byteArray will always be filled.

但是您永远无法知道多少字节如果实际长度是可变的,您应该会收到,除非您添加一些额外的信息。例如,使用4个字节先发送长度。您实际如何执行此操作通常与您的实际用例密切相关。由你决定

But you can never know how many bytes you should receive if the actual length is variable unless you add some extra information. For example, send the length first, using 4 bytes. How you will actually do this is usually closely related to your actual use case. And it's up to you

这篇关于使用TCP连接获取字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆