为什么我的客户端线程/侦听器没有从服务器接收广播消息?

时间:2014-01-06 04:08:19

标签: java android client-server port

我在Android上制作了一个客户端 - 服务器应用程序的简单原型

我设法将两个客户端连接到服务器,服务器可以接收它们的消息。现在的问题是我似乎无法向其他客户广播/接收消息。

我尝试通过Server类中的for循环广播收到的消息:

private void broadcastMessage(String message) {

        for (int i = 0, j = clients.size(); i <= j; i++) {
            PrintWriter out = null;
            Socket socket = clients.get(i);
            try {
                out = new PrintWriter(new BufferedWriter(
                        new OutputStreamWriter(socket.getOutputStream())), true);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // WHERE YOU ISSUE THE COMMANDS
            out.println(message);
            Log.d("SERVER Loop", "Broadcasting messages...");
            out.close();
        }
        Log.d("SERVER", "Message Brodcasted");
    }

然后,我尝试通过Client类中的侦听器接收:

    public class ClientThreadListener implements Runnable {

    protected Socket serverSocket = null;
    protected String mMsgFromServer;

    public ClientThreadListener(Socket serverSocket) {
        this.serverSocket = serverSocket;
    }

    public void run() {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    serverSocket.getInputStream()));

            while ((mMsgFromServer = in.readLine()) != null) {
                Log.d("MESSAGE FROM SERVER: ", mMsgFromServer);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        msgFromOtherClients.append('\n'
                                + "Message From Server: " + mMsgFromServer);
                    }
                });
            }
        } catch (Exception e) {
            Log.e("ClientListener", "C: Error", e);
            connected = false;
        }
    }
}

我没有收到任何错误或强行关闭。原谅我,我知道它非常混乱,但请耐心等待,请专注于手头的问题:D

以下是Server类的完整代码

public class Server extends Activity {

private TextView serverStatus;

// DEFAULT IP
public static String SERVERIP = "10.0.2.15";

// DESIGNATE A PORT
public static final int SERVERPORT = 8080;

private Handler handler = new Handler();

private ServerSocket serverSocket;

private String mMsgFromClient;

private MultiThreadedServer server;

private ArrayList<Socket> clients = new ArrayList<Socket>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.server);
    serverStatus = (TextView) findViewById(R.id.server_status);

    // SERVERIP = getLocalIpAddress();

    server = new MultiThreadedServer(8080);
    new Thread(server).start();

}

public class MultiThreadedServer implements Runnable {

    protected int serverPort = 8080;
    protected ServerSocket serverSocket = null;
    protected boolean isStopped = false;
    protected Thread runningThread = null;

    public MultiThreadedServer(int port) {
        this.serverPort = port;
    }

    public void run() {
        synchronized (this) {
            this.runningThread = Thread.currentThread();
        }
        openServerSocket();
        while (!isStopped()) {
            Socket clientSocket = null;
            try {
                clientSocket = this.serverSocket.accept();
                clients.add(clientSocket);
            } catch (IOException e) {
                if (isStopped()) {
                    Log.d("SERVER TEXT", "Server Stopped.");
                    return;
                }
                throw new RuntimeException(
                        "Error accepting client connection", e);
            }
            new Thread(new WorkerRunnable(clientSocket, this)).start();

        }
        Log.d("SERVER TEXT", "Server Stopped.");
    }

    private synchronized boolean isStopped() {
        return this.isStopped;
    }

    public synchronized void stop() {
        this.isStopped = true;
        try {
            this.serverSocket.close();
        } catch (IOException e) {
            throw new RuntimeException("Error closing server", e);
        }
    }

    private void openServerSocket() {
        try {
            this.serverSocket = new ServerSocket(this.serverPort);
        } catch (IOException e) {
            throw new RuntimeException("Cannot open port 8080", e);
        }
    }

    private void broadcastMessage(String message) {

        for (int i = 0, j = clients.size(); i <= j; i++) {
            PrintWriter out = null;
            Socket socket = clients.get(i);
            try {
                out = new PrintWriter(new BufferedWriter(
                        new OutputStreamWriter(socket.getOutputStream())), true);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // WHERE YOU ISSUE THE COMMANDS
            out.println(message);
            Log.d("SERVER Loop", "Broadcasting messages...");
            out.close();
        }
        Log.d("SERVER", "Message Brodcasted");
    }

}

public class WorkerRunnable implements Runnable {

    protected Socket clientSocket = null;
    protected String mMsgFromClient = null;

    private UUID id;

    public WorkerRunnable(Socket clientSocket, MultiThreadedServer server) {
        this.clientSocket = clientSocket;
        id = UUID.randomUUID();
    }

    public void run() {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    clientSocket.getInputStream()));

            while ((mMsgFromClient = in.readLine()) != null) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        serverStatus.append('\n'
                                + "Message From Client ID " + getID()
                                + ": " + mMsgFromClient);
                    }
                });
            }
            Log.d("SERVERTEXT", "Proceed to broadcast");
            server.broadcastMessage(mMsgFromClient);

        } catch (IOException e) {
            Handler handler = new Handler();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    serverStatus
                            .append('\n'
                                    + "Message From Client ID "
                                    + getID()
                                    + ": "
                                    + "Oops. Connection interrupted. Please reconnect your phones.");
                }
            });
            e.printStackTrace();
        }

    }

    private String getID() {
        return id.toString();
    }
}
}

