简体   繁体   中英

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. 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. Or make another method in the same class with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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