Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

help with JavaCode Please:: Exercise - Read File Over Network Onto Screen This p

ID: 3820243 • Letter: H

Question

help with JavaCode Please::

Exercise - Read File Over Network Onto Screen

This program will read a file over a network from a server and display the file on the screen of the client. It will use a TCP/IP stream socket connection between a server and a client. It will consist of two programs. You can do it on two computers or on the same computer as long as the two programs are both running separately.

Server (read file, write over network):

The hvcc.txt file attached will reside on the server computer. The server program will use a port number for a connection with clients. The connection will use the server's host address (local host if using the same computer). The server program will be able to send a file to the client upon request of the file's name if that file exists (if it doesn't exist, it will just send back a message saying so). As you loop through the reading the file, write it over the socket line by line. You can set it up so only one file is read by each client at a time.

Client (read over network, write onto screen):

The client program will prompt the user to enter the server's host address or name (address or name of server host using getByName()) and the server's determined port number for a connection with the server. The user will also be prompted to enter a file name and will be sent to the server. The file should then be read back over the network and displayed onto your computer (in readable lines). As you loop through reading over the socket, write it onto the screen line by line. You can set it up so only one file is read each time the program is run.

Both programs will use try blocks for possible exceptions. They both will use buffers and streams of data to read and write data. You may need to flush() the output buffer between writing the output streams.

1.        client will have user input host, port #, and filename.

2.        client writes filename to server.

3.        server reads filename from client.

4         server reads the file from their computer line by line.

5.        server writes the file to client line by line.

6.        client reads the file from the server line by line.

7.        client writes the file onto screen line by line.

The output to be submitted will be:

Server - server program code with full documentation - 4 line full paragraph about program, and at least 5 lines explaining throughout.

Client - client program code with full documentation - 4 line full paragraph about program, and at least 5 lines explaining throughout. The client console output screen copied and pasted (this will be the server's file on the client's computer screen) into a separate plain text output file.

The server will run and wait for client requests. The client will run and input the server host address and port number and will request the attached hvcc.txt file from the server. The file will be shown on the client console.

This is the txtFile:

Explanation / Answer

import java.net.*;

import java.io.*;

public class ContentsClient  

{

  public static void main( String args[ ] ) throws Exception

  {

     Socket sock = new Socket( "127.0.0.1", 4000);

                   // reading the file name from keyboard. Uses input stream

     System.out.print("Enter the file name");

     BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));

     String fname = keyRead.readLine();

                                        

        // sending the file name to server. Uses PrintWriter

     OutputStream  ostream = sock.getOutputStream( );

     PrintWriter pwrite = new PrintWriter(ostream, true);

     pwrite.println(fname);            

                     // receiving the contents from server.  Uses input stream

     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(); keyRead.close();

  }

}

import java.net.*;    

import java.io.*;

public class ContentsServer  

{

  public static void main(String args[]) throws Exception

  {                           // establishing the connection with the server

     ServerSocket sersock = new ServerSocket(4000);

     System.out.println("Server ready for connection");

     Socket sock = sersock.accept();            // binding with port: 4000

     System.out.println("Connection is successful and wating for chatting");

                                                                                                

                               // reading the file name from client

     InputStream istream = sock.getInputStream( );

     BufferedReader fileRead =new BufferedReader(new InputStreamReader(istream));

     String fname = fileRead.readLine( );

                             // reading file contents

     BufferedReader contentRead = new BufferedReader(new FileReader(fname) );

          

                           // keeping output stream ready to send the contents

     OutputStream ostream = sock.getOutputStream( );

     PrintWriter pwrite = new PrintWriter(ostream, true);

    

     String str;

     while((str = contentRead.readLine()) !=  null) // reading line-by-line from file

     {

pwrite.println(str);         // sending each line to client

     }

     sock.close();  sersock.close();       // closing network sockets

     pwrite.close();  fileRead.close(); contentRead.close();

  }

}

import java.net.*;    

import java.io.*;

public class ContentsServer  

{

  public static void main(String args[]) throws Exception

  {                           // establishing the connection with the server

     ServerSocket sersock = new ServerSocket(4000);

     System.out.println("Server ready for connection");

     Socket sock = sersock.accept();            // binding with port: 4000

     System.out.println("Connection is successful and wating for chatting");

                                                                                                

                               // reading the file name from client

     InputStream istream = sock.getInputStream( );

     BufferedReader fileRead =new BufferedReader(new InputStreamReader(istream));

     String fname = fileRead.readLine( );

                             // reading file contents

     BufferedReader contentRead = new BufferedReader(new FileReader(fname) );

          

                           // keeping output stream ready to send the contents

     OutputStream ostream = sock.getOutputStream( );

     PrintWriter pwrite = new PrintWriter(ostream, true);

    

     String str;

     while((str = contentRead.readLine()) !=  null) // reading line-by-line from file

     {

pwrite.println(str);         // sending each line to client

     }

     sock.close();  sersock.close();       // closing network sockets

     pwrite.close();  fileRead.close(); contentRead.close();

  }

}