Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.io.File;
- class Server{
- public static void main (String[]args)throws IOException{
- ServerSocket socket1 = new ServerSocket (8000);
- while (true) {
- Socket incoming = socket1.accept();
- new newclass(incoming).start();
- }
- }
- }
- class newclass extends Thread implements Runnable {
- Socket incoming;
- public newclass(Socket incoming) {
- this.incoming = incoming;
- }
- public void run() {
- try {
- byte x = 0;
- String z;
- String s = "HTTP 1.0 200 Document follows";
- String s1 = "Bad request message";
- BufferedReader input = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
- PrintWriter output = new PrintWriter(incoming.getOutputStream(), true);
- DataOutputStream sending = new DataOutputStream(incoming.getOutputStream());
- File directory = new File("C:\Documents and Settings\Ahmed\Desktop\bla\Server");
- File[] files = directory.listFiles();
- int x1 = files.length;
- if ((x1 - 3) < 10) {
- boolean done = false;
- while (!done) {
- String line = input.readLine();
- System.out.println(line);
- if (line.equals("BYE")) {
- output.println("BYE");
- done = true;
- } else {
- if (line.trim().substring(0, 3).equals("GET ")) {
- if (line.equals("<javalogo.png> HTTP 1.0")) {
- File f = new File("javalogo.png");
- int size = (int) f.length();
- if (f.exists() == true) {
- output.println(s);
- output.println(size);
- output.println("javalogo1.png");
- DataInputStream bytefile = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));
- while (bytefile.available() != 0) {
- x = bytefile.readByte();
- sending.write(x);
- }
- } else {
- System.out.println("Getting file from main server");
- Socket socket2 = new Socket("127.0.0.1", 8100);
- BufferedReader bUsr = new BufferedReader(new InputStreamReader(System.in));
- PrintWriter pOut = new PrintWriter(socket2.getOutputStream(), true);
- BufferedReader bIn = new BufferedReader(new InputStreamReader(socket2.getInputStream()));
- pOut.println("GET <javalogo.png> HTTP 1.0");
- String rep = bIn.readLine();
- if (rep.equals("HTTP 1.0 200 Document follows")) {
- int len = Integer.parseInt(bIn.readLine());
- String fname = bIn.readLine();
- File f1 = new File(fname);
- f1.createNewFile();
- FileOutputStream fos = new FileOutputStream(f1);
- DataInputStream dis = new DataInputStream(socket2.getInputStream());
- for (int i = 0; i < len; i++) {
- fos.write(dis.read());
- }
- fos.close();
- } else if (rep.equals("File does not exist")) {
- output.println("Sorry, but the file was neither found in the proxy server or the main server or the name is wrong.");
- }
- }
- }
- File f2 = new File("javalogo.png");
- if (f2.exists() == true) {
- int size = (int) f2.length();
- output.println(s);
- output.println(size);
- output.println("javalogo.png");
- DataInputStream bytefile = new DataInputStream(new BufferedInputStream(new FileInputStream(f2)));
- while (bytefile.available() != 0) {
- x = bytefile.readByte();
- sending.write(x);
- }
- }
- } else {
- System.out.println(s1);
- output.println(s1);
- }
- }
- }
- incoming.close();
- }
- output.println("Connecting to main server");
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- }
- }
- import java.net.*;
- import java.io.*;
- public class Client {
- public static void main(String[] args) throws Exception {
- Socket socket1 = new Socket("127.0.0.1", 8000);
- BufferedReader bUsr = new BufferedReader(new InputStreamReader(System.in));
- PrintWriter pOut = new PrintWriter(socket1.getOutputStream(), true);
- BufferedReader bIn = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
- String cmd;
- String rep;
- while (true) {
- cmd = bUsr.readLine();
- pOut.println(cmd);
- System.out.println(rep = bIn.readLine());
- if (cmd.equals("BYE") || cmd.equals("END"))
- break;
- else if (rep.equals("HTTP 1.0 200 Document follows")) {
- int len = Integer.parseInt(bIn.readLine());
- String fname = bIn.readLine();
- File f = new File(fname);
- f.createNewFile();
- FileOutputStream fos = new FileOutputStream(f);
- DataInputStream dis = new DataInputStream(socket1.getInputStream());
- for (int i = 0; i < len; i++) {
- fos.write(dis.read());
- }
- fos.close();
- System.out.println("Success");
- } else if (rep.equals("Connecting to main server")) {
- Socket socket1 = new Socket("127.0.0.1", 8100);
- BufferedReader bUsr = new BufferedReader(new InputStreamReader(System.in));
- PrintWriter pOut = new PrintWriter(socket1.getOutputStream(), true);
- BufferedReader bIn = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
- String cmd;
- String rep;
- while (true) {
- cmd = bUsr.readLine();
- pOut.println(cmd);
- System.out.println(rep = bIn.readLine());
- if (cmd.equals("BYE") || cmd.equals("END"))
- break;
- else if (rep.equals("HTTP 1.0 200 Document follows")) {
- int len = Integer.parseInt(bIn.readLine());
- String fname = bIn.readLine();
- File f = new File(fname);
- f.createNewFile();
- FileOutputStream fos = new FileOutputStream(f);
- DataInputStream dis = new DataInputStream(socket1.getInputStream());
- for (int i = 0; i < len; i++) {
- fos.write(dis.read());
- }
- fos.close();
- System.out.println("Success");
- }
- }
- }
- bIn.close();
- pOut.close();
- socket1.close();
- }
- }
Add Comment
Please, Sign In to add comment