-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreadedServer.java
More file actions
76 lines (62 loc) · 2.39 KB
/
MultiThreadedServer.java
File metadata and controls
76 lines (62 loc) · 2.39 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
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
class MultiThreadedServer {
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());
// Create separate threads for reading and writing
Thread readThread = new Thread(new ReadHandler(clientSocket));
Thread writeThread = new Thread(new WriteHandler(clientSocket));
// Start both threads
readThread.start();
writeThread.start();
}
}
}
// Thread for reading data from the client
class ReadHandler implements Runnable {
private Socket clientSocket;
public ReadHandler(Socket socket) {
this.clientSocket = socket;
}
@Override
public void run() {
try {
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String clientMessage;
while ((clientMessage = inFromClient.readLine()) != null) {
System.out.println("Client says: " + clientMessage);
}
clientSocket.close();
} catch (IOException e) {
System.out.println("Client disconnected.");
}
}
}
// Thread for writing data to the client (sends system time once)
class WriteHandler implements Runnable {
private Socket clientSocket;
public WriteHandler(Socket socket) {
this.clientSocket = socket;
}
@Override
public void run() {
try {
DataOutputStream outToClient = new DataOutputStream(clientSocket.getOutputStream());
// Send current system time once
String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date());
outToClient.writeBytes("Server Time: " + timeStamp + "\n");
// Close output stream after sending time once
outToClient.flush();
} catch (IOException e) {
System.out.println("Client disconnected.");
}
}
}