- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用java创建一个远程桌面监控应用程序,其中服务器可以看到客户端的屏幕截图,还可以监控客户端上运行的进程数。这是用户界面的屏幕截图
但现在我有一个无法修复的错误。我已经调试了好几次了,但找不到跟踪错误的方法。以下是我现在面临的问题:=
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/
我正在创建一个 sql server 存储过程,它将输入作为逗号分隔的 productid 或选项“全部”。当用户传入逗号分隔的产品 ID 时,查询应输出所有产品 ID 的数据。我使用“IN”语句执行
我有一个自动生成的 Web 服务客户端。我有很多复杂的类,我必须对其进行模式匹配。现在我的结构如下所示: val response = client.getResponse response matc
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 7年前关闭。 Improve this
我需要正确的 tsql 语法来解决这个问题: Select * from table where var_A='10' select * from table where var_B='10' 何时使
我遇到了这个问题。每当我运行程序并在需要时键入字母 m 时,我的 if 语句都不会识别它。有人知道为什么吗?我已经这样做了一个小时,但没有结果。 #include #include #includ
我从数据库列名称“你有护照”创建了一个表,用户回答是或否我如何将 css 应用到这个动态工作的表。 table, th, td { border: 1px solid black;
我对 LocationListener 类的 onStatusChanged 有一些疑问。 它知道它可以呈现三种状态:AVAILABLE、TEMPORARILY_UNAVAILABLE 和 OUT_O
当引入新的异常类型时,我总是不确定如何正确地做到这一点。有共同约定吗?你怎么做呢? 我对您组织它们的范围感兴趣(将它们保留在它们所使用的单元中?在组件级别有一个单元?包级别?应用程序?) 这也会影响命
我使用以下内容创建了日期维度: https://www.codeproject.com/Articles/647950/Create-and-Populate-Date-Dimension-for-D
您好,我正在使用 Android 完全 Kiosk 浏览器,该浏览器使用 chrome Webview。但是 javascript 中的某些方法或函数无法正常工作,例如 window.print()。
我有以下代码: public void OpenFile(string FileName) { if (FileName == null)
获取索引越界异常 for (int recordData = 0; recordData < recordDataList.size(); recordData++) {
我使用它在发生错误时在登录中显示一条消息: × Invalid user or password
这是我的场景,我有一个异常列表,其中包含来自不同层次结构的任意异常,下面的代码快照将解释我需要做什么 private List connectionExceptions; try { // tryin
我尝试动态更新 Jtextpane 中的左缩进。但我不能!这是我尝试过的! DefaultStyledDocument document = (DefaultStyledDocument) textp
我不知道为什么这个异常不起作用...... import java.util.*; public class a { public static void main(String[] args
我目前在 case 中使用多个 when 时遇到问题。当我删除第二个当时,它就起作用了。这是什么问题? 报告的MYSQL错误为: #1064 - You have an error in your S
例如,我有一个表记录用户查看和下载文件的事件, file_id user activity 2 Tim view 1 Ron
这是一个非常愚蠢的问题,但我需要一点安慰/帮助。我有当前的“递归”情况: void add( int value ) { // do something ... // if ( conditi
我尝试使用以下代码在按钮数组上注册回调。但我似乎无法理解如何绑定(bind)回调中需要的字符串。任何建议将不胜感激! for (var i = 0; i < this.car_types.length
我是一名优秀的程序员,十分优秀!