简体   繁体   中英

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

I'm trying to make a client/server application using an Android phone as a client using AsyncTask to send messages from UI.

I've written some very basic implementation just to test the connection and the way that messages are received / sent and I found a very big problem.

The client part seems to work fine..from my perspective. But the server part is the problem. I can't make the server reading and displaying messages countinously from the client.

I tried something like while(line = (in.readLine()) != null) {} but it doesn't seems to work.

After I sent my first word from the client, the server reads null and it stops.

Can someone show me a proper way to keep the server running while the client is not sending nothing? I'd like to avoid using while(true) if it's not 100% necessary.

Here is the implementation until now:

Server:

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();
    }
}

Client:

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);
}

Thank you.

The problem is that your BufferedReader only read the first input stream. In order to receive the text after that, you have to re-read the input stream. I do it by recreating the socket when I am done reading, so that I can read next coming data. I am using the following code in my app. You can use this

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();
            }
        });

    }

Just call startSocketServer() to start the server socket in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM