-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
30 lines (23 loc) · 1016 Bytes
/
Server.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
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server started. Listening on port 8888.");
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected.");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Client says: " + inputLine);
out.println("Server received: " + inputLine);
}
out.close();
in.close();
clientSocket.close();
System.out.println("Client disconnected.");
}
}
}