×
NOTE!
Click on MENU to Browse between Subjects...
Advertisement
17CSL57
COMPUTER NETWORK LABORATORY
[As per Choice Based Credit System (CBCS) scheme]
(Effective from the academic year 2017-2018)
SEMESTER - V
This Page Provides Program & Output.
Program 9
Program 9
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.
Advertisement
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 | import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class TCPServer { public static void main(String args[]) throws Exception { ServerSocket sersock = new ServerSocket(4000); System.out.println("Server ready for connection"); Socket sock = sersock.accept(); System.out.println("Connection successful | wating for filename"); InputStream istream = sock.getInputStream( ); BufferedReader br =new BufferedReader(new InputStreamReader(istream)); String fname = br.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); } System.out.println("File Contents sent successfully"); sock.close(); sersock.close(); pwrite.close(); br.close(); contentRead.close(); } } |
TCPClient.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 | import java.net.*; import java.io.*; public class TCPClient { public static void main( String args[ ] ) throws Exception { Socket sock = new Socket( "127.0.0.1", 4000); System.out.print("Enter the file name\n"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String fname = br.readLine(); OutputStream ostream = sock.getOutputStream( ); PrintWriter pwrite = new PrintWriter(ostream, true); pwrite.println(fname); InputStream istream = sock.getInputStream(); BufferedReader socketRead = new BufferedReader(new InputStreamReader(istream)); String str; while((str = socketRead.readLine()) != null) // reading line-by-line { System.out.println(str); } pwrite.close(); socketRead.close(); br.close(); sock.close(); } } |
Process to Execute the Program
Step 1: We need to have Java JDK installed, So That Java Programs can Run.
Step 2: Copy & Paste the Below Code of TCPServer.java & TCPClient.java.
Step 3: or simple Download the Source Code.
Fig 9.1: Required Files .
Step 5: Navigate to the TCPServer.java & TCPClient.java File location.
Step 6: Use CD & DIR command on cmd to navigate.
Step 7: First Run the TCPServer.java => javac TCPServer.java
Step 8: => java TCPServer
Fig 9.2: Demonstration of TCPServer .
Step 10: => java TCPClient
Fig 9.3: Demonstration of TCPClient .
Fig 9.4: Content inside file.txt (In my case) .
×
Note
Please Share the website link with Your Friends and known Students...
-ADMIN
-ADMIN
×
Note
Page Number is specified to navigate between Pages...
T = Text book
QB = Question Bank
AS = Amswer Script
-ADMIN
T = Text book
QB = Question Bank
AS = Amswer Script
-ADMIN