-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4-TCPS.java
More file actions
41 lines (28 loc) · 1.11 KB
/
4-TCPS.java
File metadata and controls
41 lines (28 loc) · 1.11 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
// Using TCP/IP sockets, write a client – server program to make the client send the file name and to make the server send back the contents of the requested file if present.
import java.util.*;
import java.net.*;
import java.io.*;
public class TCPS{
public static void main(String[] args) throws Exception{
ServerSocket sersock=new ServerSocket(4000);
System.out.println("Server is ready for connection");
Socket sock=sersock.accept();
System.out.println("Connection is established and server is waiting for client request");
InputStream istream=sock.getInputStream();
BufferedReader fileRead=new BufferedReader(new InputStreamReader(istream));
String fname=fileRead.readLine();
BufferedReader contentRead=new BufferedReader(new FileReader(fname));
OutputStream ostream=sock.getOutputStream();
PrintWriter pwrite=new PrintWriter(ostream,true);
String str;
while((str=contentRead.readLine())!=null){
pwrite.println(str);
}
sersock.close();
sock.close();
fileRead.close();
contentRead.close();
pwrite.close();
System.out.println("Client request fulfilled");
}
}