gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:25:31 25 4
gpt4 key购买 nike

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

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

我尝试通过 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

这是服务器类的完整代码

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

这是客户端类的完整代码

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

最佳答案

您的代码存在一些问题,无法正常工作。

  • 正如在其他答案中所述,在您的代码中,您在向客户端发送消息后立即关闭套接字输出流。仅在 for 消息循环之外调用 close()。当然在客户端关闭socket和在服务器端关闭是一样的。只有当客户端和服务器完成对话时,您才必须关闭套接字。在传输数据时关闭它就像在谈话中挂断电话。

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

    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 的输出或日​​志跟踪。

关于java - 为什么我的客户端线程/监听器没有收到来自服务器的广播消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20942821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com