2121import com .google .gson .Gson ;
2222
2323import org .jetbrains .annotations .NotNull ;
24+ import org .yuezhikong .JavaIMAndroid .Client ;
2425import org .yuezhikong .JavaIMAndroid .ConfigFile ;
2526import org .yuezhikong .JavaIMAndroid .Encryption .KeyData ;
2627import org .yuezhikong .JavaIMAndroid .Encryption .RSA ;
3839import java .net .Socket ;
3940import java .nio .charset .StandardCharsets ;
4041import java .util .Scanner ;
42+ import java .util .Timer ;
43+ import java .util .TimerTask ;
4144import java .util .UUID ;
4245
4346import javax .crypto .SecretKey ;
@@ -82,6 +85,21 @@ private void RequestRSA(@NotNull String key, @NotNull Socket client,@NotNull Log
8285 do {
8386 json = reader .readLine ();
8487 } while (json == null );
88+ if ("Decryption Error" .equals (json ))
89+ {
90+ logger .info ("你的服务端公钥疑似不正确" );
91+ logger .info ("服务端返回:Decryption Error" );
92+ logger .info ("服务端无法解密" );
93+ logger .info ("客户端正在退出" );
94+ if (this instanceof Client )
95+ {
96+ ((Client ) this ).TerminateClient ();
97+ }
98+ else
99+ {
100+ System .exit (0 );
101+ }
102+ }
85103 json = unicodeToString (json );
86104 protocol = getClient ().protocolRequest (json );
87105 if (protocol .getMessageHead ().getVersion () != ConfigFile .ProtocolVersion || !("Test" .equals (protocol .getMessageHead ().getType ())))
@@ -161,6 +179,7 @@ public void start(String ServerAddress,int ServerPort)
161179 {
162180 Instance = this ;
163181 Logger logger = LoggerInit ();
182+ Timer timer = new Timer (true );
164183 logger .info ("正在连接主机:" + ServerAddress + " ,端口号:" + ServerPort );
165184 try {
166185 Socket client = new Socket (ServerAddress , ServerPort );
@@ -177,6 +196,20 @@ public void start(String ServerAddress,int ServerPort)
177196 writer .write ("你好,服务端" );
178197 writer .newLine ();
179198 writer .flush ();
199+ TimerTask task = new TimerTask ()
200+ {
201+ @ Override
202+ public void run () {
203+ try {
204+ writer .write ("Alive" );
205+ writer .newLine ();
206+ writer .flush ();
207+ } catch (IOException e ) {
208+ e .printStackTrace ();
209+ }
210+ }
211+ };
212+ timer .schedule (task ,0 ,ConfigFile .HeartbeatInterval );
180213 //测试通讯协议
181214 NormalProtocol protocol = protocolRequest (unicodeToString (reader .readLine ()));
182215 if (protocol .getMessageHead ().getVersion () != ConfigFile .ProtocolVersion || !("Test" .equals (protocol .getMessageHead ().getType ())))
@@ -337,6 +370,11 @@ else if ("Fail".equals(protocol.getMessageBody().getMessage()))
337370 SendMessage (logger ,client ,aes );
338371 } catch (IOException ignored ) {
339372 }
373+ timer .cancel ();
374+ if (this instanceof Client )
375+ {
376+ ((Client ) this ).TerminateClient ();
377+ }
340378 }
341379
342380 protected void SendMessageToServer (String UserInput , BufferedWriter writer , AES aes ) throws IOException
0 commit comments