-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenshotServer.java
More file actions
182 lines (167 loc) · 7.36 KB
/
Copy pathScreenshotServer.java
File metadata and controls
182 lines (167 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.awt.Window;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.lang.management.ManagementFactory;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.sql.SQLException;
import java.util.Scanner;
import javax.imageio.ImageIO;
public class ScreenshotServer extends Thread
{
private ServerSocket serverSocket;
Socket server;
private String clientIP;
private byte messageType;
private final static String FILE_TO_SEND = "results.txt";
private int counter = 0;
private String[] store;
private String everything;
public ScreenshotServer(int port) throws IOException, SQLException, ClassNotFoundException, Exception
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(30000);
}
public void run()
{
while(true)
{
try
{
server = serverSocket.accept();
DataInputStream dIn = new DataInputStream(server.getInputStream());
DataOutputStream output = new DataOutputStream(server.getOutputStream());
// FileInputStream fis = null; //NEW ADDITIONS
// BufferedInputStream bis = null;
// OutputStream os = null;
messageType = dIn.readByte();
switch(messageType){
case 1:
clientIP = dIn.readUTF();
System.out.println(clientIP);
messageType = dIn.readByte();
System.out.println("Type:Photo");
if(messageType == -1){
// BufferedImage image = ImageIO.read(ImageIO.createImageInputStream(server.getInputStream()));
// System.out.println("Received " + image.getHeight() + "x" + image.getWidth() + ": " + System.currentTimeMillis());
// ImageIO.write(image, "jpg", new File("input.jpg"));
File file = new File("input.jpg");
ObjectInputStream ois = new ObjectInputStream(server.getInputStream());
System.out.println("Input connected");
byte[] bytes;
FileOutputStream fos = null;
try {
bytes = (byte[])ois.readObject();
System.out.println("Reading");
fos = new FileOutputStream(file);
fos.write(bytes);
System.out.println("Writing");
fos.flush();
System.out.println("Received: " + System.currentTimeMillis());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (EOFException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
if (!file.exists()) {
System.out.println("Transfer failed!");
System.exit(1);
}
String command = "sudo python classify_image.py ";
Process p = Runtime.getRuntime().exec(command + "--image_file " + "input.jpg");
System.out.println("Classified");
// server.shutdownInput();
p.waitFor();
//DATA ADDITIONS
try(BufferedReader br = new BufferedReader(new FileReader("results.txt"))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
++counter;
}
everything = sb.toString();
System.out.println(everything);
output.writeByte(counter);
output.flush();
output.writeUTF(everything);
output.flush();
File file1 = new File("results.txt");
file.delete();
file1.delete();
System.out.println("Images deleted");
} catch(IOException e1){
e1.printStackTrace();
}
}
else{
System.out.println("Error");
restart();
}
break;
default:
System.out.println("Invalid IP processing, exit");
restart();
}
}
catch(SocketTimeoutException st)
{
System.out.println("Socket timed out!");
break;
}
catch(IOException e)
{
e.printStackTrace();
break;
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
public static void main(String [] args) throws IOException, SQLException, ClassNotFoundException, Exception
{
Thread t = new ScreenshotServer(12345);
t.start();
System.out.println("Initialised");
// Send t2 = new Send("BTC");
// t2.start();
}
public String returnIp(){
return clientIP;
}
public void restart() {
StringBuilder cmd = new StringBuilder();
cmd.append(System.getProperty("java.home") + File.separator + "bin" + File.separator + "java ");
for (String jvmArg : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
cmd.append(jvmArg + " ");
}
cmd.append("-cp ").append(ManagementFactory.getRuntimeMXBean().getClassPath()).append(" ");
cmd.append(ScreenshotServer.class.getName()).append(" ");
try{
Runtime.getRuntime().exec(cmd.toString());
System.out.println("restarting");
} catch (IOException e){
e.printStackTrace();
}
System.exit(0);
}
}