Guest User

How to resolve a Stream Closed Error in java

a guest
Feb 26th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.03 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.io.File;
  4.  
  5. class Server{
  6. public static void main (String[]args)throws IOException{
  7. ServerSocket socket1 = new ServerSocket (8000);
  8. while (true) {
  9. Socket incoming = socket1.accept();
  10. new newclass(incoming).start();
  11. }
  12. }
  13. }
  14.  
  15. class newclass extends Thread implements Runnable {
  16.  
  17. Socket incoming;
  18.  
  19. public newclass(Socket incoming) {
  20. this.incoming = incoming;
  21. }
  22.  
  23. public void run() {
  24. try {
  25. byte x = 0;
  26. String z;
  27. String s = "HTTP 1.0 200 Document follows";
  28. String s1 = "Bad request message";
  29. BufferedReader input = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
  30. PrintWriter output = new PrintWriter(incoming.getOutputStream(), true);
  31. DataOutputStream sending = new DataOutputStream(incoming.getOutputStream());
  32. File directory = new File("C:\Documents and Settings\Ahmed\Desktop\bla\Server");
  33. File[] files = directory.listFiles();
  34. int x1 = files.length;
  35. if ((x1 - 3) < 10) {
  36. boolean done = false;
  37. while (!done) {
  38. String line = input.readLine();
  39. System.out.println(line);
  40. if (line.equals("BYE")) {
  41. output.println("BYE");
  42. done = true;
  43. } else {
  44. if (line.trim().substring(0, 3).equals("GET ")) {
  45. if (line.equals("<javalogo.png> HTTP 1.0")) {
  46. File f = new File("javalogo.png");
  47. int size = (int) f.length();
  48. if (f.exists() == true) {
  49. output.println(s);
  50. output.println(size);
  51. output.println("javalogo1.png");
  52. DataInputStream bytefile = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));
  53. while (bytefile.available() != 0) {
  54. x = bytefile.readByte();
  55. sending.write(x);
  56. }
  57. } else {
  58. System.out.println("Getting file from main server");
  59. Socket socket2 = new Socket("127.0.0.1", 8100);
  60. BufferedReader bUsr = new BufferedReader(new InputStreamReader(System.in));
  61. PrintWriter pOut = new PrintWriter(socket2.getOutputStream(), true);
  62. BufferedReader bIn = new BufferedReader(new InputStreamReader(socket2.getInputStream()));
  63. pOut.println("GET <javalogo.png> HTTP 1.0");
  64. String rep = bIn.readLine();
  65. if (rep.equals("HTTP 1.0 200 Document follows")) {
  66. int len = Integer.parseInt(bIn.readLine());
  67. String fname = bIn.readLine();
  68. File f1 = new File(fname);
  69. f1.createNewFile();
  70. FileOutputStream fos = new FileOutputStream(f1);
  71. DataInputStream dis = new DataInputStream(socket2.getInputStream());
  72. for (int i = 0; i < len; i++) {
  73. fos.write(dis.read());
  74. }
  75. fos.close();
  76. } else if (rep.equals("File does not exist")) {
  77. output.println("Sorry, but the file was neither found in the proxy server or the main server or the name is wrong.");
  78. }
  79.  
  80. }
  81. }
  82. File f2 = new File("javalogo.png");
  83. if (f2.exists() == true) {
  84. int size = (int) f2.length();
  85. output.println(s);
  86. output.println(size);
  87. output.println("javalogo.png");
  88. DataInputStream bytefile = new DataInputStream(new BufferedInputStream(new FileInputStream(f2)));
  89. while (bytefile.available() != 0) {
  90. x = bytefile.readByte();
  91. sending.write(x);
  92. }
  93. }
  94. } else {
  95. System.out.println(s1);
  96. output.println(s1);
  97. }
  98. }
  99. }
  100. incoming.close();
  101.  
  102. }
  103. output.println("Connecting to main server");
  104. } catch (Exception ex) {
  105. System.out.println(ex.getMessage());
  106. }
  107. }
  108. }
  109.  
  110. import java.net.*;
  111. import java.io.*;
  112.  
  113.  
  114. public class Client {
  115.  
  116. public static void main(String[] args) throws Exception {
  117.  
  118. Socket socket1 = new Socket("127.0.0.1", 8000);
  119.  
  120. BufferedReader bUsr = new BufferedReader(new InputStreamReader(System.in));
  121. PrintWriter pOut = new PrintWriter(socket1.getOutputStream(), true);
  122. BufferedReader bIn = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
  123. String cmd;
  124. String rep;
  125. while (true) {
  126. cmd = bUsr.readLine();
  127. pOut.println(cmd);
  128.  
  129. System.out.println(rep = bIn.readLine());
  130.  
  131. if (cmd.equals("BYE") || cmd.equals("END"))
  132. break;
  133. else if (rep.equals("HTTP 1.0 200 Document follows")) {
  134. int len = Integer.parseInt(bIn.readLine());
  135. String fname = bIn.readLine();
  136. File f = new File(fname);
  137. f.createNewFile();
  138. FileOutputStream fos = new FileOutputStream(f);
  139. DataInputStream dis = new DataInputStream(socket1.getInputStream());
  140. for (int i = 0; i < len; i++) {
  141. fos.write(dis.read());
  142. }
  143.  
  144. fos.close();
  145. System.out.println("Success");
  146.  
  147. } else if (rep.equals("Connecting to main server")) {
  148. Socket socket1 = new Socket("127.0.0.1", 8100);
  149. BufferedReader bUsr = new BufferedReader(new InputStreamReader(System.in));
  150. PrintWriter pOut = new PrintWriter(socket1.getOutputStream(), true);
  151. BufferedReader bIn = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
  152. String cmd;
  153. String rep;
  154. while (true) {
  155. cmd = bUsr.readLine();
  156. pOut.println(cmd);
  157.  
  158. System.out.println(rep = bIn.readLine());
  159.  
  160. if (cmd.equals("BYE") || cmd.equals("END"))
  161. break;
  162. else if (rep.equals("HTTP 1.0 200 Document follows")) {
  163. int len = Integer.parseInt(bIn.readLine());
  164. String fname = bIn.readLine();
  165. File f = new File(fname);
  166. f.createNewFile();
  167. FileOutputStream fos = new FileOutputStream(f);
  168. DataInputStream dis = new DataInputStream(socket1.getInputStream());
  169. for (int i = 0; i < len; i++) {
  170. fos.write(dis.read());
  171. }
  172.  
  173. fos.close();
  174. System.out.println("Success");
  175. }
  176. }
  177. }
  178.  
  179. bIn.close();
  180. pOut.close();
  181. socket1.close();
  182. }
  183. }
Add Comment
Please, Sign In to add comment