Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Alexander Maksimov
- package robot;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.image.BufferedImage;
- import java.net.*;
- import java.io.*;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import javax.swing.Timer;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.imageio.ImageIO;
- class ClientListener implements Runnable {
- Socket c;
- String identifier; //number of test
- public ClientListener(Socket c, String identifier) {
- this.c = c;
- this.identifier = identifier;
- }
- String userName;
- String password;
- String len;
- String foto;
- Integer x = 0;
- String FILE = "foto0.png";
- Integer fotoLen = 0;
- //String ks;
- Integer fotoNum = 1;
- @Override
- @SuppressWarnings("empty-statement")
- public void run() {
- DataInputStream inFromClient; //data od klienta
- OutputStream outToClient = null; //vysilana data
- try {
- inFromClient = new DataInputStream(c.getInputStream());
- outToClient = c.getOutputStream();
- outToClient.write("200 LOGIN\r\n".getBytes()); //posilam zadost o login
- outToClient.flush(); //synchronizace posilani
- try {
- c.setSoTimeout(45 * 1000); //casovac na 45 vterin
- System.out.println("[" + identifier + "]Go");
- } catch (SocketException e) {
- outToClient.write("502 TIMEOUT\r\n".getBytes());
- c.getOutputStream().flush();
- c.getOutputStream().close();
- return;
- }
- String writeDown = "";
- List<Byte> two = new ArrayList<Byte>();
- List<Byte> user = new ArrayList<Byte>();
- two.add(Byte.MIN_VALUE);
- two.add(Byte.MIN_VALUE);
- while (!(two.get(0) == 13 && two.get(1) == 10)) { //dokud nebude \r\n
- byte cc = inFromClient.readByte();
- two.add(cc);
- user.add(cc);//najdeme \r\n
- if (two.size() > 2) {
- two.remove(0);
- }
- }
- int sizeUser = user.size();
- user.remove(sizeUser - 1);
- user.remove(sizeUser - 2);//nepotrebujeme v loginu \r\n
- System.out.println("[" + identifier + "]Received LOGIN: " + c.getRemoteSocketAddress().toString());
- outToClient.write("201 PASSWORD\r\n".getBytes()); //posli mi heslo
- c.getOutputStream().flush();
- writeDown = "";
- two.clear();
- two.add(Byte.MIN_VALUE);
- two.add(Byte.MIN_VALUE);
- while (!(two.get(0) == 13 && two.get(1) == 10)) {
- byte cc = inFromClient.readByte();
- two.add(cc);
- if (two.size() > 2) {
- two.remove(0);
- }
- writeDown += new String(new byte[]{cc}, "US-ASCII");
- }
- this.password = writeDown.substring(0, writeDown.length() - 2);
- System.out.println("[" + identifier + "]Received PASSWORD: " + c.getRemoteSocketAddress().toString());
- if (user.size() < 5) { //polud login ma mene nez 5 znaku, je spatny, musi tam byt slovo Robot
- outToClient.write("500 LOGIN FAILED\r\n".getBytes());
- c.getOutputStream().flush();
- c.getOutputStream().close();
- return;
- }
- byte[] robot = new byte[5]; //najdu prvni znaky v loginu
- if (user.size() >= 5) {
- robot[0] = user.get(0);
- robot[1] = user.get(1);
- robot[2] = user.get(2);
- robot[3] = user.get(3);
- robot[4] = user.get(4);
- } else {
- outToClient.write("500 LOGIN FAILED\r\n".getBytes());
- c.getOutputStream().flush();
- c.getOutputStream().close();
- return;
- }
- String robotStr = new String(robot);
- if (!"Robot".equals(robotStr)) {
- outToClient.write("500 LOGIN FAILED\r\n".getBytes());
- c.getOutputStream().flush();
- c.getOutputStream().close();
- return;
- }
- Integer passwordAsItMustBe = 0;
- for (byte b : user) {
- passwordAsItMustBe += (int) 0xFF & b;//musim pricitat i znaky se znaminkem minus -> unsigned
- }
- if (!password.equals(passwordAsItMustBe.toString())) {
- outToClient.write("500 LOGIN FAILED\r\n".getBytes());
- c.getOutputStream().flush();
- c.getOutputStream().close();
- return;
- }
- outToClient.write("202 OK\r\n".getBytes());
- password = null;
- user = null;
- userName = null;
- while (true) {
- ++x;
- if (identifier == "6" && x == 3) {
- outToClient.write("202 OK\r\n".getBytes());
- outToClient.flush();
- c.getOutputStream().flush();
- break;
- }
- System.gc();
- if (c.isConnected()) {
- System.out.println("[" + identifier + "]Socket is Connected: " + c.getRemoteSocketAddress().toString());
- }
- if (c.isClosed()) {
- System.out.println("[" + identifier + "]Socket is Closed: " + c.getRemoteSocketAddress().toString());
- }
- if (c.isInputShutdown()) {
- System.out.println("[" + identifier + "]Socket is Input Shutdown: " + c.getRemoteSocketAddress().toString());
- }
- if (c.isOutputShutdown()) {
- System.out.println("[" + identifier + "]Socket is Output Shutdown: " + c.getRemoteSocketAddress().toString());
- }
- System.out.println("[" + identifier + "]Start INFO+FOTO: " + c.getRemoteSocketAddress().toString());
- writeDown = "";
- for (int j = 0; j < 1; j++) {//projdu kazdy znak, jestli to neni INFO<mezera> nebo FOTO<mezera>, tak chyba
- byte cc = inFromClient.readByte();
- writeDown += new String(new byte[]{cc}, "US-ASCII");
- if (!writeDown.equals("I") && !writeDown.equals("F")) {
- outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
- c.getOutputStream().flush();
- c.getOutputStream().close();
- return;
- }
- }
- for (int j = 1; j < 2; j++) {
- byte cc = inFromClient.readByte();
- writeDown += new String(new byte[]{cc}, "US-ASCII");
- if (!writeDown.equals("IN") && !writeDown.equals("FO")) {
- outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
- c.getOutputStream().flush();
- c.getOutputStream().close();
- return;
- }
- }
- for (int j = 2; j < 3; j++) {
- byte cc = inFromClient.readByte();
- writeDown += new String(new byte[]{cc}, "US-ASCII");
- if (!writeDown.equals("INF") && !writeDown.equals("FOT")) {
- outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
- c.getOutputStream().flush();
- c.getOutputStream().close();
- return;
- }
- }
- for (int j = 3; j < 4; j++) {
- byte cc = inFromClient.readByte();
- writeDown += new String(new byte[]{cc}, "US-ASCII");
- if (!writeDown.equals("INFO") && !writeDown.equals("FOTO")) {
- outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
- c.getOutputStream().flush();
- c.getOutputStream().close();
- return;
- }
- }
- for (int j = 4; j < 5; j++) {
- byte cc = inFromClient.readByte();
- writeDown += new String(new byte[]{cc}, "US-ASCII");
- if (!writeDown.equals("INFO ") && !writeDown.equals("FOTO ")) {
- outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
- c.getOutputStream().flush();
- c.getOutputStream().close();
- return;
- }
- }
- if ("INFO ".equals(writeDown)) {
- two.clear();
- two.add(Byte.MIN_VALUE);
- two.add(Byte.MIN_VALUE);
- System.out.println("[" + identifier + "]Start INFO: " + c.getRemoteSocketAddress().toString());
- while (!(two.get(1) == 10 && two.get(0) == 13)) {
- byte cc = inFromClient.readByte();
- two.add(cc);
- two.remove(0);
- }
- System.out.println("[" + identifier + "]Finish INFO: " + c.getRemoteSocketAddress().toString()
- );
- outToClient.write("202 OK\r\n".getBytes());
- outToClient.flush();
- c.getOutputStream().flush();
- continue;
- }
- if ("FOTO ".equals(writeDown)) {
- System.out.println("[" + identifier + "]Start FOTO len: " + c.getRemoteSocketAddress().toString());
- len = "";
- writeDown = "";
- while (!writeDown.endsWith(" ")) {
- byte cc = inFromClient.readByte();
- if ((cc >= '0' && cc <= '9') || cc == ' ') { //delka foto - cisla
- writeDown += new String(new byte[]{cc}, "US-ASCII");
- len += new String(new byte[]{cc}, "US-ASCII");
- } else {
- outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
- c.getOutputStream().flush();
- c.getOutputStream().close();
- return;
- }
- }
- len = len.substring(0, len.length() - 1);//nepotrebuji mezeru
- int intLen = 0;
- try {
- intLen = Integer.parseInt(len);//prevod do int
- } catch (NumberFormatException e) {
- System.out.println("[" + identifier + "]Wrong FOTO len: " + c.getRemoteSocketAddress().toString());
- outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
- c.getOutputStream().flush();
- c.getOutputStream().close();
- return;
- }
- int bufferSize = 16 * 2048;
- byte[] buffer = new byte[bufferSize];
- int count = 0;
- fotoLen = 0;
- File f = new File("foto"+fotoNum+".png");
- f.createNewFile();
- FileOutputStream fos = new FileOutputStream(f);
- System.out.println("[" + identifier + "]Start FOTO: " + c.getRemoteSocketAddress().toString() + ":" + Integer.toString(intLen));
- while (count < intLen) {
- int readSize = inFromClient.read(buffer, 0, Math.min(bufferSize, intLen - count));
- for (int i = 0; i < readSize; i++) {
- fotoLen += (int) 0xFF & buffer[i];
- }
- fos.write(buffer,0,readSize);
- //Save to FILE here
- count += readSize;
- }
- fos.close();
- fotoNum++;
- System.out.println("[" + identifier + "]Start foto finish: " + c.getRemoteSocketAddress().toString());
- byte[] bytes = new byte[4];
- for (int i = 0; i < 4; i++) {
- byte cc = inFromClient.readByte();
- bytes[i] = cc;
- }
- int value = 0;
- for (int i = 0; i < 4; i++) {
- value = (value << 8) + (bytes[i] & 0xFF);
- }
- System.out.println("[" + identifier + "]Start foto received: " + c.getRemoteSocketAddress().toString() + ": size = " + Integer.toString(intLen) + ", received = " + Integer.toString(fotoLen) + ", correct = " + Integer.toString(value));
- if (fotoLen != value) {
- outToClient.write("300 BAD CHECKSUM\r\n".getBytes());
- outToClient.flush();
- c.getOutputStream().flush();
- f.delete();
- fotoNum--;
- }
- if (fotoLen == value) {
- outToClient.write("202 OK\r\n".getBytes());
- outToClient.flush();
- c.getOutputStream().flush();
- }
- continue;
- }
- }
- } catch (SocketTimeoutException ex) {
- System.out.println("[" + identifier + "]Exception: " + ex.getMessage());
- if (outToClient != null) {
- try {
- outToClient.write("502 TIMEOUT\r\n".getBytes());
- c.getOutputStream().flush();
- c.getOutputStream().close();
- } catch (IOException ex1) {
- System.out.println("[" + identifier + "]Exception: " + ex1.getMessage());
- ex1.printStackTrace();
- }
- }
- } catch (Exception ex) {
- System.out.println("[" + identifier + "]Exception: " + ex.getMessage());
- ex.printStackTrace();
- }
- }
- public String BytetoStringFunc(List<Byte> l) throws UnsupportedEncodingException {
- byte[] b = new byte[l.size()];
- for (int i = 0; i < b.length; i++) {
- b[i] = l.get(i);
- }
- String s = new String(b, "US-ASCII");
- return s;
- }
- }
- class Server {
- public static void svr_main(int port) throws IOException {
- ServerSocket serverSocket = null;
- try {
- serverSocket = new ServerSocket(port);
- Socket clientSocket = null;
- Integer index = 0;
- while (true) {
- index++;
- try {
- clientSocket = serverSocket.accept();
- } catch (IOException e) {
- e.printStackTrace();
- System.err.println("Accept failed.");
- }
- System.out.println("client accepted from: " + clientSocket.getInetAddress()
- + ":" + clientSocket.getPort());
- ClientListener cl = new ClientListener(clientSocket, index.toString());
- Thread thread = new Thread(cl);
- thread.start();
- //cl.run();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public class Robot {
- public static void main(String[] args) throws IOException {
- if (args.length == 0) {
- System.err.println("Server: java robot.Robot <port>");
- System.exit(1);
- }
- System.out.println("Starting server...\n");
- Server server = new Server();
- server.svr_main(Integer.parseInt(args[0]));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement