current position:Home>Server socket + Android socket can only send a message once after connecting. The second sending does not respond. You need to reconnect the socket and solve it

Server socket + Android socket can only send a message once after connecting. The second sending does not respond. You need to reconnect the socket and solve it

2022-02-03 00:13:28 CSDN Q & A

Server use serversocket, The Android terminal uses socket Connect , You can connect to the server , But you can only send a message once , After that, the server can't receive the message ,debug Only for the first time dis.readUTF() Blocking waiting clients , If you use dis.readLine() Method prints the message for the first time , There will be no obstruction in the back , Print all the time null.
Server code

img


Android client code

img




Refer to the answer 1:

The connection with the server needs to be maintained through heartbeat ,socket Disconnection often occurs So you need to send a heartbeat After receiving the heartbeat, the back end replies to the front end , Prove that the connection is normal .




Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030013269032.html

Random recommended