Write a program that will read a file over a network from a server and display t
ID: 3548457 • Letter: W
Question
Write a program that 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 student and a client student.
Server Student (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 Student (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 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.
Explanation / Answer
Server side:
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/fileserver")
public class server extends HttpServlet{
FileWriter writer;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
String filename = request.getHeader("filename");
FileInputStream fr = new FileInputStream(filename);
ServletOutputStream wr = response.getOutputStream();
byte[] buf = new byte[1024];
int read;
while((read = fr.read(buf, 0, 1024)) > 0)
{
wr.write(buf, 0, read);
}
wr.flush();
}
catch(Exception ex)
{
response.addHeader("error", ex.toString());
}
}
}
Client side:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
public class client {
public static void main(String[] args)
{
try
{
System.out.println("Enter the server address with port");
Scanner sc = new Scanner(System.in);
String server = sc.nextLine();
//server = "http://192.168.2.20:8080/ChatAppWebServer/fileserver"; //Example
System.out.println("Enter the filename");
String filename = sc.nextLine();
//filename = "C:\Users\sam\Desktop\output.txt"; Example
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(server);
httppost.addHeader("filename", filename);
// Request parameters and other properties
//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
if(response.containsHeader("error"))
{
System.out.println(response.getHeaders("error")[0].getValue());
}
else
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 65728);
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}catch(Exception ex)
{
System.out.println("Error in retrieving info");
ex.printStackTrace();
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.