Поток на сервере остановится, и я понятия не имею, почемуJAVA

Программисты JAVA общаются здесь
Anonymous
Поток на сервере остановится, и я понятия не имею, почему

Сообщение Anonymous »

i am making a client handler for my chat app so I can have more then one client connected but right it just 2 connections and the 2 connections run on 2 threads(1 client per thread) now everything work fine and the server will get info from both client but after a while (1 sec) which every client will talk with the last will stay connected and the server stops the other thread running the other connection and I have no idea why
Server code

Код: Выделить всё

import java.io.IOException;
import java.net.ServerSocket;

public class server{

public static ServerSocket serverSocket;
public static void main(String[] args)throws IOException{
serverSocket = new ServerSocket(1234);
clientHandler clientHandler = new clientHandler();
Thread clienThread = new Thread(clientHandler);
Thread clienThread2 = new Thread(clientHandler);
clienThread.start();
clienThread2.start();
}
}
< /code>
Код обработчика клиента < /p>
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class clientHandler implements Runnable{
public static server server = new server();
public static Socket socket;
public static BufferedReader in;
public static PrintWriter out;

@Override
public void run(){
try {
socket = server.serverSocket.accept();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
while(true){
String msg = in.readLine();
System.out.println(msg);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Я попытался использовать 2 сервера на 2 разных порта, но получил одинаковую проблему, поэтому, пожалуйста, помогите

Подробнее здесь: https://stackoverflow.com/questions/795 ... o-idea-why

Вернуться в «JAVA»