Написал программу типа Сервер-Клиент. Запускаю сервер и первый клиент. Всё нормально работает, но стоит подключить второй клиент, первый перестает получать сообщения. Вот код:
Client.javapublic class Client extends JFrame implements Runnable{
private static boolean bconnect = true;
static private Random random = new Random();
static private ObjectOutputStream output;
static private ObjectInputStream input;
static private String inObject = null;
static private int height = 600;
static private Socket connect;
public static void main(String[] args) {
new Thread(new Client("Client")).start();
}
public Client(String name) {
//Здесь находится интерфейс программы
}
public void run() {
createConnection();
}
public static void createConnection() {
try {
while (true) {
connect = new Socket(InetAddress.getByName(Settings.getIP()), Settings.getPort());
output = new ObjectOutputStream(connect.getOutputStream());
input = new ObjectInputStream(connect.getInputStream());
if (bconnect) {
sendData("<font color=green>Новый клиент " + Settings.getName() + ":<br>подключился к серверу</font>");
bconnect = false;
}
inObject = (String)input.readObject();
if (inObject != null && inObject.equals("&&&$client_recd;") == false) {
Messages.newMessage(inObject);
last_message.setText(Messages.list());
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (HeadlessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void sendData(Object obj) {
try {
output.writeObject(Settings.getName() + ": " + obj);
output.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Server.javapublic class Server extends JFrame implements Runnable{
static private int height = 950;
static private String inObject = null;
static private ServerSocket server;
private static Socket connect;
public static void main(String[] args) {
new Thread(new Server("Server")).start();
}
public Server(String name) {
//Здесь находится интерфейс программы
}
public void run() {
try {
server = new ServerSocket(SettingsServer.getPort(), SettingsServer.getMaxUsers());
} catch (IOException e1) {
e1.printStackTrace();
}
//Создаю бесконечный цикл для подключения новых клиентов
while (true) {
try {
setConnect(server.accept());
} catch (IOException e) {
e.printStackTrace();
}
new Thread(new IncomingHandler(getConnect())).start();
}
}
public static Socket getConnect() {
return connect;
}
public static void setConnect(Socket connect) {
Server.connect = connect;
}
IncomingHandler.javapublic class IncomingHandler implements Runnable{
static private int counter = 0;
static private String inObject;
static private Socket incomingConnect;
static private ObjectOutputStream output;
static private ObjectInputStream input;
private static Socket incoming;
public IncomingHandler(Socket incoming) {
this.incoming = incoming;
}
public void run() {
incomingConnect = Server.getConnect();
try {
output = new ObjectOutputStream(incomingConnect.getOutputStream());
input = new ObjectInputStream(incomingConnect.getInputStream());
inObject = (String)input.readObject();
if (inObject != null) {
++counter;
ServerMessages.newMessage(counter + ": " + inObject);
Server.last_message.setText(ServerMessages.list());
sendData(inObject);
}
inObject = null;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void sendData(Object obj) {
try {
ServerMessages.newMessage("Message №" + counter + " resend.");
Server.last_message.setText(ServerMessages.list());
output.writeObject(obj);
output.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}