-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrentServer.java
More file actions
49 lines (40 loc) · 1.52 KB
/
Copy pathConcurrentServer.java
File metadata and controls
49 lines (40 loc) · 1.52 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
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
class ConcurrentServer {
public static void main(String[] args) throws IOException {
int port = 6789;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server running on port " + port);
while (true) {
// Accept new client connection
Socket clientSocket = serverSocket.accept();
System.out.println("New client connected from port: " + clientSocket.getPort());
// Handle client in a new thread
ClientHandler clientHandler = new ClientHandler(clientSocket);
new Thread(clientHandler).start();
}
}
}
class ClientHandler implements Runnable {
private Socket clientSocket;
public ClientHandler(Socket socket) {
this.clientSocket = socket;
}
@Override
public void run() {
try {
// Get output stream to send data to client
DataOutputStream outToClient = new DataOutputStream(clientSocket.getOutputStream());
// Get current system time
String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date());
// Send system time to client
outToClient.writeBytes("Current Server Time: " + timeStamp + "\n");
// Close client connection
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}