简体   繁体   English

套接字输入流不会逐段读取

[英]Socket input stream doesn't read fragment by fragment

I have problem in my app, I read from socket with ip=192.168.0.3 and port =502. 我的应用程序有问题,我从套接字读取ip = 192.168.0.3和port = 502。 Another program write messages to that port fragment by fragment ( mac fragmnet length is 205 bytes) and I need to read that messages fragment by fragment. 另一个程序通过片段将消息写入该端口片段(mac fragmnet长度为205字节),我需要逐段读取该消息。 But when I wrote this I get all fragments in one big. 但是当我写这篇文章时,我将所有片段都放在一个大片中。

boolean last=false;
                int numberFragment=0;
                while (((!last))  ) {

                    numberReceived = socketInputStream.read(buffer);
                    numberFragment++;
                    tempBuffer = new byte[numberReceived];
                    ByteBuffer baferce = ByteBuffer.wrap(tempBuffer);
                    baferce.put(buffer, 0, numberReceived);
                    //System.out.println("RECEIVED="+(new String(tempBuffer)));
                    last=ResponseFragmentCheck.IsLastFragment(tempBuffer, numberFragment);
                    System.out.println("LAST ="+last);

                    PrintBytesArray(tempBuffer);
                    received.add(tempBuffer);
                }

Is there any way i Java to read fragment by fragment ? 我有什么方法可以逐段读取片段吗?

If you want Java to read fragment by fragment, there are two things you must do: 如果您希望Java逐段读取,则必须执行以下两项操作:

  1. Use UDP, not TCP. 使用UDP,而不是TCP。 You may already be doing this. 你可能已经这样做了。
  2. Use Socket.receive() to receive a DatagramPacket . 使用Socket.receive()接收DatagramPacket

Any stream method may fail to respect the fragment boundaries. 任何流方法都可能无法遵守片段边界。

There are no 'fragments' in TCP. TCP中没有“碎片”。 It is a byte stream. 它是一个字节流。 If you want to read messages of a specific length, use DataInputStream.readFully(). 如果要读取特定长度的消息,请使用DataInputStream.readFully()。 If you want to read messages the same way you sent them you have to prefix a length word to each, or use a type-length-value protocol, or a self-describing protocol such as XML. 如果要以与发送消息相同的方式读取消息,则必须为每个消息添加长度字,或使用类型长度值协议或自描述协议(如XML)。

If you are using TCP, make sure your other program calls flush() (or equivalent in other languages) after writing one fragment. 如果您使用的是TCP,请确保您的其他程序在写入一个片段后调用flush() (或其他语言的等效语句)。 Otherwise your fragments may be collected by the sending TCP stack until enough data is there to fill one TCP packet (depending on implementation and maybe some settings). 否则,您的片段可能会被发送TCP堆栈收集,直到有足够的数据来填充一个TCP数据包(取决于实现和某些设置)。

Of course, you still can't be sure to receive the packets/fragments one by one, but you'll at least be sure to receive them as fast as possible. 当然,您仍然不能确定逐个接收数据包/片段,但您至少一定要尽快收到它们。

If you need your packet structure, either use another protocol instead of TCP, or a protocol over TCP, which shows you when a packet starts and ends. 如果您需要数据包结构,请使用其他协议而不是TCP,或使用TCP协议,它会在数据包开始和结束时显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM