-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPmultiServer_threads.java
More file actions
60 lines (53 loc) · 2.61 KB
/
TCPmultiServer_threads.java
File metadata and controls
60 lines (53 loc) · 2.61 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TCPmultiServer_threads implements Runnable { //TCPmultiServer_threads extends Thread -> can be done either way
//because TCPmultiServer_threads does not extend another class
Socket clientSocket;
private Object socket;
private Object mySock;
TCPmultiServer_threads(Socket clientSocket) {
this.clientSocket = clientSocket;
}
public static void main(String args[]) throws IOException {
ServerSocket listener = new ServerSocket(3333);
System.out.println("Listening");
ExecutorService pool = Executors.newFixedThreadPool(10);
while (true) {
Socket sock = listener.accept();
System.out.println("Got connection from " + sock.getInetAddress());
pool.execute(new TCPmultiServer_threads(sock)); //start a new thread for the client,
} //so that we could accept other clients on ServerSocket listener at the same time
}
@Override
public void run() { //executed by every client thread
try {
System.out.println("Got connection from " + clientSocket.getInetAddress());
BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); //autoflush=true
//autoflush - A value indicating whether the stream should be automatically flushed when it is written to.
//equivalent to manual flush: out.flush(); -> This forces data to be sent to the server without closing the Socket.
String query;
while (true) {
query = input.readLine();
if (query == null || query.equals("END")) { // Short-circuit evaluation
break;
}
out.println(query);
}
System.out.println("Connection with " + clientSocket.getInetAddress() + " closed.");
clientSocket.close();
} catch (IOException ex) {
Logger.getLogger(TCPmultiServer_threads.class.getName()).log(Level.SEVERE, null, ex);
}
}
}