【发布时间】:2020-12-16 21:22:19
【问题描述】:
我有一个网络程序,我尝试从客户端获取响应并将其发送到服务器,然后将其发送到另一个特定客户端。最后一步是问题发生的地方。我尝试通过将所有客户端添加到哈希映射并从那里获取它们来获取客户端,但我从中得到了 ConcurrentModificationException。以下是我的代码:
服务器:
public class Server {
public static void main(String[] args) {
new Server();
}
ServerSocket serverSocket = null;
static int port = 3339;
static Map<String, Socket> clients = new HashMap<String, Socket>();
private int clientCounter = 0;
public Server() {
try {
serverSocket = new ServerSocket(port);
System.out.println("[SERVER] Server successfully launched on port " + port);
} catch (IOException e) {
System.out.println("[ERROR] Unable to launch server on port " + port);
}
while(true) {
try {
Socket socket = serverSocket.accept();
clientCounter++;
ClientThread client = new ClientThread("Client " + clientCounter, socket);
System.out.println("[SERVER] New client connected: " + client.getClientName() + " (ip:" + socket.getInetAddress() + " port:"
+ socket.getPort() + " localPort:" + socket.getLocalPort() + ")");
clients.put(client.getClientName(), socket);
Thread clientThread = new Thread(client);
clientThread.start();
} catch (IOException e) {
System.out.println("[ERROR] Unable to accept request from client");
}
}
}
}
ClientThread 类(用于多线程)
public class ClientThread implements Runnable{
private String clientName;
private Socket socket;
public ClientThread(String clientName, Socket socket) {
this.socket = socket;
this.clientName = clientName;
}
@Override
public void run() {
/**
* The while(true) loop makes it possible to send multiple responses from client to server back and forth
* without it could only make 1 request because of socket.accept() method
*/
while(true) {
Server.clients.forEach((string, socket) -> {
String message = null;
if(string.equals("Client 1")) {
try {
DataInputStream inputStream1 = new DataInputStream(socket.getInputStream());
DataOutputStream outputStream1 = new DataOutputStream(socket.getOutputStream());
message = inputStream1.readUTF();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(string.equals("Client 2")) {
try {
DataInputStream inputStream2 = new DataInputStream(socket.getInputStream());
DataOutputStream outputStream2 = new DataOutputStream(socket.getOutputStream());
outputStream2.writeUTF(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
public String getClientName() {
return clientName;
}
}
客户:
public class Client {
public static void main(String[] args) {
new Client();
}
public Client() {
Socket socket = null;
try {
socket = new Socket("127.0.0.1", Server.port);
} catch (UnknownHostException e) {
System.out.println("[ERROR] Error connecting to unknown ip adress");
} catch (IOException e) {
System.out.println("[ERROR] Error connecting to server");
}
try {
while(true) {
System.out.println("You can start typing:");
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
Scanner scanner = new Scanner(System.in);
String message;
while(scanner.hasNextLine()) {
message = scanner.nextLine();
if(message.equalsIgnoreCase("quit")) {
socket.close();
break;
}
outputStream.writeUTF(message);
//reading messages from server
String received = inputStream.readUTF();
System.out.println(received);
}
}
} catch (IOException e) {
System.out.println("[ERROR] Unable to get streams from server");
}
}
}
非常感谢任何帮助。
【问题讨论】:
标签: java networking tcp connection client-server