wieruowq

ClientRecieve.java

Nov 1st, 2017
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3. public class ClientRecieve extends Thread
  4. {
  5. int filePortNumber = 0;
  6. Socket servSocket;
  7. Socket fileSocket;
  8. boolean m_bRunThread = true;
  9. boolean ServerOn = true;
  10. int once = 0;
  11. String fileNameGlobal = "";
  12. public ClientRecieve(Socket s) throws FileNotFoundException
  13. {
  14. super();
  15. this.servSocket = s;
  16. }
  17. public void run()
  18. {
  19. try
  20. {
  21. DataInputStream messagesFromServer = new DataInputStream(servSocket.getInputStream());
  22. boolean stop = false;
  23. DataOutputStream output = new DataOutputStream(servSocket.getOutputStream());
  24.  
  25. while (!stop)
  26. {
  27. String response = messagesFromServer.readUTF();
  28. if(response!=null && !response.isEmpty())
  29. {
  30. if(response.matches(".*\\d+.*"))
  31. {
  32. System.out.println("got a number, it is: " + response);
  33. }
  34. else if(response.contains("`"))
  35. {
  36. String[] parts = response.split("`");
  37. String part1 = parts[0];
  38. //part1 contains file name....now send the file or file size
  39. File file= new File(part1);
  40. if ( file.exists() && file.canRead() )
  41. {
  42. long file_size= file.length();
  43. System.out.println("File size is: " + file_size);
  44. if ( file_size > 0 )
  45. {
  46. output.flush();
  47. output.writeLong( file_size );
  48. output.flush();
  49. }
  50.  
  51. }
  52. else
  53. {
  54. output.flush();
  55. output.writeLong( 0L );
  56. output.flush();
  57. }
  58. FileInputStream file_input= new FileInputStream(file);
  59. System.out.println( "Transmitting file: " + part1);
  60. byte[] file_buffer= new byte[1500];
  61. int number_read;
  62. while( (number_read= file_input.read( file_buffer )) != -1 )
  63. {
  64. output.write( file_buffer, 0, number_read );
  65. }
  66. output.flush();
  67. System.out.println("File " + part1 + " sent with " + number_read + " bytes read.");
  68.  
  69. }
  70. else if(response.equals("x") || response.equals("X"))
  71. {
  72. stop = true;
  73. System.exit(0);
  74. break;
  75. }
  76. else if(!response.contains("`") && !response.equals("x") && !response.equals("X"))
  77. {
  78. System.out.println(response);
  79. }
  80. }
  81. else if(response == null)
  82. {
  83. stop = true;
  84. System.exit(0);
  85. break;
  86. }
  87. }
  88. }
  89. catch (IOException e)
  90. {
  91. e.printStackTrace();
  92. }
  93. finally
  94. {
  95. }
  96. }
  97. void OutputOptions()
  98. {
  99. System.out.println("Enter an option ('m', 'f', 'x'): ");
  100. System.out.println("(M)essage (send)");
  101. System.out.println("(F)ile (request) ");
  102. System.out.println("e(X)it ");
  103. }
  104. public int strToInt( String str )
  105. {
  106. int i = 0;
  107. int num = 0;
  108. boolean isNeg = false;
  109. if (str.charAt(0) == '-')
  110. {
  111. isNeg = true;
  112. i = 1;
  113. }
  114.  
  115. while( i < str.length())
  116. {
  117. num *= 10;
  118. num += str.charAt(i++) - '0';
  119. }
  120. if (isNeg)
  121. {
  122. num = -num;
  123. }
  124. return num;
  125. }
  126. }
Add Comment
Please, Sign In to add comment