我无法为我制作的游戏制作多客户端服务器。我能够创建一个ServerSocket,建立连接并通过ObjectInputStream和ObjectOutputStream发送数据。我的问题在于涉及多个客户端,据我所知,需要为每个客户端创建一个新线程,但我希望发送和接收的数据被硬编码到客户端类中。我如何允许我的客户发送可变数据?
这是我的客户端类:
public class Client extends Thread {
private Server server = new Server();
private Socket connection;
private ObjectOutputStream out;
private ObjectInputStream in;
public Client(Socket connection) {
this.connection = connection;
this.out = server.objOutStream(this.connection);
this.in = server.objInStream(this.connection);
}
public void run() {
while(this.connection.isConnected()) {
//
List<String> list = new ArrayList<String>();
list.add("hello there");
server.sendData(out, list);
//
List<String> recive = new ArrayList<String>();
recive = server.reveiveData(in);
}
}
public Socket getConnection() {
return connection;
}
}
以下是服务器端:
public ServerSocket createServer(Integer serverPort, Integer serverSize) {
ServerSocket server = null;
try {
server = new ServerSocket(serverPort, serverSize);
System.out.println("created server");
} catch (IOException e) {
e.printStackTrace();
}
return server;
}
public Socket makeConnection(ServerSocket server) {
Socket connection = null;
try {
connection = server.accept();
Client client = new Client(connection);
client.start();
System.out.println("connection made with " + connection.getInetAddress().getHostAddress());
} catch (IOException e) {
e.printStackTrace();
}
return connection;
}
public ObjectInputStream objInStream(Socket connection) {
ObjectInputStream in = null;
try {
in = new ObjectInputStream(connection.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
return in;
}
public ObjectOutputStream objOutStream(Socket connection) {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(connection.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
return out;
}
public void sendData(ObjectOutputStream out, List<String> list) {
try {
out.writeObject(list);
out.flush();
System.out.println("sending " + list);
} catch (IOException e) {
e.printStackTrace();
}
}
public List<String> reveiveData(ObjectInputStream in) {
List<String> list = null;
try {
list = (List<String>) in.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return list;
}
public Socket joinServer(String serverIP, Integer serverPort) {
Socket connection = null;
try {
connection = new Socket(InetAddress.getByName(serverIP), serverPort); System.out.println("joined");
} catch (IOException e) {
e.printStackTrace();
}
return connection;
}
public void stop(Socket connection) {
System.out.println("closing");
try {
if(connection.isInputShutdown() == false) {
connection.getInputStream().close();
}
if(connection.isOutputShutdown() == false) {
connection.getOutputStream().close();
}
if(connection.isConnected() == true) {
connection.close();
}
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}
在我的mian课程中我运行:
try {
if(connection == null) {
connection = server.makeConnection(serverSocket);
in = server.objInStream(connection);
out = server.objOutStream(connection);
}else {
//readlist
List<String> readList = (List<String>) in.readObject();
System.out.println("reciving: " + readList);
//writelist
List<String> writeList = new ArrayList<String>();
writeList.add("hi");
out.writeObject(writeList);
System.out.println("sending: " + writeList);
}
}catch(IOException | ClassNotFoundException e) {
e.printStackTrace();
}