-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiThreadServerGUI.java
65 lines (52 loc) · 2.19 KB
/
MultiThreadServerGUI.java
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
package threaded;
import java.io.*;
import java.io.Serializable;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class MultiThreadServerGUI extends JFrame {
private JTextArea jta = new JTextArea();
// private Server server = new Server();
private ServerSocket serverSocket;
Socket socket;
public static void main(String[] args) {
new MultiThreadServerGUI();
}
public MultiThreadServerGUI() {
// Place text area on the frame
setLayout(new BorderLayout());
add(new JScrollPane(jta), BorderLayout.CENTER);
setTitle("MultiThreadServer");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); // It is necessary to show the frame here!
jta.append("MultiThreadServer started at " + new Date() + '\n');
int clientNo = 1;
try {
serverSocket = new ServerSocket(8000);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (true) {
// Listen for a new connection request
try {
socket = serverSocket.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Display the client number
jta.append("Starting thread for client " + clientNo + " at " + new Date() + '\n');
// Find the client's host name, and IP address
InetAddress inetAddress = socket.getInetAddress();
jta.append("Client " + clientNo + "'s host name is " + inetAddress.getHostName() + "\n");
jta.append("Client " + clientNo + "'s IP Address is " + inetAddress.getHostAddress() + "\n");
// Create a new task for the connection
Thread task = new Thread(new HandleAClient(socket));
task.start();
clientNo++;
}
}
}