Advertisement
Esha1

Server

Nov 17th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. package ServerProgram;
  2.  
  3. import java.net.ServerSocket;
  4. import java.io.BufferedOutputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8.  
  9. import java.net.Socket;
  10.  
  11. public class main {
  12.  
  13. private static ServerSocket serverSocket;
  14. private static Socket clientSocket;
  15. private static InputStream inputStream;
  16. private static FileOutputStream fileOutputStream;
  17. private static BufferedOutputStream bufferedOutputStream;
  18. private static int filesize = 1000; // filesize
  19. private static int bytesRead;
  20. private static int current = 0;
  21. public static void main(String[] args) throws IOException {
  22.  
  23.  
  24. serverSocket = new ServerSocket(8080); //Server socket
  25.  
  26. System.out.println("Server started. Listening to the port 8080");
  27.  
  28.  
  29. clientSocket = serverSocket.accept();
  30.  
  31.  
  32. byte[] mybytearray = new byte[filesize]; //create byte array to buffer the file
  33.  
  34. inputStream = clientSocket.getInputStream();
  35. fileOutputStream = new FileOutputStream("D:/output.txt");
  36. bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
  37.  
  38. System.out.println("Receiving...");
  39.  
  40. //following lines read the input slide file byte by byte
  41. // bytesRead = inputStream.read(mybytearray, 0, mybytearray.length);
  42. // bytesRead = inputStream.read(mybytearray, 0, mybytearray.length);
  43. //System.out.println(bytesRead);
  44. // current = bytesRead;
  45. // System.out.println(mybytearray.length);
  46. System.out.println(current);
  47.  
  48. do {
  49. bytesRead = inputStream.read(mybytearray, current, (mybytearray.length - current));
  50. bufferedOutputStream.write(mybytearray, 0, current);
  51. System.out.println(bytesRead);
  52. if (bytesRead >= 0) {
  53. current += bytesRead;
  54. }
  55. } while (bytesRead > -1);
  56.  
  57.  
  58. //byte[] buffer = new byte[BUFFER_SIZE];
  59.  
  60. // int bytesRead = 0;
  61. // while ((bytesRead = inputStream.read(mybytearray)) >= 0){
  62. // System.out.println(bytesRead);
  63. // for (int i = 0; i < bytesRead; i++){
  64. // //Do whatever you need with the bytes here
  65. // System.out.println(i);
  66. // System.out.println(bytesRead);
  67. // bufferedOutputStream.write(mybytearray, 0, bytesRead);
  68. // }
  69. // }
  70.  
  71.  
  72. // System.out.println(current);
  73.  
  74. bufferedOutputStream.flush();
  75. bufferedOutputStream.close();
  76. inputStream.close();
  77. clientSocket.close();
  78. serverSocket.close();
  79.  
  80. System.out.println("Server recieved the file");
  81.  
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement