-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
46 lines (40 loc) · 1.21 KB
/
Server.java
File metadata and controls
46 lines (40 loc) · 1.21 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
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
/*
* Main server class. This class includes main(), and is the class that listens
* for incoming connections and starts ServerThreads to handle those connections
*
*/
public class Server {
/**
* @param args
*/
public static void main(String[] args) {
AtomicInteger numThreads = new AtomicInteger(0);
// the list of threads is kept in a linked list
ArrayList<Thread> list = new ArrayList<Thread>();
try {
// listen for incoming connections on port 15432
ServerSocket socket = new ServerSocket(15432);
System.out.println("Server listening on port 15432");
// loop (forever) until program is stopped
while(true) {
// accept a new connection
Socket client = socket.accept();
// start a new ServerThread to handle the connection and send
// output to the client
Thread thrd = new Thread(new ServerThread(client));
list.add(thrd);
thrd.start();
numThreads.incrementAndGet();
System.out.println("Thread " + numThreads.get() + " started.");
}
}
catch (IOException ioe){
ioe.printStackTrace();
}
}
}