Skip to content

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
  • Loading branch information
loveofguoke committed Dec 21, 2022
1 parent b3bb1fd commit 2e3f826
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
clean:
${MAKE} -C src clean
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# ChatServer
A project for JAD22fall

### Requirements

实现一个多客户端的纯文本聊天服务器,能同时接受多个客户端的连接,并将任意一个客户端发送的文本向所有客户端(包括发送方)转发。

### How to Run

VSCode

在终端运行 jar 文件: java -jar ./ChatServer.jar

### Progress
2 changes: 2 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
clean:
del *.class
17 changes: 8 additions & 9 deletions src/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface ClientListener {
public class Server implements ClientListener, Runnable {
public static final int PORT = 3936;
public static final int SUBSELNUM = 3;
private static Server theServer = new Server();
LinkedList<Client> lstClient = new LinkedList<Client>();
private Queue<String> msgQueue = new ArrayDeque<String>();
private ExecutorService executer = Executors.newCachedThreadPool();
Expand All @@ -44,28 +45,24 @@ private Server() { // MainReactor
e.printStackTrace();
}
}
private static Server theServer = new Server();

protected static Server getServer() {
return theServer;
}
synchronized void sendMsg(String msg) {
// for (Client c : lstClient) {
// c.sendMsg(msg);
// }
msgQueue.add(msg);
this.notify();
}

@Override
public void msgRcvd(String msg) {
sendMsg(msg);
sendMsg(msg);
}

@Override
public void clientQuit(Client c) {
lstClient.remove(c);
// c.stopRx();
synchronized public void clientQuit(Client c) {
lstClient.remove(c);
// c.stopRx();
}

void go() throws IOException {
Expand Down Expand Up @@ -129,7 +126,9 @@ public void run() {
Client c = new Client(subSelectors[next], sc);
if (++next == subSelectors.length) next = 0;
c.addMsgListener(theServer);
lstClient.add(c);
synchronized (theServer) {
lstClient.add(c);
}
// executer.submit(c);
}
} catch(IOException ex) {}
Expand Down

0 comments on commit 2e3f826

Please sign in to comment.