我正在尝试使用java创建远程桌面监视器应用程序,其中服务器可以看到客户端的屏幕截图,还可以监视客户端上运行的进程数。以下是用户界面的屏幕截图
但现在我有一个我无法修复的错误。我已经调试了好几次,但我找不到跟踪错误的方法。以下是我现在面临的问题:=
1)当我按下" GET PROCESS LIST"按钮没有任何反应当按下捕获按钮时,进程列表被提取到textarea,首先我必须按下" GET PROCESS LIST"然后我必须按下捕获按钮才能显示进程列表。
2)当我按下捕获按钮时,它只捕获客户端的屏幕截图一次。在那之后,当我按下捕获按钮时,它不会做任何事情。与获取流程按钮相同。它们只执行一次。
我已尝试调试客户端和服务器但未能跟踪错误。
这是我的客户端和服务器代码。服务器的代码很大,但我必须提供整个代码,因为我不知道问题究竟在哪里。
这是客户
public class ScreenClient {
private static BufferedImage screen;
public static void main(String[] args) {
try {
Socket server = new Socket("localhost", 5494);
while (true) {
BufferedReader bf = new BufferedReader(new InputStreamReader(server.getInputStream()));
String s;
s = bf.readLine();
System.out.println(s);
if (s.contains("execute")) {
ImageThread im=new ImageThread();
im.start();
}
if (s.contains("getProcessList")) {
ProcessThread pt=new ProcessThread();
pt.start();
}
}
} catch (Exception e) {
System.err.println("Disconnected From server ->" + e.getMessage());
}
}
public static class ImageThread extends Thread {
@Override
public void run() {
try {
Socket server = new Socket("localhost", 9999);//connect to server at port 9999 for screenshots
Robot robot = new Robot();
Rectangle size = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
screen = robot.createScreenCapture(size);
int[] rgbData = new int[(int) (size.getWidth() * size.getHeight())];
screen.getRGB(0, 0, (int) size.getWidth(), (int) size.getHeight(), rgbData, 0, (int) size.getWidth());
OutputStream baseOut = server.getOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baseOut);
out.writeObject(size);
//for (int x = 0; x < rgbData.length; x++) {
out.writeObject(rgbData);
// }
out.flush();
server.close();
//added new
} catch (Exception e) {
e.printStackTrace();
System.err.println("Disconnected From server ->" + e.getMessage());
}
}
}
public static class ProcessThread extends Thread {
@Override
public void run() {
System.out.println("\n\n********");
StringBuilder builder = new StringBuilder("");
Socket server;
PrintWriter ps;
String query = "tasklist";
try {
server = new Socket("localhost", 9898);//connect to server at port 9898 for process list
Runtime runtime = Runtime.getRuntime();
InputStream input = runtime.exec(query).getInputStream();
BufferedInputStream buffer = new BufferedInputStream(input);
BufferedReader commandResult = new BufferedReader(new InputStreamReader(buffer));
String line = "";
while ((line = commandResult.readLine()) != null) {
builder.append(line + "\n");
}
//byte[] responseClient=s.getBytes();
System.out.println("Obj ::" + server);
ps = new PrintWriter(server.getOutputStream());
ps.write(builder.toString());
System.out.println(builder.toString());
ps.flush();
server.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
这是服务器的代码:=
public class ScreenServer extends JFrame implements ActionListener {
String uname;
BufferedReader br;
static JTextArea taMessages, taUserList, taProcessList;
JTextField tfInput;
static Socket client, client1,client2;//
JButton btnCapture, btnExit, btnGetUsers, btnGetProcess;
static HashMap<String, Socket> users = new HashMap<>();
static ArrayList<Socket> usersSockets = new ArrayList();
PrintWriter os;
PrintWriter os2;
public static void main(String[] args) throws Exception {
ScreenServer screenServer = new ScreenServer();
screenServer.buildInterface();
ServerSocket server = new ServerSocket(5494);
ServerSocket s1 = new ServerSocket(9898);
ServerSocket s2=new ServerSocket(9999);
while (true) {
ScreenServer.client = server.accept();
users.put(client.getInetAddress().getHostName(), client);
usersSockets.add(client); //not needed
InetAddress inet1 = client.getInetAddress();
broadcast(inet1.getHostName(), " Has connected!");
client1=s2.accept();//accept connection on port 9999
ScreenshotThread st = new ScreenshotThread(client1);
st.start();
client2=s1.accept();//accept connection on port 9898
ProcessListClient clientProcessList = new ProcessListClient(client2);
clientProcessList.start();
}
}
public void buildInterface() {
btnCapture = new JButton("Capture Screen");
btnExit = new JButton("Exit");
btnGetUsers = new JButton("Get Users");
btnGetProcess = new JButton("Get Process List");
//chat area
taMessages = new JTextArea();
taMessages.setRows(10);
taMessages.setColumns(50);
taMessages.setEditable(false);
taProcessList = new JTextArea();
taProcessList.setRows(10);
taProcessList.setColumns(50);
taProcessList.setEditable(false);
//online users list
taUserList = new JTextArea();
taUserList.setRows(10);
taUserList.setColumns(10);
taUserList.setEditable(false);
//top panel (chat area and online users list
JScrollPane chatPanel = new JScrollPane(taMessages, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JScrollPane onlineUsersPanel = new JScrollPane(taUserList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JScrollPane processPanel = new JScrollPane(taProcessList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JPanel tp = new JPanel(new FlowLayout());
tp.add(chatPanel);
tp.add(onlineUsersPanel);
tp.add(processPanel);
add(tp, "Center");
//user input field
tfInput = new JTextField(50);
//buttom panel (input field, send and exit)
JPanel bp = new JPanel(new FlowLayout());
bp.add(tfInput);
bp.add(btnCapture);
bp.add(btnExit);
bp.add(btnGetUsers);
bp.add(btnGetProcess);
add(bp, "South");
btnCapture.addActionListener(this);
tfInput.addActionListener(this);//allow user to press Enter key in order to send message
btnExit.addActionListener(this);
btnGetUsers.addActionListener(this);
btnGetProcess.addActionListener(this);
setSize(500, 300);
setVisible(true);
pack();
}
@Override
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == btnExit) {
System.exit(0);
} else if (tfInput.getText().contains("!getusers")) {
taUserList.setText(" ");
this.getUsersOnline();
} else if (evt.getSource() == btnCapture) {
MessagesThread mt = new MessagesThread(client);
mt.start();
}
if (evt.getSource() == btnGetUsers) {
taUserList.setText("");
System.out.println("ERROR--> No Client Connection Found");
}
if (evt.getSource() == btnGetProcess) {
GetProcessThread gt = new GetProcessThread(client);
gt.start();
}
}
private void getUsersOnline() {
Iterator it = users.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
putUserOnline((String) pairs.getKey());
}
}
public static void broadcast(String user, String message) {
putMessage(user, message);
}
public static void putMessage(String uname, String msg) {
taMessages.append(uname + ": " + msg + "\n");
}
public static void putUserOnline(String name) {
taUserList.setText(name + ": " + "\n");
}
public class MessagesThread extends Thread {
Socket client;
public MessagesThread(Socket client) {
this.client = client;
}
@Override
public void run() {
String ClientName = tfInput.getText();
try {
String response = "execute";
Socket clientValue = users.get(ClientName);
os = new PrintWriter(clientValue.getOutputStream(), true);
os.println(response);
} catch (IOException ex) {
Logger.getLogger(ScreenServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//here
public class GetProcessThread extends Thread {
Socket client;
public GetProcessThread(Socket client) {
this.client = client;
}
@Override
public void run() {
String ClientName = tfInput.getText();
try {
String response = "getProcessList";
Socket clientValue = users.get(ClientName);
os2 = new PrintWriter(clientValue.getOutputStream(), true);
os2.println(response);
} catch (IOException ex) {
Logger.getLogger(ScreenServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//here
static class ScreenshotThread extends Thread {
BufferedImage screen;
Socket client;
public ScreenshotThread(Socket client) {
this.client=client;
}
@Override
public void run() {
try {
Robot robot = new Robot();
ObjectInputStream in = new ObjectInputStream(client.getInputStream());
Rectangle size = (Rectangle) in.readObject();
int[] rgbData = new int[(int) (size.getWidth() * size.getHeight())];
/* for (int x = 0; x < rgbData.length; x++) {
rgbData[x] = in.readInt();
}*/
rgbData=(int[])in.readObject();
screen = new BufferedImage((int) size.getWidth(), (int) size.getHeight(), BufferedImage.TYPE_INT_ARGB);
screen.setRGB(0, 0, (int) size.getWidth(), (int) size.getHeight(), rgbData, 0, (int) size.getWidth());
InetAddress clientAddress = client.getInetAddress();
String clientName = clientAddress.getHostName();
String t = (new Date()).toString();
ImageIO.write(screen, "png", new File(clientName + ".png"));
} catch (Exception ioe) {
ioe.printStackTrace();
}
}
}
static class ProcessListClient extends Thread {
Socket client;
BufferedReader is;
public ProcessListClient(Socket s1) {
this.client=s1;
}
@Override
public void run() {
StringBuilder stringBuilder = new StringBuilder("");
try {
this.is = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line = "";
while ((line = is.readLine()) != null) {
stringBuilder.append(line + "\n");
}
System.out.println(stringBuilder);
taProcessList.setText(stringBuilder.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
我想我已经陈述了所有问题并提供了相关的代码。我真的很感激任何帮助。 :)
答案 0 :(得分:1)
ServerSocket server = new ServerSocket(5494);
ServerSocket s1 = new ServerSocket(9898);
ServerSocket s2=new ServerSocket(9999);
while (true) {
ScreenServer.client = server.accept();
client1=s2.accept();//accept connection on port 9999
client2=s1.accept();//accept connection on port 9898
除非服务器获得客户端,否则server.accept()
不会转到下一行,类似s2.accept()
除非s2获取客户端,否则{}不会转到下一行。因此,所有三个ServerSocket
应该accept()
在不同的线程中。