-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTcpServer.java
32 lines (31 loc) · 1.2 KB
/
TcpServer.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
31
32
import java.net.*;
import java.io.*;
public class TcpServer {
public static void main(String[] args) throws Exception {
ServerSocket sersock = new ServerSocket(4000);
System.out.println("Server Connected, waiting for client");
Socket sock = sersock.accept();
System.out.println("Connection successful, waiting for filename");
InputStream iStream = sock.getInputStream();
BufferedReader nameRead = new BufferedReader(new InputStreamReader(iStream));
String fname = nameRead.readLine();
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
try {
BufferedReader contentRead = new BufferedReader(new FileReader(fname));
String str;
while ((str = contentRead.readLine()) != null) {
pwrite.println(str);
}
contentRead.close();
} catch (FileNotFoundException e) {
pwrite.println("File does not exist");
} finally {
System.out.println("Closing connection");
pwrite.close();
nameRead.close();
sock.close();
sersock.close();
}
}
}