-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
69 lines (62 loc) · 2.44 KB
/
Server.java
File metadata and controls
69 lines (62 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package Chat;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* The server side of the Chat program
*
* @author joar
*/
public class Server extends Connection {
ChatWindow chatWindow;
/**
* Creates a server, that accepts connections on a specified port
*
* @param port the specified port
* @param multiConversation if they should all share the same chat
* @throws IOException
*/
public Server(int port, boolean multiConversation) throws IOException {
super.cryptos.clear();
cryptos.clear();
this.multiConversation = multiConversation;
ServerSocket listener = new ServerSocket(port);
Runnable acceptOthers = () -> {
try {
Socket socket = listener.accept();
if (multiConversation) {
chatWindow = new ChatWindow(this, true);
} else {
chatWindow = new ChatWindow(this, socket, true);
}
chatWindow.setTitle("Server");
Thread messageParser = new Thread(new MessageParser(socket,
chatWindow.getConversation(), chatWindow.getController(),
this, false));
messageParser.start();
while (true) {
Socket newSocket = listener.accept();
if (multiConversation) {
Thread newMessageParser = new Thread(new MessageParser(
newSocket, chatWindow.getConversation(),
chatWindow.getController(), this, false));
newMessageParser.start();
} else {
ChatWindow newChat = new ChatWindow(this,
newSocket, true);
Thread newMessageParser = new Thread(new MessageParser(
newSocket, newChat.getConversation(),
newChat.getController(), this, false));
newChat.setTitle("Server");
newMessageParser.start();
}
}
} catch (Exception e) {
chatWindow.getConversation()
.addInfo("Error while creating server");
}
};
Thread acceptThread = new Thread(acceptOthers);
acceptThread.start();
}
}