gpt4 book ai didi

java - 为什么会发生这种情况?

转载 作者:行者123 更新时间:2023-12-01 13:07:14 28 4
gpt4 key购买 nike

我正在尝试使用java创建一个远程桌面监控应用程序,其中服务器可以看到客户端的屏幕截图,还可以监控客户端上运行的进程数。这是用户界面的屏幕截图

enter image description here

但现在我有一个无法修复的错误。我已经调试了好几次了,但找不到跟踪错误的方法。以下是我现在面临的问题:=

1)当我按下“获取进程列表”按钮时,没有任何反应。当按下捕获按钮时,进程列表将被提取到文本区域,即首先我必须按下“获取进程列表”,然后我必须按下捕获按钮才能显示进程列表。

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();
}

}
}

}

我想我已经陈述了所有问题并提供了相关代码。我非常感谢任何帮助。 :)

最佳答案

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()

关于java - 为什么会发生这种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23179321/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com