Advertisement
Guest User

Untitled

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