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.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;
- Integer fotoLen = 0;
- @Override
- public void run() {
- DataInputStream inFromClient;
- try {
- inFromClient = new DataInputStream(c.getInputStream());
- DataOutputStream outToClient = new DataOutputStream(c.getOutputStream());
- outToClient.writeBytes("200 LOGIN\r\n");
- 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 = "";
- while (!writeDown.endsWith("\r\n")) {
- byte cc = inFromClient.readByte();
- writeDown += new String(new byte[]{cc}, "US-ASCII");
- }
- this.userName = writeDown.substring(0, writeDown.length() - 2);
- System.out.println("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
- outToClient.writeBytes("201 PASSWORD\r\n");
- writeDown = "";
- while (!writeDown.endsWith("\r\n")) {
- byte cc = inFromClient.readByte();
- writeDown += new String(new byte[]{cc}, "US-ASCII");
- }
- this.password = writeDown.substring(0, writeDown.length() - 2);
- System.out.println("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
- if (!this.userName.startsWith("Robot")) {
- outToClient.writeBytes("500 LOGIN FAILED\r\n");
- c.close();
- return;
- }
- Integer passwordAsItMustBe = 0;
- byte[] unb = this.userName.getBytes("US-ASCII");
- for (byte b : unb) {
- passwordAsItMustBe += b;
- }
- if (!this.password.equals(passwordAsItMustBe.toString())) {
- outToClient.writeBytes("500 LOGIN FAILED\r\n");
- c.close();
- return;
- }
- outToClient.writeBytes("202 OK\r\n");
- while (true) {
- writeDown = "";
- for (int j = 0; j < 5; j++) {
- byte cc = inFromClient.readByte();
- writeDown += new String(new byte[]{cc}, "US-ASCII");
- }
- if (!"INFO ".equals(writeDown) || !"FOTO ".equals(writeDown)) {
- outToClient.writeBytes("501 SYNTAX ERROR\r\n");
- 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);
- System.out.println("Received: " + c.getRemoteSocketAddress().toString() + ":" + writeDown);
- outToClient.writeBytes("202 OK\r\n");
- }
- File fnew = new File("foto0.png");
- BufferedImage originalImage = ImageIO.read(fnew);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ImageIO.write(originalImage, "png", baos);
- if ("FOTO ".equals(writeDown)) {
- 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");
- fnew.delete();
- c.close();
- return;
- }
- if (fotoLen == value) {
- outToClient.writeBytes("202 OK\r\n");
- 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();
- }
- }
- }
- //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