シンプルなJavaソケットクライアントに問題があります。接続が正しく確立され、サーバーとクライアントの両方がデータを送受信します。クライアントが数秒間接続を失うと、ソケットは終了して再確立されます。これは正常に完了しましたが、その時点で、クライアントが最後に受信したメッセージ(再接続前)は何の理由もなく再び受信されます(同じメッセージの2回の受信)。ソケットの再接続時にメッセージが重複して受信される
私のコードは次のとおりです。
ソケットを開始するには、次の接続が失われると
public class IncomingReader implements Runnable {
String message = "";
@Override
public void run() {
Thread thisThread = Thread.currentThread();
while (readerThread == thisThread) {
try {
while ((message = reader.readLine()) != null) {
manageServerMessage(message);
}
} catch (IOException e) {
Log.d(General.TAG, e.getMessage()+" "+e.getCause());
}
}
:データを読み込み
Socket sock = new Socket();
sock.bind(null);
InetSocketAddress isa = new InetSocketAddress(serverIPAddress,serverPort);
sock.connect(isa, conectionTimeOut);
sock.setKeepAlive(true);
InputStreamReader isR = new InputStreamReader(sock.getInputStream(), "UTF8");
reader = new BufferedReader(isR);
out = new BufferedWriter(new OutputStreamWriter(
sock.getOutputStream(), "UTF8"));
public void stopSocket() {
readerThread = null;
try{
if (reader != null){
reader.close();
reader=null;
}
} catch (IOException e) {
Log.d(General.TAG, e.getMessage()+" "+e.getCause());
}
try{
if (out != null)
out.close();
} catch (IOException e) {
Log.d(General.TAG, e.getMessage()+" "+e.getCause());
}
try{
if (sock != null){
sock.close();
sock=null;
}
} catch (IOException e) {
Log.d(General.TAG, e.getMessage()+" "+e.getCause());
sock=null;
}
readerThread = null;
}
あなたの質問に答えられた場合、またはそれがもはや有効でない場合は、誰もが問題が解決されたことを知るように、最も適切な回答を選択してください。ありがとう。 – wattostudios