简体   繁体   English

Java TCPIP检测完整数据包消息

[英]Java TCPIP Detecting Full Packet Message

I have developed an application using TCPIP in Java though the use of ServerSockets and Sockets. 我已经通过使用ServerSockets和Sockets在Java中使用TCPIP开发了一个应用程序。 The application is a Listener that listens for messages arriving at a specified port. 该应用程序是一个侦听器,用于侦听到达指定端口的消息。 When a message arrives it is processed and various things are done. 消息到达时,将对其进行处理并完成各种操作。 My company has provided a small application that acts as a client that sends a message. 我公司提供了一个小型应用程序,可以充当发送消息的客户端。 Recently, they have extended the small application that that sends multiple messages in one go. 最近,他们扩展了小型应用程序,该应用程序可以一次性发送多个消息。

Now, my application that is the Listener does not fully receive the multiple messages. 现在,作为监听器的我的应用程序无法完全接收多条消息。 I have tried to increase the default buffer on the ServerSocket and still I don't receive the full message. 我试图增加ServerSocket上的默认缓冲区,但仍然没有收到完整的消息。 How can this be debugged? 如何调试? The company has a working version of another application that instantly sends the messages and they are all received instantly. 该公司拥有另一个应用程序的工作版本,该应用程序可以立即发送消息,并且它们都会立即收到。 The Listener application receives one message in one go. 侦听器应用程序一次性接收一条消息。 The below code is a sample: 下面的代码是一个示例:

@Startup
@Singleton
public class Listener {

       private ServerSocket serverSocket;
       private Socket socket;
       @Resource
       private ScheduledExecutorService executor;

       @PostConstruct
       public void init() {
              serverSocket = new ServerSocket(portNumber);
              Runnable runnable = new Runnable() {
                    public void run() {
                       try {
                         if(socket == null) {
                            socket = serverSocket.accept();
                            socket.setKeepAlive(true);
                         }
                         else if(socket.isClosed()) {
                             socket = serverSocket.accept();
                             socket.setKeepAlive(true);
                         }
                         char[] messages = buildMessage(socket.getInputStream());
                        }
                        catch(Exception e) {
                            System.out.println("Exception occurred");
                            e.printStackTrace();
                        }
                    }
              };
              executor.scheduleAtFixedRate(runnable, 0, 0, TimeUnit.SECONDS);
       }

     public char[] buildMessage(InputStream inputStream) throws Exception {
            StringBuilder message = null;
            char[] values = null;
            if(inputStream != null) {
                message = new StringBuilder();
                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                int byteRead = bufferedInputStream.read();
                while(byteRead != -1) {
                     char value = (char) byteRead;
                     message.append(value);
                     // check how many bytes available
                     if(bufferedInputStream.available() != 0) {
                         byteRead = bufferedInputStream.read();
                     } 
                     else {
                        // to avoid blocking of data
                        break;
                     }
                }
             }
         }
     }

There is no such thing as a message in TCP. TCP中没有消息。

It is a byte stream, and it can be received in chunks as small as a byte at a time. 它是一个字节流,可以一次接收与一个字节一样小的块。

If you want messages you must implement them yourself, by reading until you have everything you need. 如果您想要消息,则必须自己阅读,直到您拥有所需的一切为止,自己实现它们。

That may require altering or providing an application protocol such as lines, XML, length-word prefixes, etc., so you know where a message stops. 这可能需要更改或提供应用程序协议,例如行,XML,长字前缀等,因此您知道消息在哪里停止。

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

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