-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarketServer.java
More file actions
77 lines (62 loc) · 2.38 KB
/
MarketServer.java
File metadata and controls
77 lines (62 loc) · 2.38 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
70
71
72
73
74
75
76
77
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.Runnable;
import java.text.*;
/**
* MarketServer
*
* The Thread ArrayList threads is created, and it can be used to start,
* stop or stop all threads. Thread thread uses the MarketRun object marketRun
* for its parameters. Arraylist threads consists of the thread made with marketRun.
*
* A new MarketRun object marketRun is created with Socket newSocket, DataInputStream socketReader,
* DataOutputStream socketWriter, and MarketServer server.
*/
public class MarketServer {
private static List<Thread> threads = new ArrayList<>();
private MarketServer server;
public MarketServer(int port) {
this.server = this;
}
public static void main(String[] args) throws IOException {
int port = 4242;
ServerSocket serverSocket = new ServerSocket(port);
//This creates the serverSocket
//Creates a new instance of MarketServer class, connecting to serverSocket
MarketServer server = new MarketServer(port);
while (true) {
Socket newSocket = null;
try {
newSocket = serverSocket.accept();
DataInputStream socketReader = new DataInputStream(newSocket.getInputStream());
DataOutputStream socketWriter = new DataOutputStream(newSocket.getOutputStream());
MarketRun marketRun = new MarketRun(newSocket, socketReader, socketWriter, server);
//This creates a new MarketRun object with Socket newSocket, DataInputStream socketReader,
// DataOutputStream socketWriter, and MarketServer server
Thread thread = new Thread(marketRun);
//This creates a new Thread object thread with parameter marketRun
threads.add(thread);
//This adds marketRun to the threads ArrayList
thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void startThread(Thread thread) {
threads.add(thread);
thread.start();
}
public void stopThread(Thread thread) {
threads.remove(thread);
thread.interrupt();
}
public void stopAllThreads() {
for (Thread thread : threads) {
thread.interrupt();
}
}
//End of the class
}