java - 聊天服务器。线程问题

标签 java sockets chat

嗨,有人可以帮助我处理这段代码吗,因为我正在尝试使用 If 语句,但没有工作。我并不是要求为我编写代码,只是向我指出一些东西。

主要问题是,每次有人连接时,都会创建新线程,但是当他关闭聊天框时,线程会保持打开状态,并且无法正常工作。我的意思是有些人自己建立了 10 个联系,其他人都无法聊天。

import java.net.*;
import java.io.*;

public class myServer {

    static ServerSocket server;
    static Socket client;
    static DataInputStream in;
    static DataOutputStream out;
    static clientThread t[] = new clientThread[10];

    public static void main(String[] args) throws IOException {

        System.out.println("Starting Server");
        server = new ServerSocket(7555);
        System.out.println("Started Server");

        while (true) {

            client = server.accept();
            System.out.println("CONNECTION");
            out = new DataOutputStream(client.getOutputStream());
            out.writeUTF("Welcome to the chat room");
            for (int i = 0; i <= 9; i++) {
                if (t[i] == null) {
                    (t[i] = new clientThread(client, t)).start();
                    break;
                }
            }
        }

    }

}

class clientThread extends Thread {

    DataInputStream in;
    DataOutputStream out;
    static String msg;
    Socket client = null;
    clientThread t[];

    public clientThread(Socket client, clientThread[] t) {
        this.client = client;
        this.t = t;
    }

    public void run() {

        try {
            in = new DataInputStream(client.getInputStream());
            out = new DataOutputStream(client.getOutputStream());
            boolean tru = true;

            while (tru) {
                msg = in.readUTF();
                System.out.println(msg);
                for (int i = 0; i <= 9; i++)
                    if (t[i] != null) {
                        t[i].out.writeUTF(msg);
                        System.out.println(t[i]);
                    }
            }
        } catch (IOException e) {
        }
    }
}

最佳答案

您的问题不是线程保持打开状态,而是您没有将客户端线程标记为已完成的机制。即使线程已经退出,t[i]也不会变成null。它仍然会引用线程的实例——只是一个“死”线程。

这里有两种解决方法:

  1. 在线程退出之前,标记 t[i] = null(其中 i 是当前线程的索引)。请注意,您需要在每个线程中存储 i 的值。

    1. 修改clientThread并添加private int threadIndex;作为成员变量。
    2. 修改clientThread的构造函数并添加threadIndex作为参数。

      public clientThread(Socket client, clientThread[] t, int threadIndex) 
      {
          this.threadIndex=threadIndex;
          //...
      }
      
    3. run 的右大括号之前添加

      synchronized(t){t[this.threadIndex]=null;}
      
  2. 使用Executor并向其提交您的clientThread。 Java 的Executor将为您清理线程。

关于java - 聊天服务器。线程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8670928/

相关文章:

java - 以编程方式进行 Dialogflow 身份验证 java

java - 如何在 google maps API v2 中添加叠加层 |安卓|

java - 从最短单词到最长单词输出一个句子

html - 在 CSS 和 HTML 中为聊天室创建布局

javascript - 如何使用 Socket.io 和 Node.js 开发大型聊天应用程序

c#,尝试发送消息,如果离线自动休眠,重新连接,重新发送

java - Spring AOP错误: java. lang.NoClassDefFoundError:org/springframework/cglib/core/SpringNamingPolicy

c - 使用 C 在 TCP 套接字中人工延迟,用于竞争条件利用

python - 将计算与 Python 中的套接字工作分开

c - 为什么我在 send() 函数中丢失了这个字节?