Dobrý den, prosím vás o pomoc s programem ,u kterýho si nevím rady s modifikací. Když k TCP serveru připojím jednoho clienta vše funguje normálně když jich připojím vícero začne to zlobit a vypisovat různé chyby typu CurrentModificationException(v debugu) a program dále nefunguje, proto prosím o radu jak by to šlo spravně synchronizovat děkuji.
Server
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
public class Server {
private ServerSocket ssc;
private String temp = "";
private ArrayList<BufferedReader> read = new ArrayList<BufferedReader>();
private ArrayList<BufferedWriter> write= new ArrayList<BufferedWriter>();
public Server(String port) {
try {
ssc = new ServerSocket(Integer.parseInt(port));
Thread writer = new Thread(new Runnable() {
synchronized public void run() {
while(true) {
cyrcle();
}
}
});
Thread clients = new Thread(new Runnable() {
synchronized public void run() {
while(true) {
try {
Socket sc = ssc.accept();
newClient(sc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
clients.start();
writer.start();
System.out.println("Server was started on port: " + port);
while(true) {
System.gc();
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private synchronized void cyrcle() {
for(BufferedReader bfr : read ) {
try {
String temp = bfr.readLine();
System.out.println(temp);
for(BufferedWriter bfw: write) {
bfw.write(temp);
bfw.newLine();
bfw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private synchronized void newClient(Socket sc) {
try {
read.add(new BufferedReader(new InputStreamReader(sc.getInputStream())));
write.add(new BufferedWriter(new OutputStreamWriter(sc.getOutputStream())));
System.out.println("Client " + sc.getInetAddress().getHostAddress() + " was connected succesfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Server("25565");
}
}
Client
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
public Client(String hostname,String port,String name) {
try {
Socket sc = new Socket(hostname,Integer.valueOf(port));
Scanner sca = new Scanner(System.in);
final BufferedReader in = new BufferedReader(new InputStreamReader(sc.getInputStream()));
final BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sc.getOutputStream()));
new Thread() {
public void run() {
while(true) {
try {
String get = in.readLine();
System.out.println("\n" + get);
}catch(IOException e) {
}
}
}
}.start();
while(true) {
String send = sca.nextLine();
out.write(name +": " + send + "\r\n");
out.flush();
try{
Thread.sleep(1);
} catch(InterruptedException a) {a.printStackTrace();}
}
} catch(IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//new Client(args[0],args[1],args[2]);
new Client("127.0.0.1", "25565", "Name");
}
}