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 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;
- public ClientListener(Socket c) {
- this.c = c;
- }
- String userName;
- String password;
- String info;
- String len;
- String foto;
- String FILE = "foto0.png";
- Integer fotoLen = 0;
- String ks;
- boolean closeIt = false;
- @Override
- public void run() {
- DataInputStream inFromClient;
- try {
- inFromClient = new DataInputStream(c.getInputStream());
- OutputStream outToClient = c.getOutputStream();
- //DataOutputStream outToClient = new DataOutputStream(c.getOutputStream());
- outToClient.write("200 LOGIN\r\n".getBytes());
- // Timer t = new Timer(45, new ActionListener() {
- // @Override
- // public void actionPerformed(ActionEvent e) {
- // DataOutputStream outToClient;
- // try {
- // outToClient = new DataOutputStream(c.getOutputStream());
- // outToClient.writeBytes("502 TIMEOUT\r\n");
- // c.close();
- // } catch (IOException ex) {
- // Logger.getLogger(ClientListener.class.getName()).log(Level.SEVERE, null, ex);
- // }
- //
- // }
- // });
- String writeDown = "";
- List<Byte> two = new ArrayList<Byte>();
- List<Byte> user = new ArrayList<Byte>();
- List<Byte> pass = new ArrayList<Byte>();
- 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);
- user.add(cc);
- if(two.size()>2){two.remove(0);}
- writeDown += new String(new byte[]{cc}, "US-ASCII");
- }
- int sizeUser = user.size();
- user.remove(sizeUser-1);
- user.remove(sizeUser-2);
- //this.userName = writeDown.substring(0, writeDown.length() - 2);
- System.out.println("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
- //outToClient.writeBytes("201 PASSWORD\r\n");
- outToClient.write("201 PASSWORD\r\n".getBytes());
- 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(cc!=13&&cc!=10){
- pass.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("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
- byte[] robot = {user.get(0),user.get(1),user.get(2),user.get(3),user.get(4)};
- String robotStr =new String(robot);
- if (!"Robot".equals(robotStr)) {
- //outToClient.writeBytes("500 LOGIN FAILED\r\n");
- outToClient.write("500 LOGIN FAILED\r\n".getBytes());
- c.getOutputStream().flush();
- c.getOutputStream().close();
- return;
- }
- Integer passwordAsItMustBe = 0;
- //byte[] unb = this.userName.getBytes("US-ASCII");
- for (byte b : user) {
- passwordAsItMustBe += b;
- }
- // Integer passwordAsItIs =0;
- // for (byte s : pass) {
- // passwordAsItIs += s;
- // }
- if (!password.equals(passwordAsItMustBe.toString())) {
- //outToClient.writeBytes("500 LOGIN FAILED\r\n");
- outToClient.write("500 LOGIN FAILED\r\n".getBytes());
- c.getOutputStream().flush();
- c.getOutputStream().close();
- return;
- }
- //outToClient.writeBytes("202 OK\r\n");
- outToClient.write("202 OK\r\n".getBytes());
- while (true) {
- // byte[] b = new byte[4];
- // byte cc = (byte) inFromClient.read(b);
- // writeDown += new String(new byte[]{cc}, "US-ASCII");
- writeDown = "";
- ArrayList<Byte> infoArray = new ArrayList();
- for(int j=0;j<1;j++){
- byte cc = inFromClient.readByte();
- writeDown += new String(new byte[]{cc}, "US-ASCII");
- if(!writeDown.equals("I")&&!writeDown.equals("F")){
- //outToClient.writeBytes("501 SYNTAX ERROR\r\n");
- 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();
- }
- }
- 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;
- }
- }
- // for (int j = 0; j < 5; j++) {
- // byte cc = inFromClient.readByte();
- // writeDown += new String(new byte[]{cc}, "US-ASCII");
- // a=new String(new byte[]{cc}, "US-ASCII");
- //
- //
- // }
- // if (!"INFO ".equals(writeDown) && !"FOTO ".equals(writeDown)) {
- // outToClient.writeBytes("501 SYNTAX ERROR\r\n");
- // outToClient.flush();
- // c.close();
- // //return;
- // }
- if ("INFO ".equals(writeDown)) {
- while (!writeDown.endsWith("\r\n")) {
- byte cc = inFromClient.readByte();
- writeDown += new String(new byte[]{cc}, "US-ASCII");
- }
- this.info += writeDown.substring(0, writeDown.length() - 2);
- if(info.contains("\r")||info.contains("\n")){
- outToClient.write("501 SYNTAX ERROR\r\n".getBytes());
- c.getOutputStream().flush();
- c.getOutputStream().close();}
- System.out.println("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
- outToClient.write("202 OK\r\n".getBytes());
- }
- File fnew = new File("foto0.png");
- if ("FOTO ".equals(writeDown)) {
- BufferedImage originalImage = ImageIO.read(fnew);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ImageIO.write(originalImage, "png", baos);
- while (!writeDown.endsWith(" ")) {
- byte cc = inFromClient.readByte();
- writeDown += new String(new byte[]{cc}, "US-ASCII");
- len += new String(new byte[]{cc}, "US-ASCII");
- }
- int intLen = Integer.parseInt(len);
- boolean one = true;
- // String m = "";
- // while (one) {
- // byte cc = inFromClient.readByte();
- // writeDown += new String(new byte[]{cc}, "US-ASCII");
- // m += new String(new byte[]{cc}, "US-ASCII");
- // one = false;
- // }
- // if (!" ".equals(m)) {
- // outToClient.writeBytes("501 SYNTAX ERROR\r\n");
- // c.close();
- // return;
- // }
- for (int i = 0; i < intLen; i++) {
- byte[] b = new byte[1];
- foto = "";
- byte cc = inFromClient.readByte();
- writeDown += new String(new byte[]{cc}, "US-ASCII");
- foto += new String(new byte[]{cc}, "US-ASCII");
- fotoLen += Integer.parseInt(foto);
- b = baos.toByteArray();
- }
- byte[] bytes = new byte[4];
- for (int i = 0; i < 4; i++) {
- byte cc = inFromClient.readByte();
- writeDown += new String(new byte[]{cc}, "US-ASCII");
- bytes[i] = cc;
- //ks += new String(new byte[]{cc}, "US-ASCII");
- }
- int value = 0;
- for (int i = 0; i <= 3; i++) {
- value = (value << 8) + (bytes[i] & 0xFF);
- }
- if (fotoLen != value) {
- //outToClient.writeBytes("300 BAD CHECKSUM\r\n");
- outToClient.write("300 BAD CHECKSUM\r\n".getBytes());
- fnew.delete();
- }
- if (fotoLen == value) {
- //outToClient.writeBytes("202 OK\r\n");
- outToClient.write("202 OK\r\n".getBytes());
- ArrayList ar = new ArrayList();
- Random rn = new Random();
- int r = rn.nextInt(999) + 1;
- while (!ar.contains(r)) {
- r++;
- if (r > 999) {
- r = 1;
- }
- }
- ar.add(r);
- File f = new File("foto" + r + ".png");
- fnew.renameTo(f);
- //c.close();
- }
- }
- // writeDown = "";
- // while (!writeDown.endsWith("\r\n")) {
- // byte cc = inFromClient.readByte();
- // writeDown += new String(new byte[]{cc}, "US-ASCII");
- // }
- // this.info = writeDown.substring(0, writeDown.length() - 2);
- // if (!this.info.startsWith("INFO ")) {
- // outToClient.writeBytes("501 SYNTAX ERROR\r\n");
- // c.close();
- // return;
- // }
- //
- // System.out.println("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
- // outToClient.writeBytes("202 OK\r\n");
- }
- //c.close();
- } catch (IOException ex) {
- System.err.println("Couldn't get I/O.");
- System.exit(1);
- }
- }
- }
- class Server {
- public static void svr_main(int port) throws IOException {
- ServerSocket serverSocket = null;
- try {
- serverSocket = new ServerSocket(port);
- } catch (IOException e) {
- System.err.println("Could not listen on port: " + port);
- System.exit(1);
- }
- Socket clientSocket = null;
- while (true) {
- try {
- clientSocket = serverSocket.accept();
- } catch (IOException e) {
- serverSocket.close();
- System.err.println("Accept failed.");
- System.exit(1);
- }
- System.out.println("client accepted from: " + clientSocket.getInetAddress()
- + ":" + clientSocket.getPort());
- ClientListener cl = new ClientListener(clientSocket);
- Thread thread = new Thread(cl);
- thread.start();
- }
- }
- }
- 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