Advertisement
Guest User

Untitled

a guest
Nov 10th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.95 KB | None | 0 0
  1. //Alexander Maksimov
  2. package robot;
  3.  
  4. import java.awt.image.BufferedImage;
  5. import java.net.*;
  6. import java.io.*;
  7. import static java.lang.Math.random;
  8. import static java.lang.StrictMath.random;
  9. import java.util.ArrayList;
  10. import java.util.Random;
  11. import javax.imageio.ImageIO;
  12.  
  13. class ClientListener implements Runnable {
  14.  
  15. Socket c;
  16.  
  17. public ClientListener(Socket c) {
  18. this.c = c;
  19. }
  20. String userName;
  21. String password;
  22. String info;
  23. String len;
  24. String foto;
  25. String FILE = "foto0.png";
  26. Integer fotoLen = 0;
  27. String ks;
  28.  
  29. @Override
  30. public void run() {
  31. DataInputStream inFromClient;
  32. try {
  33. inFromClient = new DataInputStream(c.getInputStream());
  34. DataOutputStream outToClient = new DataOutputStream(c.getOutputStream());
  35. outToClient.writeBytes("200 LOGIN\r\n");
  36.  
  37. String writeDown = "";
  38. while (!writeDown.endsWith("\r\n")) {
  39. byte cc = inFromClient.readByte();
  40. writeDown += new String(new byte[]{cc}, "US-ASCII");
  41. }
  42. this.userName = writeDown.substring(0, writeDown.length() - 2);
  43. System.out.println("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
  44.  
  45. outToClient.writeBytes("201 PASSWORD\r\n");
  46.  
  47. writeDown = "";
  48. while (!writeDown.endsWith("\r\n")) {
  49. byte cc = inFromClient.readByte();
  50. writeDown += new String(new byte[]{cc}, "US-ASCII");
  51. }
  52. this.password = writeDown.substring(0, writeDown.length() - 2);
  53. System.out.println("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
  54.  
  55. if (!this.userName.startsWith("Robot")) {
  56. outToClient.writeBytes("500 LOGIN FAILED\r\n");
  57. c.close();
  58. return;
  59. }
  60.  
  61. Integer passwordAsItMustBe = 0;
  62. byte[] unb = this.userName.getBytes("US-ASCII");
  63. for (byte b : unb) {
  64. passwordAsItMustBe += b;
  65. }
  66. if (!this.password.equals(passwordAsItMustBe.toString())) {
  67. outToClient.writeBytes("500 LOGIN FAILED\r\n");
  68. c.close();
  69. return;
  70. }
  71. outToClient.writeBytes("202 OK\r\n");
  72.  
  73. while (true) {
  74. // byte[] b = new byte[4];
  75. // byte cc = (byte) inFromClient.read(b);
  76. // writeDown += new String(new byte[]{cc}, "US-ASCII");
  77.  
  78. writeDown = "";
  79.  
  80. for (int j = 0; j < 5; j++) {
  81. byte cc = inFromClient.readByte();
  82. writeDown += new String(new byte[]{cc}, "US-ASCII");
  83. }
  84.  
  85. if (!"INFO ".equals(writeDown) || !"FOTO ".equals(writeDown)) {
  86. outToClient.writeBytes("501 SYNTAX ERROR\r\n");
  87. c.close();
  88. return;
  89. }
  90.  
  91. if ("INFO ".equals(writeDown)) {
  92. while (!writeDown.endsWith("\r\n")) {
  93. byte cc = inFromClient.readByte();
  94. writeDown += new String(new byte[]{cc}, "US-ASCII");
  95. }
  96. this.info = writeDown.substring(0, writeDown.length() - 2);
  97. System.out.println("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
  98. outToClient.writeBytes("202 OK\r\n");
  99. }
  100.  
  101. File fnew = new File("foto0.png");
  102. BufferedImage originalImage = ImageIO.read(fnew);
  103. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  104. ImageIO.write(originalImage, "png", baos);
  105. if ("FOTO ".equals(writeDown)) {
  106. while (!writeDown.endsWith(" ")) {
  107.  
  108. byte cc = inFromClient.readByte();
  109. writeDown += new String(new byte[]{cc}, "US-ASCII");
  110. len += new String(new byte[]{cc}, "US-ASCII");
  111.  
  112. }
  113. int intLen = Integer.parseInt(len);
  114.  
  115. boolean one = true;
  116. String mez = " ";
  117. String m = "";
  118. while (one) {
  119. byte cc = inFromClient.readByte();
  120. writeDown += new String(new byte[]{cc}, "US-ASCII");
  121. m += new String(new byte[]{cc}, "US-ASCII");
  122. one = false;
  123. }
  124. if (m != mez) {
  125. outToClient.writeBytes("501 SYNTAX ERROR\r\n");
  126. c.close();
  127. return;
  128. }
  129.  
  130. for (int i = 0; i < intLen; i++) {
  131. byte[] b = new byte[1];
  132. foto = "";
  133. byte cc = inFromClient.readByte();
  134. writeDown += new String(new byte[]{cc}, "US-ASCII");
  135. foto += new String(new byte[]{cc}, "US-ASCII");
  136. fotoLen += Integer.parseInt(foto);
  137. b = baos.toByteArray();
  138. }
  139.  
  140. byte[] bytes = new byte[4];
  141. for (int i = 0; i < 4; i++) {
  142. byte cc = inFromClient.readByte();
  143. writeDown += new String(new byte[]{cc}, "US-ASCII");
  144. bytes[i] = cc;
  145. //ks += new String(new byte[]{cc}, "US-ASCII");
  146. }
  147. int value = 0;
  148. for (int i = 0; i <= 3; i++) {
  149. value = (value << 8) + (bytes[i] & 0xFF);
  150. }
  151.  
  152. if (fotoLen != value) {
  153. outToClient.writeBytes("300 BAD CHECKSUM\r\n");
  154. fnew.delete();
  155. c.close();
  156. return;
  157. }
  158. if (fotoLen == value) {
  159. outToClient.writeBytes("202 OK\r\n");
  160. ArrayList ar = new ArrayList();
  161. Random rn = new Random();
  162. int r = rn.nextInt(999) + 1;;
  163.  
  164. while (!ar.contains(r)) {
  165. r ++;
  166. if(r>999){r=1;}
  167.  
  168.  
  169. }
  170. ar.add(r);
  171. File f = new File("foto" + r + ".png");
  172. fnew.renameTo(f);
  173. c.close();
  174. }
  175.  
  176. }
  177.  
  178. // writeDown = "";
  179. // while (!writeDown.endsWith("\r\n")) {
  180. // byte cc = inFromClient.readByte();
  181. // writeDown += new String(new byte[]{cc}, "US-ASCII");
  182. // }
  183. // this.info = writeDown.substring(0, writeDown.length() - 2);
  184. // if (!this.info.startsWith("INFO ")) {
  185. // outToClient.writeBytes("501 SYNTAX ERROR\r\n");
  186. // c.close();
  187. // return;
  188. // }
  189. //
  190. // System.out.println("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
  191. // outToClient.writeBytes("202 OK\r\n");
  192. }
  193.  
  194. //c.close();
  195. } catch (IOException ex) {
  196. System.err.println("Couldn't get I/O.");
  197. System.exit(1);
  198. }
  199.  
  200. }
  201.  
  202. }
  203.  
  204. class Server {
  205.  
  206. public static void svr_main(int port) throws IOException {
  207. ServerSocket serverSocket = null;
  208. try {
  209. serverSocket = new ServerSocket(port);
  210. } catch (IOException e) {
  211. System.err.println("Could not listen on port: " + port);
  212. System.exit(1);
  213. }
  214. Socket clientSocket = null;
  215. while (true) {
  216. try {
  217. clientSocket = serverSocket.accept();
  218. } catch (IOException e) {
  219. serverSocket.close();
  220. System.err.println("Accept failed.");
  221. System.exit(1);
  222. }
  223. System.out.println("client accepted from: " + clientSocket.getInetAddress()
  224. + ":" + clientSocket.getPort());
  225. ClientListener cl = new ClientListener(clientSocket);
  226. Thread thread = new Thread(cl);
  227. thread.start();
  228. }
  229. }
  230. }
  231.  
  232. public class Robot {
  233.  
  234. public static void main(String[] args) throws IOException {
  235. if (args.length == 0) {
  236. System.err.println("Server: java robot.Robot <port>");
  237. System.exit(1);
  238. }
  239. System.out.println("Starting server...\n");
  240. Server server = new Server();
  241. server.svr_main(Integer.parseInt(args[0]));
  242. }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement