-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileServer.java
More file actions
35 lines (29 loc) · 1.09 KB
/
FileServer.java
File metadata and controls
35 lines (29 loc) · 1.09 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
import java.io.*;
import java.net.*;
public class FileServer {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(5000);
System.out.println("Server started. Waiting for client...");
Socket client = server.accept();
System.out.println("Client connected!");
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
String filename = in.readLine();
System.out.println("Requested file: " + filename);
File file = new File(filename);
if (file.exists()) {
BufferedReader fr = new BufferedReader(new FileReader(file));
String line;
while ((line = fr.readLine()) != null) {
out.println(line);
}
fr.close();
} else {
out.println("File not found!");
}
in.close();
out.close();
client.close();
server.close();
}
}