Advertisement
Guest User

Untitled

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