簡體   English   中英

Android客戶端/服務器應用程序-連續接收消息的正確方法

[英]Android client/server application - proper way to receive messages continously

我正在嘗試使用Android手機作為使用AsyncTask從UI發送消息的客戶端來制作客戶端/服務器應用程序。

我寫了一些非常基本的實現,只是為了測試連接和消息收發的方式,我發現了一個很大的問題。

從我的角度來看,客戶端部分似乎工作正常。 但是服務器部分是問題所在。 我無法使服務器從客戶端大量讀取和顯示消息。

我嘗試了類似while(line = (in.readLine()) != null) {}但它似乎不起作用。

從客戶端發送第一個單詞后,服務器讀取null並停止。

有人可以向我展示一種在客戶端不發送任何內容時保持服務器運行的正確方法嗎? 如果不是100%必要,我想避免使用while(true)

這是到目前為止的實現:

服務器:

public class SocketServerThread extends Thread {
private static final Logger log = Logger.getLogger(SocketServerThread.class);
private static final int SERVER_PORT_NUMBER = 5000;

@Override
public void run() {
    try {

        ServerSocket serverSocket = new ServerSocket(SERVER_PORT_NUMBER);
        serverSocket.setReuseAddress(true);
        log.info("Waiting for connection...");

        Socket clientSocket = serverSocket.accept();
        log.info("Connected! Receiving message...");

        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        try {
            while (true) {
                String line = in.readLine();
                if (line != null) {
                    log.info(line);
                }
            }
        } catch (Exception e) {
            log.error("Unexpected exception while sending / receiving messages.");
            e.printStackTrace();
        } finally {
            in.close();
            clientSocket.close();
            serverSocket.close();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

客戶:

public class MyAsyncTask extends AsyncTask<String, Integer, String> {

private static final String TAG = "MyAsyncTask";
private static final String SERVER_IP_ADDRESS = "10.0.2.2";
private static final int SERVER_PORT_NUMBER = 5000;
private PrintWriter out;

@Override
protected String doInBackground(String... params) {
    String message = "";
    try {
        InetAddress address = InetAddress.getByName(SERVER_IP_ADDRESS);
        Log.d(TAG, "Connecting...");
        Socket socket = new Socket(address, SERVER_PORT_NUMBER);
        try {
            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
            Log.d(TAG, "I/O created");
            message = params[0];
            if (!message.equals("stop")) {
                sendMessage(message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
            socket.close();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return message;
}

private void sendMessage(String message) {
    if (out != null && !out.checkError()) {
        out.println(message);
        out.flush();
        Log.d(TAG, "Sent message: " + message);
    }
}

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    Log.d(TAG, "onPostExecute(), s: " + s);
}

謝謝。

問題在於您的BufferedReader僅讀取第一個輸入流。 為了在此之后接收文本,您必須重新閱讀輸入流。 我通過在完成讀取后重新創建套接字來完成此操作,以便可以讀取下一個即將到來的數據。 我在我的應用程序中使用以下代碼。 你可以用這個

private ServerSocket serverSocket;
public static final int SERVERPORT = 5000;
Thread serverThread = null;

public void startSocketServer(){

    this.serverThread = new Thread(new ServerThread());
    this.serverThread.start();
}

public void stopSocket(){

        if(serverSocket != null){
            try{
                serverSocket.close();
            }
            catch (IOException e){
                e.printStackTrace();
            }

        }
    }

class ServerThread implements Runnable {

    public void run() {
        Socket socket = null;
        try {
            Log.wtf(TAG,"Socket: New Socket");
            serverSocket = new ServerSocket(SERVERPORT);
            if(serverSocket == null){
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        startSocketServer();
                    }
                });
                return;
            }
            while (!Thread.currentThread().isInterrupted() && !serverSocket.isClosed()) {

                try {
                    socket = serverSocket.accept();
                    Log.wtf(TAG,"Socket: Accepting");
                    CommunicationThread commThread = new CommunicationThread(socket);
                    new Thread(commThread).start();

                } catch (IOException e) {
                    Log.wtf(TAG,"Socket: Error");
                    e.printStackTrace();
                }
                if(Thread.currentThread().isInterrupted()){
                    Log.wtf(TAG, "Thread Interrupted");
                }
                if(serverSocket.isClosed()){
                    Log.wtf(TAG, "serverSocket closed");
                }
            }

        } catch (IOException e) {
            e.printStackTrace();

        }

    }
}

class CommunicationThread implements Runnable {

    private Socket clientSocket;





    public CommunicationThread(Socket clientSocket) {

        this.clientSocket = clientSocket;
        log.info("Connected! Receiving message...");
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    try {
        while (true) {
            String line = in.readLine();
            if (line != null) {
                log.info(line);
            }
            else 
              break;//This will exit the loop and refresh the socket for next data
        }
    } catch (Exception e) {
        log.error("Unexpected exception while sending / receiving messages.");
        e.printStackTrace();
    }
    finally {
        in.close();
        clientSocket.close();
    }
            refreshSocket();



        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    }

    public void refreshSocket(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                stopSocket();
                startSocketServer();
            }
        });

    }

只需調用startSocketServer()即可在代碼中啟動服務器套接字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM