Advertisement
shamiul93

server

Nov 3rd, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package mailpractice;
  7.  
  8. import java.io.IOException;
  9. import java.net.ServerSocket;
  10. import java.net.Socket;
  11.  
  12. /**
  13.  *
  14.  * @author Heisenberg
  15.  */
  16. public class finalServer {
  17.     private static int workerThreadNo, id;
  18.     public static void main(String[] args) throws IOException {
  19.         workerThreadNo = 0;
  20.         id = 0 ;
  21.         int port = 6789;
  22.         ServerSocket ss = new ServerSocket(port);
  23.         System.out.println("Server Socket at port " + port + " is opened successfully.");
  24.        
  25.         while(true)
  26.         {
  27.             Socket s = ss.accept();
  28.             finalHTTPWorker worker = new finalHTTPWorker(s, id);
  29.             Thread t = new Thread(worker);
  30.             t.start();
  31.             workerThreadNo++;
  32.             id++;
  33.         }
  34.     }
  35. }
  36.  
  37.  
  38.  
  39.  
  40. public class finalHTTPWorker implements Runnable {
  41.  
  42.     private Socket socket;
  43.     InputStream is = null;
  44.     OutputStream os = null;
  45.     int id;
  46.  
  47.     public finalHTTPWorker(Socket s, int id) throws IOException {
  48.         this.socket = s;
  49.         this.id = id;
  50.  
  51.         is = s.getInputStream();
  52.         os = s.getOutputStream();
  53.     }
  54.  
  55.     @Override
  56.     public void run() {
  57.         BufferedReader br = new BufferedReader(new InputStreamReader(is));
  58.         PrintWriter pw = new PrintWriter(os);
  59.        // while (true) {
  60.             try {
  61.                 String str = br.readLine(), fileName;
  62.  
  63.                 /*GET /ihi.jpg HTTP/1.1*/
  64. //                StringTokenizer tokenizer = new StringTokenizer(str, "/");
  65. //                fileName = tokenizer.nextToken();
  66. //                if (tokenizer.hasMoreTokens()) {
  67. //
  68. //                    fileName = tokenizer.nextToken();
  69. //                }
  70. //                
  71. //                fileName = fileName.replace(" HTTP", "");
  72. //
  73. //                if (fileName.equals("")) {
  74. //                    fileName = "index.html";
  75. //                }
  76.  
  77.                
  78.                
  79.                 pw.println("HTTP/1.0 200 OK");
  80.                 pw.flush();
  81.                
  82.                 System.out.println(br.readLine());
  83.                
  84.                 pw.println("MIME-Version: 1.0");
  85.                 pw.flush();
  86.                 pw.println("Content-Type: image/png");
  87.                 pw.flush();
  88.                 System.out.println(br.readLine());
  89.                
  90.                
  91.                 fileName = "riko.png";
  92.                
  93.                 System.out.println(fileName);
  94.                 File file = new File(fileName);
  95.                 byte[] content;
  96.  
  97.                 FileInputStream fis = new FileInputStream(file);
  98.                 BufferedInputStream bis = new BufferedInputStream(fis);
  99.                 OutputStream sendFileStream = os;
  100.  
  101.                 int fileLength = (int) file.length();
  102.                
  103.                 pw.println("Content-Length: " + fileLength);
  104.                 pw.flush();
  105.                
  106.                 pw.println("\r\n");
  107.                 pw.flush();
  108.                 System.out.println(br.readLine());
  109.                
  110.  
  111.                 long current = 0;
  112.  
  113.                 while (current != fileLength) {
  114.                     int size = 10000;
  115.                     if (fileLength - current >= size) {
  116.                         current += size;
  117.                     } else {
  118.                         size = (int) (fileLength - current);
  119.                         current = fileLength;
  120.                     }
  121.  
  122.                     content = new byte[size];
  123.                     bis.read(content, 0, size);
  124.                     sendFileStream.write(content);
  125.                     //System.out.println("Hiiii");
  126.                 }
  127.                 sendFileStream.flush();
  128.                 System.out.println("File sent successfully!");
  129.                
  130.                 is.close();
  131.                 os.close();
  132.                
  133.                
  134.                 socket.close();
  135.  
  136.             } catch (IOException ex) {
  137.                 Logger.getLogger(finalHTTPWorker.class.getName()).log(Level.SEVERE, null, ex);
  138.  
  139.             }
  140.        // }
  141.  
  142.     }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement