当前位置: 首页 > 知识库问答 >
问题:

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

沃阳飙
2023-03-14

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

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

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

下面是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;
        }
    }
}
}

共有1个答案

岳正浩
2023-03-14

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

 类似资料:
  • 服务器: 客户: 但最终,客户端阻塞了read(buffer)语句。

  • 使用依赖关系spring-cloud-starter-zipkin,应用程序应该在sleuth触发时连接到zipkin服务器。我没有启动zipkin服务器,所以它应该抛出一个连接异常。但什么也没发生。而当我启动zipkin服务器时,它不能接收任何东西。 应用程序.属性 和日志

  • 我开发了一个客户端-服务器UDP应用程序。服务器UDP套接字设置为广播UDP套接字。双方的代码不会产生任何错误,但客户端未接收到从广播UDP服务器端发送的消息。请看一下我的代码,我知道有一些错误我想不通。我真的需要帮助: 服务器: 客户: }

  • 我们收到了这段代码,应该对其进行修改,以便无论何时客户端连接到服务器并发送消息,服务器都应该回复我听到了,伙计。这适用于一个或多个客户机,但下一个任务是,当新客户机连接时,服务器应该告诉所有其他已连接的客户机。 我以为这会很容易,但结果并不像我想象的那样。由于服务器总是得到sock 3,第一个客户端得到4,第二个客户端得到5等等,所以我尝试创建一个for循环,每当一个新客户端连接时,该循环就会向4

  • 我有一个Python服务器使用unix数据报套接字连接与一个C客户端通信。下面的代码设置一个套接字,然后从客户端发送和接收一条消息。这个脚本在python 2.7中工作,但是,当在python 3中测试它时,对recv()的调用会超时等待来自客户端的消息。然而,客户端确实从服务器接收消息而没有问题。我已经用3.5.2和3.7.1在两台不同的机器上测试过了,结果相同。 更新:我添加了一个ioloop

  • 我目前有一个在非阻塞模式下使用 NIO java 库编写的桌面服务器。您可以在此处找到完整的服务器项目。我还创建了一个用于测试目的的非阻塞 NIO 客户端。你可以在这里找到这个项目。最后,服务器应该用于Android即时通讯应用程序。我使客户端和服务器都基于发送“数据包”进行通信的想法。我的意思是,Packet 类的引用被打包到字节缓冲区中并通过套接字通道发送。然后,它们在另一端反序列化并执行。