精炼服务器/客户端TCP套接字示例

时间:2015-07-03 17:42:40

标签: android sockets tcp

我有一个服务器代码示例,用于通过wifi进行服务器/客户端通信。 问题是,只要客户端应用程序重新启动或WIFI信号丢失,服务器应用程序就会关闭。

  1. 如何保持服务器应用程序的正常运行并保持新连接的能力?

  2. 如何在wifi信号丢失时保持服务器应用程序“活着”?

  3. import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.text.Html;
    import android.widget.TextView;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    
    public class MainActivity extends Activity {
    
    private ServerSocket serverSocket;
    
    Handler updateConversationHandler;
    
    Thread serverThread = null;
    
    private TextView text;
    
    public static final int SERVERPORT = 6000;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        text = (TextView) findViewById(R.id.text2);
    
        updateConversationHandler = new Handler();
    
        this.serverThread = new Thread(new ServerThread());
        this.serverThread.start();
    
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        try {
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    class ServerThread implements Runnable {
    
        public void run() {
            Socket socket = null;
            try {
                serverSocket = new ServerSocket(SERVERPORT);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    socket = serverSocket.accept();
                    CommunicationThread commThread = new CommunicationThread(socket);
                    new Thread(commThread).start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    class CommunicationThread implements Runnable {
    
        private Socket clientSocket;
        private BufferedReader input;
    
        public CommunicationThread(Socket clientSocket) {
    
            this.clientSocket = clientSocket;
    
            try {
                this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void run() {
    
            while (!Thread.currentThread().isInterrupted()) {
    
                try {
                    String read = input.readLine();
                    updateConversationHandler.post(new updateUIThread(read));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    class updateUIThread implements Runnable {
    
        private String msg;
    
        public updateUIThread(String str) {
            this.msg = str;
        }
    
        @Override
        public  void run() {
    
            text.setText(Html.fromHtml(msg + "<br>" + text.getText()));
    
    
            }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

您可以处理IOException。例如,当WiFi信号丢失时,找到返回异常的位置,并处理该异常(而不是仅打印堆栈跟踪)。