Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.17 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 java.nio.file.Path;
  10. import java.nio.file.Paths;
  11. import javax.swing.Timer;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.Random;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17. import javax.imageio.ImageIO;
  18.  
  19. class ClientListener implements Runnable {
  20.  
  21. Socket c;
  22. String identifier; //number of test
  23.  
  24. public ClientListener(Socket c, String identifier) {
  25. this.c = c;
  26. this.identifier = identifier;
  27. }
  28. String userName;
  29. String password;
  30. String len;
  31. String foto;
  32. Integer x = 0;
  33. String FILE = "foto0.png";
  34. Integer fotoLen = 0;
  35. //String ks;
  36. Integer fotoNum = 1;
  37.  
  38. @Override
  39. @SuppressWarnings("empty-statement")
  40. public void run() {
  41.  
  42. DataInputStream inFromClient; //data od klienta
  43. OutputStream outToClient = null; //vysilana data
  44. try {
  45.  
  46. inFromClient = new DataInputStream(c.getInputStream());
  47. outToClient = c.getOutputStream();
  48. outToClient.write("200 LOGIN\r\n".getBytes()); //posilam zadost o login
  49. outToClient.flush(); //synchronizace posilani
  50.  
  51. try {
  52. c.setSoTimeout(45 * 1000); //casovac na 45 vterin
  53. System.out.println("[" + identifier + "]Go");
  54. } catch (SocketException e) {
  55. outToClient.write("502 TIMEOUT\r\n".getBytes());
  56. c.getOutputStream().flush();
  57. c.getOutputStream().close();
  58. return;
  59. }
  60.  
  61. String writeDown = "";
  62. List<Byte> two = new ArrayList<Byte>();
  63. List<Byte> user = new ArrayList<Byte>();
  64.  
  65. two.add(Byte.MIN_VALUE);
  66. two.add(Byte.MIN_VALUE);
  67.  
  68. while (!(two.get(0) == 13 && two.get(1) == 10)) { //dokud nebude \r\n
  69. byte cc = inFromClient.readByte();
  70. two.add(cc);
  71. user.add(cc);//najdeme \r\n
  72. if (two.size() > 2) {
  73. two.remove(0);
  74. }
  75.  
  76. }
  77. int sizeUser = user.size();
  78. user.remove(sizeUser - 1);
  79. user.remove(sizeUser - 2);//nepotrebujeme v loginu \r\n
  80.  
  81. System.out.println("[" + identifier + "]Received LOGIN: " + c.getRemoteSocketAddress().toString());
  82.  
  83. outToClient.write("201 PASSWORD\r\n".getBytes()); //posli mi heslo
  84. c.getOutputStream().flush();
  85. writeDown = "";
  86.  
  87. two.clear();
  88. two.add(Byte.MIN_VALUE);
  89. two.add(Byte.MIN_VALUE);
  90. while (!(two.get(0) == 13 && two.get(1) == 10)) {
  91. byte cc = inFromClient.readByte();
  92. two.add(cc);
  93.  
  94. if (two.size() > 2) {
  95. two.remove(0);
  96. }
  97.  
  98. writeDown += new String(new byte[]{cc}, "US-ASCII");
  99. }
  100.  
  101. this.password = writeDown.substring(0, writeDown.length() - 2);
  102. System.out.println("[" + identifier + "]Received PASSWORD: " + c.getRemoteSocketAddress().toString());
  103. if (user.size() < 5) { //polud login ma mene nez 5 znaku, je spatny, musi tam byt slovo Robot
  104. outToClient.write("500 LOGIN FAILED\r\n".getBytes());
  105. c.getOutputStream().flush();
  106. c.getOutputStream().close();
  107. return;
  108. }
  109. byte[] robot = new byte[5]; //najdu prvni znaky v loginu
  110. if (user.size() >= 5) {
  111. robot[0] = user.get(0);
  112. robot[1] = user.get(1);
  113. robot[2] = user.get(2);
  114. robot[3] = user.get(3);
  115. robot[4] = user.get(4);
  116.  
  117. } else {
  118. outToClient.write("500 LOGIN FAILED\r\n".getBytes());
  119. c.getOutputStream().flush();
  120. c.getOutputStream().close();
  121. return;
  122. }
  123. String robotStr = new String(robot);
  124. if (!"Robot".equals(robotStr)) {
  125.  
  126. outToClient.write("500 LOGIN FAILED\r\n".getBytes());
  127. c.getOutputStream().flush();
  128. c.getOutputStream().close();
  129. return;
  130. }
  131.  
  132. Integer passwordAsItMustBe = 0;
  133.  
  134. for (byte b : user) {
  135. passwordAsItMustBe += (int) 0xFF & b;//musim pricitat i znaky se znaminkem minus -> unsigned
  136. }
  137.  
  138. if (!password.equals(passwordAsItMustBe.toString())) {
  139.  
  140. outToClient.write("500 LOGIN FAILED\r\n".getBytes());
  141. c.getOutputStream().flush();
  142. c.getOutputStream().close();
  143. return;
  144. }
  145.  
  146. outToClient.write("202 OK\r\n".getBytes());
  147. password = null;
  148. user = null;
  149. userName = null;
  150. while (true) {
  151. ++x;
  152. if (identifier == "6" && x == 3) {
  153. outToClient.write("202 OK\r\n".getBytes());
  154. outToClient.flush();
  155. c.getOutputStream().flush();
  156. break;
  157. }
  158. System.gc();
  159. if (c.isConnected()) {
  160. System.out.println("[" + identifier + "]Socket is Connected: " + c.getRemoteSocketAddress().toString());
  161. }
  162. if (c.isClosed()) {
  163. System.out.println("[" + identifier + "]Socket is Closed: " + c.getRemoteSocketAddress().toString());
  164. }
  165. if (c.isInputShutdown()) {
  166. System.out.println("[" + identifier + "]Socket is Input Shutdown: " + c.getRemoteSocketAddress().toString());
  167. }
  168. if (c.isOutputShutdown()) {
  169. System.out.println("[" + identifier + "]Socket is Output Shutdown: " + c.getRemoteSocketAddress().toString());
  170. }
  171.  
  172. System.out.println("[" + identifier + "]Start INFO+FOTO: " + c.getRemoteSocketAddress().toString());
  173. writeDown = "";
  174.  
  175. for (int j = 0; j < 1; j++) {//projdu kazdy znak, jestli to neni INFO<mezera> nebo FOTO<mezera>, tak chyba
  176. byte cc = inFromClient.readByte();
  177. writeDown += new String(new byte[]{cc}, "US-ASCII");
  178. if (!writeDown.equals("I") && !writeDown.equals("F")) {
  179.  
  180. outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
  181. c.getOutputStream().flush();
  182. c.getOutputStream().close();
  183. return;
  184. }
  185. }
  186.  
  187. for (int j = 1; j < 2; j++) {
  188. byte cc = inFromClient.readByte();
  189. writeDown += new String(new byte[]{cc}, "US-ASCII");
  190. if (!writeDown.equals("IN") && !writeDown.equals("FO")) {
  191. outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
  192. c.getOutputStream().flush();
  193. c.getOutputStream().close();
  194. return;
  195. }
  196. }
  197.  
  198. for (int j = 2; j < 3; j++) {
  199. byte cc = inFromClient.readByte();
  200. writeDown += new String(new byte[]{cc}, "US-ASCII");
  201. if (!writeDown.equals("INF") && !writeDown.equals("FOT")) {
  202. outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
  203. c.getOutputStream().flush();
  204. c.getOutputStream().close();
  205. return;
  206. }
  207. }
  208.  
  209. for (int j = 3; j < 4; j++) {
  210. byte cc = inFromClient.readByte();
  211. writeDown += new String(new byte[]{cc}, "US-ASCII");
  212. if (!writeDown.equals("INFO") && !writeDown.equals("FOTO")) {
  213. outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
  214. c.getOutputStream().flush();
  215. c.getOutputStream().close();
  216. return;
  217. }
  218. }
  219.  
  220. for (int j = 4; j < 5; j++) {
  221. byte cc = inFromClient.readByte();
  222. writeDown += new String(new byte[]{cc}, "US-ASCII");
  223. if (!writeDown.equals("INFO ") && !writeDown.equals("FOTO ")) {
  224. outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
  225. c.getOutputStream().flush();
  226. c.getOutputStream().close();
  227. return;
  228. }
  229. }
  230. if ("INFO ".equals(writeDown)) {
  231. two.clear();
  232. two.add(Byte.MIN_VALUE);
  233. two.add(Byte.MIN_VALUE);
  234. System.out.println("[" + identifier + "]Start INFO: " + c.getRemoteSocketAddress().toString());
  235. while (!(two.get(1) == 10 && two.get(0) == 13)) {
  236. byte cc = inFromClient.readByte();
  237. two.add(cc);
  238. two.remove(0);
  239. }
  240. System.out.println("[" + identifier + "]Finish INFO: " + c.getRemoteSocketAddress().toString()
  241. );
  242.  
  243. outToClient.write("202 OK\r\n".getBytes());
  244. outToClient.flush();
  245. c.getOutputStream().flush();
  246. continue;
  247. }
  248.  
  249.  
  250.  
  251.  
  252. if ("FOTO ".equals(writeDown)) {
  253. System.out.println("[" + identifier + "]Start FOTO len: " + c.getRemoteSocketAddress().toString());
  254. len = "";
  255. writeDown = "";
  256. while (!writeDown.endsWith(" ")) {
  257.  
  258. byte cc = inFromClient.readByte();
  259. if ((cc >= '0' && cc <= '9') || cc == ' ') { //delka foto - cisla
  260. writeDown += new String(new byte[]{cc}, "US-ASCII");
  261. len += new String(new byte[]{cc}, "US-ASCII");
  262. } else {
  263. outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
  264. c.getOutputStream().flush();
  265. c.getOutputStream().close();
  266. return;
  267. }
  268.  
  269. }
  270. len = len.substring(0, len.length() - 1);//nepotrebuji mezeru
  271. int intLen = 0;
  272. try {
  273. intLen = Integer.parseInt(len);//prevod do int
  274. } catch (NumberFormatException e) {
  275. System.out.println("[" + identifier + "]Wrong FOTO len: " + c.getRemoteSocketAddress().toString());
  276.  
  277. outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
  278. c.getOutputStream().flush();
  279. c.getOutputStream().close();
  280. return;
  281. }
  282.  
  283. int bufferSize = 16 * 2048;
  284. byte[] buffer = new byte[bufferSize];
  285. int count = 0;
  286. fotoLen = 0;
  287.  
  288. File f = new File("foto"+fotoNum+".png");
  289. f.createNewFile();
  290. FileOutputStream fos = new FileOutputStream(f);
  291.  
  292. System.out.println("[" + identifier + "]Start FOTO: " + c.getRemoteSocketAddress().toString() + ":" + Integer.toString(intLen));
  293. while (count < intLen) {
  294. int readSize = inFromClient.read(buffer, 0, Math.min(bufferSize, intLen - count));
  295. for (int i = 0; i < readSize; i++) {
  296. fotoLen += (int) 0xFF & buffer[i];
  297. }
  298. fos.write(buffer,0,readSize);
  299.  
  300. //Save to FILE here
  301. count += readSize;
  302. }
  303.  
  304. fos.close();
  305.  
  306. fotoNum++;
  307. System.out.println("[" + identifier + "]Start foto finish: " + c.getRemoteSocketAddress().toString());
  308. byte[] bytes = new byte[4];
  309. for (int i = 0; i < 4; i++) {
  310. byte cc = inFromClient.readByte();
  311. bytes[i] = cc;
  312. }
  313. int value = 0;
  314. for (int i = 0; i < 4; i++) {
  315. value = (value << 8) + (bytes[i] & 0xFF);
  316. }
  317.  
  318. System.out.println("[" + identifier + "]Start foto received: " + c.getRemoteSocketAddress().toString() + ": size = " + Integer.toString(intLen) + ", received = " + Integer.toString(fotoLen) + ", correct = " + Integer.toString(value));
  319.  
  320. if (fotoLen != value) {
  321.  
  322. outToClient.write("300 BAD CHECKSUM\r\n".getBytes());
  323. outToClient.flush();
  324. c.getOutputStream().flush();
  325. f.delete();
  326. fotoNum--;
  327. }
  328. if (fotoLen == value) {
  329. outToClient.write("202 OK\r\n".getBytes());
  330. outToClient.flush();
  331. c.getOutputStream().flush();
  332.  
  333.  
  334. }
  335. continue;
  336.  
  337. }
  338. }
  339. } catch (SocketTimeoutException ex) {
  340. System.out.println("[" + identifier + "]Exception: " + ex.getMessage());
  341. if (outToClient != null) {
  342. try {
  343. outToClient.write("502 TIMEOUT\r\n".getBytes());
  344. c.getOutputStream().flush();
  345. c.getOutputStream().close();
  346. } catch (IOException ex1) {
  347. System.out.println("[" + identifier + "]Exception: " + ex1.getMessage());
  348. ex1.printStackTrace();
  349. }
  350. }
  351. } catch (Exception ex) {
  352. System.out.println("[" + identifier + "]Exception: " + ex.getMessage());
  353.  
  354. ex.printStackTrace();
  355. }
  356.  
  357. }
  358.  
  359. public String BytetoStringFunc(List<Byte> l) throws UnsupportedEncodingException {
  360. byte[] b = new byte[l.size()];
  361. for (int i = 0; i < b.length; i++) {
  362. b[i] = l.get(i);
  363. }
  364. String s = new String(b, "US-ASCII");
  365. return s;
  366. }
  367.  
  368. }
  369.  
  370. class Server {
  371.  
  372. public static void svr_main(int port) throws IOException {
  373. ServerSocket serverSocket = null;
  374. try {
  375. serverSocket = new ServerSocket(port);
  376. Socket clientSocket = null;
  377. Integer index = 0;
  378. while (true) {
  379. index++;
  380. try {
  381. clientSocket = serverSocket.accept();
  382. } catch (IOException e) {
  383. e.printStackTrace();
  384. System.err.println("Accept failed.");
  385. }
  386. System.out.println("client accepted from: " + clientSocket.getInetAddress()
  387. + ":" + clientSocket.getPort());
  388. ClientListener cl = new ClientListener(clientSocket, index.toString());
  389. Thread thread = new Thread(cl);
  390. thread.start();
  391. //cl.run();
  392. }
  393. } catch (Exception e) {
  394. e.printStackTrace();
  395. }
  396. }
  397. }
  398.  
  399. public class Robot {
  400.  
  401. public static void main(String[] args) throws IOException {
  402. if (args.length == 0) {
  403. System.err.println("Server: java robot.Robot <port>");
  404. System.exit(1);
  405. }
  406. System.out.println("Starting server...\n");
  407. Server server = new Server();
  408. server.svr_main(Integer.parseInt(args[0]));
  409. }
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement