简体   繁体   English

如何在Java中更有效地管理网络协议

[英]how to more efficiently manage network protocol in java

I'm developing a network based program in java, and I designed my own network protocol. 我正在用Java开发基于网络的程序,并设计了自己的网络协议。 Here is what I usually did to handle incoming message: 这是我通常处理传入消息的方法:

socket = ss.accept();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String msg = null;
while(true)
if((msg=in.readLine())!=null){
//handle msg with designed protocol here
    } 

It is ok to do this when the protocol is simple enough, but when it becomes complex, it is overwhelming to put everything here,it also makes my code harder to understand. 当协议足够简单时,可以这样做,但是当协议变得复杂时,将所有内容放在此处是不堪重负的,这也使我的代码更难以理解。 My question is that, is there any way to store the protocol code in another place? 我的问题是,有没有办法将协议代码存储在另一个地方? Thank you in advance! 先感谢您!

Sure, why not? 当然可以,为什么不呢? Just make another class and make a method that does the handling. 只需创建另一个类并创建一个进行处理的方法即可。 Send the msg to it and and do the handling there. msg发送给它,然后在那里进行处理。 Or make another method in the same class with: 或在同一类中使用以下方法制作另一个方法:

public (static) void handle(String msg) { //Handle message }

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

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