以下是Client类的完整代码

public class Client extends Activity {

private EditText serverIp;
private EditText chatMsg;
private Button connectPhones;
private Button sendMsg;
private TextView msgFromOtherClients;

private String serverIpAddress = "";

private boolean connected = false;
private boolean willSendMsg = false;

private Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.client);

    serverIp = (EditText) findViewById(R.id.server_ip);
    connectPhones = (Button) findViewById(R.id.connect_phones);
    connectPhones.setOnClickListener(connectListener);

    chatMsg = (EditText) findViewById(R.id.chat_msg);
    sendMsg = (Button) findViewById(R.id.send_msg);
    sendMsg.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            willSendMsg = true;
        }
    });

    msgFromOtherClients = (TextView) findViewById(R.id.msg_from_other_clients);
}

private OnClickListener connectListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        if (!connected) {
            serverIpAddress = serverIp.getText().toString();
            if (!serverIpAddress.equals("")) {
                Thread cThread = new Thread(new ClientThread());
                cThread.start();
            }
        }
    }
};

public class ClientThread implements Runnable {

    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);

            Log.d("ClientActivity", "C: Connecting...");

            Socket socket = new Socket(serverAddr, Server.SERVERPORT);
            connected = true;

            Thread listener = new Thread(new ClientThreadListener(new Socket(serverAddr, Server.SERVERPORT)));
            listener.start();

            while (connected) {
                if (willSendMsg) {
                    willSendMsg = false;
                    try {
                        Log.d("ClientActivity", "C: Sending command.");
                        PrintWriter out = new PrintWriter(
                                new BufferedWriter(new OutputStreamWriter(
                                        socket.getOutputStream())), true);
                        // WHERE YOU ISSUE THE COMMANDS
                        out.println(chatMsg.getText().toString());
                        Log.d("ClientActivity", "C: Sent.");
                    } catch (Exception e) {
                        Log.e("ClientActivity", "S: Error", e);
                    }
                }
            }
            socket.close();
            Log.d("ClientActivity", "C: Closed.");
        } catch (Exception e) {
            Log.e("ClientActivity", "C: Error", e);
            connected = false;
        }
    }
}

public class ClientThreadListener implements Runnable {

    protected Socket serverSocket = null;
    protected String mMsgFromServer;

    public ClientThreadListener(Socket serverSocket) {
        this.serverSocket = serverSocket;
    }

    public void run() {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    serverSocket.getInputStream()));

            while ((mMsgFromServer = in.readLine()) != null) {
                Log.d("MESSAGE FROM SERVER: ", mMsgFromServer);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        msgFromOtherClients.append('\n'
                                + "Message From Server: " + mMsgFromServer);
                    }
                });
            }
        } catch (Exception e) {
            Log.e("ClientListener", "C: Error", e);
            connected = false;
        }
    }
}
}

3 个答案:

答案 0 :(得分:2)

您的代码存在一些阻止其工作的问题。

  • 正如在其他答案中已经说过的那样,在您的代码中,您在将消息发送到客户端后立即关闭套接字输出流。仅在close()消息循环中调用for。当然,关闭客户端中的套接字与在服务器上关闭套接字具有相同的效果。只有在客户端和服务器完成通话后才能关闭套接字。在传输数据时关闭它就像在谈话过程中挂断电话一样。

  • 其次,在客户端创建一个新套接字:

    Log.d("ClientActivity", "C: Connecting...");
    Socket socket = new Socket(serverAddr, Server.SERVERPORT);
    

    然后你传递给另一个新创建的套接字侦听器(我想这不是预期的):

    connected = true;
    Thread listener = new Thread(new ClientThreadListener(new Socket(serverAddr, Server.SERVERPORT)));
    listener.start();
    
  • 第三,在发送数据后立即在输出流上调用flush(),否则可能不会发送数据(发送方法只会将数据排入发送缓冲区)。

  • 最后(这可能对你没用,因为我不知道你的最终目标),如果你需要在套接字上发送和接收,90%的时间它会更好,更容易地同时执行此操作,使用单独的线程进行收听和发送。

如果它仍然不起作用,请在此处添加一些来自logcat的输出或日​​志跟踪。

答案 1 :(得分:2)

您需要移动该行:

server.broadcastMessage(mMsgFromClient);

while

        while ((mMsgFromClient = in.readLine()) != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    serverStatus.append('\n'
                            + "Message From Client ID " + getID()
                            + ": " + mMsgFromClient);
                }
            });
            // HERE
            Log.d("SERVERTEXT", "Proceed to broadcast");
            server.broadcastMessage(mMsgFromClient);
        }

否则,您只会广播null

编辑:您应该确保在发布新mMsgFromClient和实际执行之间未更改Runnable。最好的方法是使用当前值初始化匿名类中的字段,然后记录该字段的值。

EDIT2:除非您的服务器在发送广播消息后应该关闭与客户端的连接,否则您应该在out.flush()方法中使用out.close()而不是broadcastMessage。最好是在超时后关闭客户端连接,或者让客户端断开连接,再次超时。

否则,大多数时候您的测试将非常有限:客户端连接并发送消息;然后它收到自己的消息,服务器关闭连接。

答案 2 :(得分:0)

请尝试在android中使用AsyncTask,它将创建与服务器通信的单独线程。