我无法在我正在编写的一个简单的 UDP 聊天客户端上运行并发,这一切都运行良好,直到我尝试将新联系人添加到存储在 Peer 中的传出消息列表中,它会引发 CurrentModificationException,有人可以帮助我了解在哪里我走错了吗?
这是我的课
import java.net.*;
import java.util.*;
import java.io.*;
public class Chatter {
public static class ReceiveMess extends Thread{
DatagramSocket ds;
public ReceiveMess(DatagramSocket s){
ds = s;
}
byte[] Rbuf = new byte[1000];
DatagramPacket Rdgp = new DatagramPacket(Rbuf, Rbuf.length);
public synchronized void run() {
try{
while (true){
for(Peer p : Peer.PeerList){
ds.receive(Rdgp);
String rcvd = new String(Rdgp.getData(), 0, Rdgp.getLength()) + ", from address: "
+ Rdgp.getAddress() + ", port: " + Rdgp.getPort();
System.out.println(rcvd);
}
}
}
catch(IOException e) {
System.out.println(e);
}
}
}
public static class SendMess extends Thread{
DatagramSocket ds;
public SendMess(DatagramSocket s){
ds = s;
}
int SPORT = 40080;
byte[] Sbuf = new byte[1000];
DatagramPacket Sdgp = new DatagramPacket(Sbuf, Sbuf.length);
public synchronized void run() {
try{
while (true) {
BufferedReader consR = new BufferedReader(new InputStreamReader(System.in));
String MessOut = consR.readLine();
if(MessOut.startsWith("/NEW")){
try{
String[] splitArray = MessOut.split(" ");
String newIP = (splitArray[1]);
Peer p = new Peer(newIP);
System.out.println(newIP + " added to the contacts list");
continue;
}
catch(Exception e){
System.out.println("Please format NEW IP address's as NEW XXX.XXX.XXX.XXX");
continue;
}
}
else{
Sbuf = ("Server Said: " + MessOut).getBytes();
for(Peer p : Peer.PeerList){
DatagramPacket out = new DatagramPacket(Sbuf, Sbuf.length, p.IP, SPORT);
ds.send(out);}
}
}
}
catch(IOException e) {
System.out.println(e);
}
}
}
public static void main(String [] args){
try{
for(String s : args){
String address = s;
Peer peer = new Peer(address);
}
int PORT = 40080;
DatagramSocket ds = new DatagramSocket(PORT);
Peer.PrintList();
SendMess sendmess = new SendMess(ds);
sendmess.start();
ReceiveMess receivemess = new ReceiveMess(ds);
receivemess.start();
}
catch(Exception e){
System.out.println(e);
}
}
}
And my peer class,
import java.net.*;
import java.util.*;
public class Peer{
InetAddress IP;
static List<Peer> PeerList = new LinkedList<Peer>();
Peer(String clientAddress){
try{
IP = IP.getByName(clientAddress);
AddToList(this);
}
catch(UnknownHostException e){
System.out.println(e.toString());
}
}
public synchronized void AddToList(Peer peer){
PeerList.add(this);
}
public List<Peer> GetList(){
return PeerList;
}
public static void PrintList(){
for(Peer p : PeerList){
System.out.println(p.IP.toString());
}
}
}