안녕하세요 누군가 내가이 문장으로 노력하고 있었기 때문에이 코드로 나를 도울 수는 있었지만 작동하지 않습니다. 나는 나를 위해 코드를 써달라고 요청하지 않고있다.채팅 서버. 스레드 문제
주요 문제는 누군가가 연결될 때마다 새 스레드가 만들어 지지만 채팅 상자를 끄면 트레드가 계속 켜져 있어야하며 작동하지 않아야한다는 것입니다. 내 말은 어떤 몸이 직접 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) {
}
}
}