Question: I'm currently working on a client | proxy server | server, that the client requests a file from the server and the server then returns
I'm currently working on a client | proxy server | server, that the client requests a file from the server and the server then returns the contents of the file to the client, and the client prints out the received contents to the terminal. For some reason client will only read one line of the received file then closes. The client and the server work fine but it seems to be an issue with the proxy server. PLEASE CODE IN JAVA.
Client.java
import java.net.*; import java.io.*; import java.lang.*;
public class client{ public final static String FILE_RECIEVED = "file.html"; public static void runclient (String urlUse) throws IOException{ URL url = new URL(urlUse); String address = url.getHost(); String file; if(url.getFile()=="") file = "index.html" ; else{ file = url.getFile();} Socket s = new Socket(address, 8080); OutputStream out = s.getOutputStream(); System.out.println(file); String message = "GET " + file + " HTTP/1.0 "; out.write(message.getBytes()); out.flush(); InputStreamReader in = new InputStreamReader(s.getInputStream()); try{ BufferedReader bf = new BufferedReader(in); String file1; while((file1=bf.readLine()) != null){ System.out.println(file1); } } catch(IOException e) {System.err.println(e);} s.close(); } public static void main (String[] args) { try{ runclient("http://localhost/testhtml.html"); } catch (IOException e){System.err.println(e);} } }
Server.java
import java.net.*; import java.io.*; import java.lang.*; import java.util.*; public class server{ private static FileInputStream inFile; private static OutputStream outFile; private static BufferedInputStream bis; private static ServerSocket ss; private static Socket s; public static void runServer(int port) throws IOException{ ss = new ServerSocket(port); inFile=null; bis=null; outFile=null; s = ss.accept(); System.out.println("client connected"); InputStreamReader in = new InputStreamReader(s.getInputStream()); BufferedReader bf = new BufferedReader(in); String str = bf.readLine(); String[] split = str.split(" "); String str1 = split[1].replace("/",""); System.out.println(str1); fileFinder(str1); s.close(); ss.close(); } public static void fileFinder(String str) throws IOException{ File input = new File(str); if(input.exists()){ try(BufferedReader br = new BufferedReader(new FileReader(str))){ PrintStream pr = new PrintStream(s.getOutputStream()); String line; while ((line = br.readLine())!= null){ pr.println(line); System.out.println(line); pr.flush(); } } catch(IOException e){} } else{ PrintStream pr = new PrintStream(s.getOutputStream()); pr.println("404: Not Found"); } } public static void main (String[] args) { try{ runServer(4999); } catch(IOException e){ System.out.println("Broken"); } } }
Proxyserver.java
import java.net.*; import java.io.*; import java.lang.*; public class proxyserver{ private static ServerSocket ss; private static Socket s; private static Socket client; public static void Proxyrun(String host, int serverport, int clientport ) throws IOException{ final byte[] request = new byte[1024]; byte[] reply = new byte[4096]; ss = new ServerSocket(clientport); client = ss.accept(); System.out.println("Client connected to Proxy"); InputStreamReader clientin = new InputStreamReader(client.getInputStream()); OutputStream clientout =client.getOutputStream();
Socket s = new Socket(host, serverport); PrintStream serverout = new PrintStream(s.getOutputStream()); InputStream serverin = s.getInputStream(); Thread t = new Thread(){ public void run(){ while(clientin != null){ BufferedReader bf = new BufferedReader(clientin); try{ String str = bf.readLine(); serverout.println(str); serverout.flush(); } catch(IOException e){ } serverout.close(); } } }; t.start(); int bytesread; try{ while ((bytesread=serverin.read(reply))!= -1){
clientout.write(reply,0,bytesread); System.out.println("Sent to client"); clientout.flush(); } } catch(IOException e){} client.close(); s.close();
} public static void main (String[] args) { try{ Proxyrun("localhost", 4999, 8080); } catch (IOException e){System.out.println("Broken");} } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
