-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathServer.java
More file actions
63 lines (55 loc) · 2.8 KB
/
Copy pathServer.java
File metadata and controls
63 lines (55 loc) · 2.8 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
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.function.Consumer;
public class Server {
//we will handle each client in a separate thread
public Consumer<Socket> getConsumer() {
//Consumer is a functional interface that takes a single argument and returns nothing
//clientSocket is the socket of the client
//PrintWriter is used to send data to the client
//true is used to flush the output stream
//clientSocket.getOutputStream() returns the output stream of the client
return (clientSocket) -> {
//lambda expression is used to create a new thread for each client
try (PrintWriter toSocket = new PrintWriter(clientSocket.getOutputStream(), true)) {
//clientSocket.getInetAddress() returns the IP address of the client
toSocket.println("Hello from server " + clientSocket.getInetAddress());
} catch (IOException ex) {
ex.printStackTrace();
}
};
}
public static void main(String[] args) {
int port = 8010;
Server server = new Server();
try {
ServerSocket serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(70000);
System.out.println("Server is listening on port " + port);
//infinite loop to accept connections
while (true) {
//accepting a connection from the client
Socket clientSocket = serverSocket.accept();
//serverSocket.accept() is a blocking call.
//the programs execution pauses here until a client connects.
//when a connection is made, it returns a socket objeect.
//this socket object is then passed to the getConsumer() method.
//creating a new thread for each client
//getConsumer() is a method that returns a Consumer<Socket>
//accept(clientSocket) is a method that accepts a Socket
//server.getConsumer().accept(clientSocket) is a method that accepts a Socket
//server.getConsumer() is a method that returns a Consumer<Socket>
//server.getConsumer().accept(clientSocket) is a method that accepts a Socket
Thread thread = new Thread(() -> server.getConsumer().accept(clientSocket));
thread.start();
//every time a client connects, this upper code will start a new thread.
//the actual contet of the code that is to be run is ddefined in the getConsumer() method.
//where it handles the client handling logic.
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}