我有一个Client
类和一个MessageServer
类,它们通过Socket
相互通信。并且它们彼此发送Message
对象(尚未实现)。
问题是,在我的MesageServer类中,我有这样的实现
LinkedList<Client> clients = new LinkedList<>();
LinkedList<ObjectOutputStream> outs = new LinkedList<>();
LinkedList<ObjectInputStream> ins = new LinkedList<>();
但是这种方法似乎很奇怪,而且我考虑得越多,它就没有用。
我想从1个输出流中获取Message
个对象,因为那样,我不必遍历每个OOS
对象来找出哪个客户端实际上在该流中放置了一个对象。
我错了吗?我应该怎么办?
MessageServer
public class MessageServer extends Server implements Runnable{
static LinkedList<Message> messages = new LinkedList<>();
LinkedList<Client> clients = new LinkedList<>();
LinkedList<ObjectOutputStream> outs = new LinkedList<>();
LinkedList<ObjectInputStream> ins = new LinkedList<>();
ExecutorService threads = Executors.newFixedThreadPool(3);
ExecutorService clientThreads = Executors.newCachedThreadPool();
public MessageServer() throws IOException {
super(Vars.MESSAGE_PORT);
}
//Accept the connetions to the server
private class ServerConnection implements Runnable{
@Override
public void run(){
while (true) {
try {
Socket client = serverSocket.accept();
//Read Client data then add to the list
ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
ObjectInputStream in = new ObjectInputStream(client.getInputStream());
outs.add(out);
ins.add(in);
Client current = (Client) in.readObject();
if(current.getType()) {
Provider p = (Provider) current;
clients.add(p);
}else{
Receiver r = (Receiver) current;
clients.add(r);
}
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(MessageServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
private class ServerIO implements Runnable{
@Override
public void run(){
}
}
public void addToMessages(Message message) {
if (!messages.contains(message)) {
messages.add(message);
}
}
@Override
public void run() {
threads.execute(new ServerConnection());
threads.execute(new ServerIO());
}
}
Provider
和Receiver
类是Client
类的子类,它们存在于将来的视频流中,并且现在的行为方式相同。
消息
public class Message implements Serializable {
private final int maxChar = 250;
private String content;
private String timeStamp;
private Client sender;
public Message(Client sender, String content, String timeStamp) {
this.sender = sender;
this.content = content;
this.timeStamp = timeStamp;
}
}
客户
public class Client implements Serializable {
protected String nickname;
protected long id;
protected int key;
/*
* True -> Provider
* False -> Receiver
*/
protected boolean type;
// MessageServer
protected transient ObjectOutputStream msgOut;
protected transient ObjectInputStream msgIn;
protected transient Socket messageSocket;
protected transient Socket videoSocket;
public Client(String nickname, boolean type){
this.type = type;
this.nickname = nickname;
createid();
makeConnection();
key = (int) (Math.random() * 8999) + 1000;
}
void makeConnection(){
try {
messageSocket = new Socket(Vars.IP, Vars.MESSAGE_PORT);
//TODO make connection with videoServer
msgOut = new ObjectOutputStream(messageSocket.getOutputStream());
msgIn = new ObjectInputStream(messageSocket.getInputStream());
msgOut.writeObject(this);
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}