-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreadServer.java
More file actions
110 lines (90 loc) · 3.12 KB
/
MultiThreadServer.java
File metadata and controls
110 lines (90 loc) · 3.12 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.io.*;
import java.net.*;
import java.util.Date;
import javax.swing.*;
public class MultiThreadServer extends JFrame implements Runnable {
// Text area for displaying contents
private JTextArea ta;
// Number a client
private int clientNo = 0;
public MultiThreadServer() {
ta = new JTextArea(10,10);
JScrollPane sp = new JScrollPane(ta);
this.add(sp);
this.setTitle("MultiThreadServer");
this.setSize(400,200);
Thread t = new Thread(this);
t.start();
}
public void run() {
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
ta.append("MultiThreadServer started at "
+ new Date() + '\n');
while (true) {
// Listen for a new connection request
Socket socket = serverSocket.accept();
// Increment clientNo
clientNo++;
ta.append("Starting thread for client " + clientNo +
" at " + new Date() + '\n');
// Find the client's host name, and IP address
InetAddress inetAddress = socket.getInetAddress();
ta.append("Client " + clientNo + "'s host name is "
+ inetAddress.getHostName() + "\n");
ta.append("Client " + clientNo + "'s IP Address is "
+ inetAddress.getHostAddress() + "\n");
// Create and start a new thread for the connection
new Thread(new HandleAClient(socket, clientNo)).start();
}
}
catch(IOException ex) {
System.err.println(ex);
}
}
// Define the thread class for handling new connection
class HandleAClient implements Runnable {
private Socket socket; // A connected socket
private int clientNum;
/** Construct a thread */
public HandleAClient(Socket socket, int clientNum) {
this.socket = socket;
this.clientNum = clientNum;
}
/** Run a thread */
public void run() {
try {
// Create data input and output streams
DataInputStream inputFromClient = new DataInputStream(
socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(
socket.getOutputStream());
// Continuously serve the client
while (true) {
// Receive radius from the client
double radius = inputFromClient.readDouble();
// Compute area
double area = radius * radius * Math.PI;
// Send area back to the client
outputToClient.writeDouble(area);
ta.append("radius received from client: " + this.clientNum + " " +
radius + '\n');
ta.append("Area found: " + area + '\n');
}
}
catch(IOException ex) {
ex.printStackTrace();
}
}
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
MultiThreadServer mts = new MultiThreadServer();
mts.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mts.setVisible(true);
}
}