Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

Android Java Socket, socket client, multi-threaded, how to close when exiting

I am making an application that connects to a server, I create three threads, one for the socket, another to get what is received and another to send.

I downloaded part of the code from the page: https://www.tutorialspoint.com/how-to-send-data-through-wifi-in-android-programmatically I want to close the threads and the socket open when exiting How can I do to close them correctly when exiting?

How can I do to close them correctly when exiting.

Client code

package com.client.myapplication.client;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

@SuppressLint("SetTextI18n")
public class MainActivity extends AppCompatActivity {
   Thread Thread1 = null;
   EditText etIP, etPort;
   TextView tvMessages;
   EditText etMessage;
   Button btnSend;
   String SERVER_IP;
   int SERVER_PORT;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      etIP = findViewById(R.id.etIP);
      etPort = findViewById(R.id.etPort);
      tvMessages = findViewById(R.id.tvMessages);
      etMessage = findViewById(R.id.etMessage);
      btnSend = findViewById(R.id.btnSend);
      Button btnConnect = findViewById(R.id.btnConnect);
      btnConnect.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            tvMessages.setText("");
            SERVER_IP = etIP.getText().toString().trim();
            SERVER_PORT = Integer.parseInt(etPort.getText().toString().trim());
            Thread1 = new Thread(new Thread1());
            Thread1.start();
         }
      });
      btnSend.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            String message = etMessage.getText().toString().trim();
            if (!message.isEmpty()) {
               new Thread(new Thread3(message)).start();
            }
         }
      });
   }
   private PrintWriter output;
   private BufferedReader input;
   class Thread1 implements Runnable {
      @Override
      public void run() {
         Socket socket;
         try {
            socket = new Socket(SERVER_IP, SERVER_PORT);
            output = new PrintWriter(socket.getOutputStream());
            input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            runOnUiThread(new Runnable() {
               @Override
               public void run() {
                  tvMessages.setText("Connected
");
               }
            });
            new Thread(new Thread2()).start();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
   class Thread2 implements Runnable {
      @Override
      public void run() {
         while (true) {
            try {
               final String message = input.readLine();
               if (message ! = null) {
                  runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                        tvMessages.append("server: " + message + "
");
                     }
                  });
               } else {
                  Thread1 = new Thread(new Thread1());
                  Thread1.start();
                  return;
               }
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
   }
   class Thread3 implements Runnable {
      private String message;
      Thread3(String message) {
         this.message = message;
      }
      @Override
      public void run() {
         output.write(message);
         output.flush();
         runOnUiThread(new Runnable() {
            @Override
            public void run() {
               tvMessages.append("client: " + message + "
");
               etMessage.setText("");
            }
         });
      }
   }
}
question from:https://stackoverflow.com/questions/65894250/android-java-socket-socket-client-multi-threaded-how-to-close-when-exiting

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...