Advertisement
Guest User

Untitled

a guest
Nov 11th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.27 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. //boolean closeIt = false;
  33.  
  34. @Override
  35. public void run() {
  36.  
  37. DataInputStream inFromClient;
  38. try {
  39.  
  40. inFromClient = new DataInputStream(c.getInputStream());
  41. OutputStream outToClient = c.getOutputStream();
  42. //DataOutputStream outToClient = new DataOutputStream(c.getOutputStream());
  43. outToClient.write("200 LOGIN\r\n".getBytes());
  44.  
  45.  
  46. // Timer t = new Timer(45, new ActionListener() {
  47. // @Override
  48. // public void actionPerformed(ActionEvent e) {
  49. // DataOutputStream outToClient;
  50. // try {
  51. // outToClient = new DataOutputStream(c.getOutputStream());
  52. // outToClient.writeBytes("502 TIMEOUT\r\n");
  53. // c.close();
  54. // } catch (IOException ex) {
  55. // Logger.getLogger(ClientListener.class.getName()).log(Level.SEVERE, null, ex);
  56. // }
  57. //
  58. // }
  59. // });
  60.  
  61.  
  62.  
  63.  
  64. String writeDown = "";
  65. List<Byte> two = new ArrayList<Byte>();
  66. List<Byte> user = new ArrayList<Byte>();
  67. List<Byte> pass = new ArrayList<Byte>();
  68. two.add(Byte.MIN_VALUE);
  69. two.add(Byte.MIN_VALUE);
  70.  
  71. while (!(two.get(0)==13&&two.get(1)==10)) {
  72. byte cc = inFromClient.readByte();
  73. two.add(cc);
  74.  
  75. user.add(cc);
  76. if(two.size()>2){two.remove(0);}
  77. writeDown += new String(new byte[]{cc}, "US-ASCII");
  78. }
  79. int sizeUser = user.size();
  80. user.remove(sizeUser-1);
  81. user.remove(sizeUser-2);
  82. //this.userName = writeDown.substring(0, writeDown.length() - 2);
  83.  
  84. System.out.println("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
  85.  
  86. //outToClient.writeBytes("201 PASSWORD\r\n");
  87. outToClient.write("201 PASSWORD\r\n".getBytes());
  88. writeDown = "";
  89.  
  90.  
  91. two.clear();
  92. two.add(Byte.MIN_VALUE);
  93. two.add(Byte.MIN_VALUE);
  94. while (!(two.get(0)==13&&two.get(1)==10)) {
  95. byte cc = inFromClient.readByte();
  96. two.add(cc);
  97. if(cc!=13&&cc!=10){
  98. pass.add(cc);}
  99. if(two.size()>2){two.remove(0);}
  100. writeDown += new String(new byte[]{cc}, "US-ASCII");
  101. }
  102.  
  103. this.password = writeDown.substring(0, writeDown.length() - 2);
  104. System.out.println("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
  105.  
  106. byte[] robot = {user.get(0),user.get(1),user.get(2),user.get(3),user.get(4)};
  107. String robotStr =new String(robot);
  108. if (!"Robot".equals(robotStr)) {
  109. //outToClient.writeBytes("500 LOGIN FAILED\r\n");
  110. outToClient.write("500 LOGIN FAILED\r\n".getBytes());
  111. c.getOutputStream().flush();
  112. c.getOutputStream().close();
  113. return;
  114. }
  115.  
  116. Integer passwordAsItMustBe = 0;
  117. //byte[] unb = this.userName.getBytes("US-ASCII");
  118. for (byte b : user) {
  119. passwordAsItMustBe += b;
  120. }
  121.  
  122.  
  123. // Integer passwordAsItIs =0;
  124. // for (byte s : pass) {
  125. // passwordAsItIs += s;
  126. // }
  127.  
  128. if (!password.equals(passwordAsItMustBe.toString())) {
  129. //outToClient.writeBytes("500 LOGIN FAILED\r\n");
  130. outToClient.write("500 LOGIN FAILED\r\n".getBytes());
  131. c.getOutputStream().flush();
  132. c.getOutputStream().close();
  133. return;
  134. }
  135. //outToClient.writeBytes("202 OK\r\n");
  136. outToClient.write("202 OK\r\n".getBytes());
  137. while (true) {
  138. // byte[] b = new byte[4];
  139. // byte cc = (byte) inFromClient.read(b);
  140. // writeDown += new String(new byte[]{cc}, "US-ASCII");
  141.  
  142. writeDown = "";
  143.  
  144. ArrayList<Byte> infoArray = new ArrayList();
  145.  
  146. for(int j=0;j<1;j++){
  147. byte cc = inFromClient.readByte();
  148. writeDown += new String(new byte[]{cc}, "US-ASCII");
  149. if(!writeDown.equals("I")&&!writeDown.equals("F")){
  150. //outToClient.writeBytes("501 SYNTAX ERROR\r\n");
  151. outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
  152. c.getOutputStream().flush();
  153. c.getOutputStream().close();
  154. //return;
  155. }
  156. }
  157.  
  158.  
  159. for(int j=1;j<2;j++){
  160. byte cc = inFromClient.readByte();
  161. writeDown += new String(new byte[]{cc}, "US-ASCII");
  162. if(!writeDown.equals("IN")&&!writeDown.equals("FO")){
  163. outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
  164. c.getOutputStream().flush();
  165. c.getOutputStream().close();
  166. //return;
  167. }
  168. }
  169.  
  170.  
  171.  
  172. for(int j=2;j<3;j++){
  173. byte cc = inFromClient.readByte();
  174. writeDown += new String(new byte[]{cc}, "US-ASCII");
  175. if(!writeDown.equals("INF")&&!writeDown.equals("FOT")){
  176. outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
  177. c.getOutputStream().flush();
  178. c.getOutputStream().close();
  179. }
  180. }
  181.  
  182.  
  183. for(int j=3;j<4;j++){
  184. byte cc = inFromClient.readByte();
  185. writeDown += new String(new byte[]{cc}, "US-ASCII");
  186. if(!writeDown.equals("INFO")&&!writeDown.equals("FOTO")){
  187. outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
  188. c.getOutputStream().flush();
  189. c.getOutputStream().close();
  190. //return;
  191. }
  192. }
  193.  
  194.  
  195.  
  196. for(int j=4;j<5;j++){
  197. byte cc = inFromClient.readByte();
  198. writeDown += new String(new byte[]{cc}, "US-ASCII");
  199. if(!writeDown.equals("INFO ")&&!writeDown.equals("FOTO ")){
  200. outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
  201. c.getOutputStream().flush();
  202. c.getOutputStream().close();
  203. //return;
  204. }
  205. }
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215. // for (int j = 0; j < 5; j++) {
  216. // byte cc = inFromClient.readByte();
  217. // writeDown += new String(new byte[]{cc}, "US-ASCII");
  218. // a=new String(new byte[]{cc}, "US-ASCII");
  219. //
  220. //
  221. // }
  222.  
  223. // if (!"INFO ".equals(writeDown) && !"FOTO ".equals(writeDown)) {
  224. // outToClient.writeBytes("501 SYNTAX ERROR\r\n");
  225. // outToClient.flush();
  226. // c.close();
  227. // //return;
  228. // }
  229.  
  230.  
  231. // List<Byte> rnr = new ArrayList<Byte>();
  232.  
  233. if ("INFO ".equals(writeDown)) {
  234. while (!writeDown.endsWith("\r\n")) {
  235. byte cc = inFromClient.readByte();
  236. //rnr.add(cc);
  237. writeDown += new String(new byte[]{cc}, "US-ASCII");
  238. }
  239. // int sizecc = rnr.size();
  240. // rnr.remove(sizecc-1);
  241. // rnr.remove(sizecc-2);
  242. // if(rnr.contains("\r")||rnr.contains("\n")){
  243. //
  244. // outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
  245. // c.getOutputStream().flush();
  246. // c.getOutputStream().close();
  247. // }
  248. this.info += writeDown.substring(0, writeDown.length() - 2);
  249.  
  250. System.out.println("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
  251. outToClient.write("202 OK\r\n".getBytes());
  252.  
  253. }
  254.  
  255. File fnew = new File("foto0.png");
  256.  
  257. if ("FOTO ".equals(writeDown)) {
  258. BufferedImage originalImage = ImageIO.read(fnew);
  259. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  260. ImageIO.write(originalImage, "png", baos);
  261. while (!writeDown.endsWith(" ")) {
  262.  
  263. byte cc = inFromClient.readByte();
  264. writeDown += new String(new byte[]{cc}, "US-ASCII");
  265. len += new String(new byte[]{cc}, "US-ASCII");
  266.  
  267. }
  268. int intLen = Integer.parseInt(len);
  269.  
  270. boolean one = true;
  271.  
  272. // String m = "";
  273. // while (one) {
  274. // byte cc = inFromClient.readByte();
  275. // writeDown += new String(new byte[]{cc}, "US-ASCII");
  276. // m += new String(new byte[]{cc}, "US-ASCII");
  277. // one = false;
  278. // }
  279. // if (!" ".equals(m)) {
  280. // outToClient.writeBytes("501 SYNTAX ERROR\r\n");
  281. // c.close();
  282. // return;
  283. // }
  284.  
  285. for (int i = 0; i < intLen; i++) {
  286. byte[] b = new byte[1];
  287. foto = "";
  288. byte cc = inFromClient.readByte();
  289. writeDown += new String(new byte[]{cc}, "US-ASCII");
  290. foto += new String(new byte[]{cc}, "US-ASCII");
  291. fotoLen += Integer.parseInt(foto);
  292. b = baos.toByteArray();
  293. }
  294.  
  295. byte[] bytes = new byte[4];
  296. for (int i = 0; i < 4; i++) {
  297. byte cc = inFromClient.readByte();
  298. writeDown += new String(new byte[]{cc}, "US-ASCII");
  299. bytes[i] = cc;
  300. //ks += new String(new byte[]{cc}, "US-ASCII");
  301. }
  302. int value = 0;
  303. for (int i = 0; i <= 3; i++) {
  304. value = (value << 8) + (bytes[i] & 0xFF);
  305. }
  306.  
  307. if (fotoLen != value) {
  308. //outToClient.writeBytes("300 BAD CHECKSUM\r\n");
  309. outToClient.write("300 BAD CHECKSUM\r\n".getBytes());
  310. fnew.delete();
  311.  
  312.  
  313. }
  314. if (fotoLen == value) {
  315. //outToClient.writeBytes("202 OK\r\n");
  316. outToClient.write("202 OK\r\n".getBytes());
  317. ArrayList ar = new ArrayList();
  318. Random rn = new Random();
  319. int r = rn.nextInt(999) + 1;
  320.  
  321. while (!ar.contains(r)) {
  322. r++;
  323. if (r > 999) {
  324. r = 1;
  325. }
  326.  
  327. }
  328. ar.add(r);
  329. File f = new File("foto" + r + ".png");
  330. fnew.renameTo(f);
  331. //c.close();
  332. }
  333.  
  334. }
  335.  
  336. // writeDown = "";
  337. // while (!writeDown.endsWith("\r\n")) {
  338. // byte cc = inFromClient.readByte();
  339. // writeDown += new String(new byte[]{cc}, "US-ASCII");
  340. // }
  341. // this.info = writeDown.substring(0, writeDown.length() - 2);
  342. // if (!this.info.startsWith("INFO ")) {
  343. // outToClient.writeBytes("501 SYNTAX ERROR\r\n");
  344. // c.close();
  345. // return;
  346. // }
  347. //
  348. // System.out.println("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
  349. // outToClient.writeBytes("202 OK\r\n");
  350. }
  351.  
  352. //c.close();
  353. } catch (IOException ex) {
  354. System.err.println("Couldn't get I/O.");
  355.  
  356. System.exit(1);
  357.  
  358. }
  359.  
  360. }
  361.  
  362. }
  363.  
  364. class Server {
  365.  
  366. public static void svr_main(int port) throws IOException {
  367. ServerSocket serverSocket = null;
  368. try {
  369. serverSocket = new ServerSocket(port);
  370. } catch (IOException e) {
  371. System.err.println("Could not listen on port: " + port);
  372. System.exit(1);
  373. }
  374. Socket clientSocket = null;
  375. while (true) {
  376. try {
  377. clientSocket = serverSocket.accept();
  378. } catch (IOException e) {
  379. serverSocket.close();
  380. System.err.println("Accept failed.");
  381. System.exit(1);
  382. }
  383. System.out.println("client accepted from: " + clientSocket.getInetAddress()
  384. + ":" + clientSocket.getPort());
  385. ClientListener cl = new ClientListener(clientSocket);
  386. Thread thread = new Thread(cl);
  387. thread.start();
  388. }
  389. }
  390. }
  391.  
  392. public class Robot {
  393.  
  394. public static void main(String[] args) throws IOException {
  395. if (args.length == 0) {
  396. System.err.println("Server: java robot.Robot <port>");
  397. System.exit(1);
  398. }
  399. System.out.println("Starting server...\n");
  400. Server server = new Server();
  401. server.svr_main(Integer.parseInt(args[0]));
  402. }
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement