Advertisement
Guest User

Untitled

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