Advertisement
Guest User

Untitled

a guest
Nov 11th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.99 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. if ("INFO ".equals(writeDown)) {
  231. while (!writeDown.endsWith("\r\n")) {
  232. byte cc = inFromClient.readByte();
  233. writeDown += new String(new byte[]{cc}, "US-ASCII");
  234. }
  235.  
  236.  
  237. this.info += writeDown.substring(0, writeDown.length() - 2);
  238. if(info.contains("\r")||info.contains("\n")){
  239. outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
  240. c.getOutputStream().flush();
  241. c.getOutputStream().close();}
  242. System.out.println("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
  243. outToClient.write("202 OK\r\n".getBytes());
  244.  
  245. }
  246.  
  247. File fnew = new File("foto0.png");
  248.  
  249. if ("FOTO ".equals(writeDown)) {
  250. BufferedImage originalImage = ImageIO.read(fnew);
  251. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  252. ImageIO.write(originalImage, "png", baos);
  253. while (!writeDown.endsWith(" ")) {
  254.  
  255. byte cc = inFromClient.readByte();
  256. writeDown += new String(new byte[]{cc}, "US-ASCII");
  257. len += new String(new byte[]{cc}, "US-ASCII");
  258.  
  259. }
  260. int intLen = Integer.parseInt(len);
  261.  
  262. boolean one = true;
  263.  
  264. // String m = "";
  265. // while (one) {
  266. // byte cc = inFromClient.readByte();
  267. // writeDown += new String(new byte[]{cc}, "US-ASCII");
  268. // m += new String(new byte[]{cc}, "US-ASCII");
  269. // one = false;
  270. // }
  271. // if (!" ".equals(m)) {
  272. // outToClient.writeBytes("501 SYNTAX ERROR\r\n");
  273. // c.close();
  274. // return;
  275. // }
  276.  
  277. for (int i = 0; i < intLen; i++) {
  278. byte[] b = new byte[1];
  279. foto = "";
  280. byte cc = inFromClient.readByte();
  281. writeDown += new String(new byte[]{cc}, "US-ASCII");
  282. foto += new String(new byte[]{cc}, "US-ASCII");
  283. fotoLen += Integer.parseInt(foto);
  284. b = baos.toByteArray();
  285. }
  286.  
  287. byte[] bytes = new byte[4];
  288. for (int i = 0; i < 4; i++) {
  289. byte cc = inFromClient.readByte();
  290. writeDown += new String(new byte[]{cc}, "US-ASCII");
  291. bytes[i] = cc;
  292. //ks += new String(new byte[]{cc}, "US-ASCII");
  293. }
  294. int value = 0;
  295. for (int i = 0; i <= 3; i++) {
  296. value = (value << 8) + (bytes[i] & 0xFF);
  297. }
  298.  
  299. if (fotoLen != value) {
  300. //outToClient.writeBytes("300 BAD CHECKSUM\r\n");
  301. outToClient.write("300 BAD CHECKSUM\r\n".getBytes());
  302. fnew.delete();
  303.  
  304.  
  305. }
  306. if (fotoLen == value) {
  307. //outToClient.writeBytes("202 OK\r\n");
  308. outToClient.write("202 OK\r\n".getBytes());
  309. ArrayList ar = new ArrayList();
  310. Random rn = new Random();
  311. int r = rn.nextInt(999) + 1;
  312.  
  313. while (!ar.contains(r)) {
  314. r++;
  315. if (r > 999) {
  316. r = 1;
  317. }
  318.  
  319. }
  320. ar.add(r);
  321. File f = new File("foto" + r + ".png");
  322. fnew.renameTo(f);
  323. //c.close();
  324. }
  325.  
  326. }
  327.  
  328. // writeDown = "";
  329. // while (!writeDown.endsWith("\r\n")) {
  330. // byte cc = inFromClient.readByte();
  331. // writeDown += new String(new byte[]{cc}, "US-ASCII");
  332. // }
  333. // this.info = writeDown.substring(0, writeDown.length() - 2);
  334. // if (!this.info.startsWith("INFO ")) {
  335. // outToClient.writeBytes("501 SYNTAX ERROR\r\n");
  336. // c.close();
  337. // return;
  338. // }
  339. //
  340. // System.out.println("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
  341. // outToClient.writeBytes("202 OK\r\n");
  342. }
  343.  
  344. //c.close();
  345. } catch (IOException ex) {
  346. System.err.println("Couldn't get I/O.");
  347.  
  348. System.exit(1);
  349.  
  350. }
  351.  
  352. }
  353.  
  354. }
  355.  
  356. class Server {
  357.  
  358. public static void svr_main(int port) throws IOException {
  359. ServerSocket serverSocket = null;
  360. try {
  361. serverSocket = new ServerSocket(port);
  362. } catch (IOException e) {
  363. System.err.println("Could not listen on port: " + port);
  364. System.exit(1);
  365. }
  366. Socket clientSocket = null;
  367. while (true) {
  368. try {
  369. clientSocket = serverSocket.accept();
  370. } catch (IOException e) {
  371. serverSocket.close();
  372. System.err.println("Accept failed.");
  373. System.exit(1);
  374. }
  375. System.out.println("client accepted from: " + clientSocket.getInetAddress()
  376. + ":" + clientSocket.getPort());
  377. ClientListener cl = new ClientListener(clientSocket);
  378. Thread thread = new Thread(cl);
  379. thread.start();
  380. }
  381. }
  382. }
  383.  
  384. public class Robot {
  385.  
  386. public static void main(String[] args) throws IOException {
  387. if (args.length == 0) {
  388. System.err.println("Server: java robot.Robot <port>");
  389. System.exit(1);
  390. }
  391. System.out.println("Starting server...\n");
  392. Server server = new Server();
  393. server.svr_main(Integer.parseInt(args[0]));
  394. }
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement