Java 自定义协议进行 socket 连接的简单认证
和消息
1、 服务器端的几次连接认证和状态的改变如上图所示:
1、服务器端进行协议认证的关键代码:
public static String processInput(String theInput) {
String theOutput = null;
if (state == WAITING) {
theOutput = "waiting";
state = 1;
}
else if (state == 1) {
if (theInput.equalsIgnoreCase("hello!!!")) {
theOutput = "step1...";
state = 2;
}
else {
theOutput = "Sorry, you are wrong. Please try again.";
state = 0;
}
}
else if (state == 2) {
if (theInput.equalsIgnoreCase("peng_zhanxuan")) {
theOutput = "step2";