Advertisement
Guest User

Untitled

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