"""Server.java"""\nimport javax.imageio.ImageIO;\nimport javax.sound.sampled.AudioFormat;\nimport javax.sound.sampled.AudioSystem;\nimport javax.sound.sampled.TargetDataLine;\nimport javax.swing.;\nimport java.awt.;\nimport java.awt.image.BufferedImage;\nimport java.io.;\nimport java.net.ServerSocket;\nimport java.net.Socket;\nimport java.util.ArrayList;\n\npublic class Server {\n private static final int SAMPLE_RATE = 44100;\n private static final int SAMPLE_SIZE = 16;\n public static ServerSocket ss;\n public static ArrayList users = new ArrayList<>();\n public static final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();\n\n public static byte[] serializeImage(BufferedImage image) throws IOException {\n ByteArrayOutputStream baos = new ByteArrayOutputStream();\n ImageIO.write(image, "png", baos);\n return baos.toByteArray();\n }\n\n public static BufferedImage deserializeImage(byte[] imageBytes) throws IOException {\n ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);\n return ImageIO.read(bais);\n }\n\n public static class GetData extends Thread {\n public Socket socket;\n public ObjectInputStream ois;\n public ObjectOutputStream oos;\n\n public GetData(Socket socket) {\n this.socket = socket;\n }\n\n @Override\n public void run() {\n try {\n ois = new ObjectInputStream(socket.getInputStream());\n oos = new ObjectOutputStream(socket.getOutputStream());\n } catch (IOException e) {\n throw new RuntimeException(e);\n }\n while (!Thread.currentThread().isInterrupted()) {\n try {\n Datapack datapack = (Datapack) ois.readObject();\n if (datapack.getHead().equals("request")) {\n if (datapack.getDatatype().equals(DATATYPE.STRING)) {\n if (datapack.getContent().equals("ScreenSize")) {\n Datapack result = new Datapack("back", DATATYPE.STRING, screenSize.width + ";" + screenSize.height, "null");\n oos.writeObject(result);\n oos.flush();\n } else if (datapack.getContent().equals("registry")) {\n users.add(socket);\n }\n }\n }\n } catch (IOException | ClassNotFoundException ignore) {\n users.remove(socket);\n Thread.currentThread().interrupt();\n }\n }\n try {\n ois.close();\n oos.close();\n socket.close();\n } catch (IOException ignore) {\n }\n }\n }\n\n public static void main(String[] args) throws IOException {\n int port = Integer.parseInt(JOptionPane.showInputDialog("输入开启的端口:"));\n ss = new ServerSocket(port);\n Thread acceptUser = new Thread(() -> {\n while (!Thread.currentThread().isInterrupted()) {\n try {\n Socket user = ss.accept();\n GetData gd = new GetData(user);\n gd.setDaemon(true);\n gd.start();\n } catch (IOException ignore) {\n }\n }\n });\n acceptUser.setDaemon(true);\n acceptUser.start();\n Thread sendPhoto = new Thread(() -> {\n Robot robot = null;\n try {\n robot = new Robot();\n } catch (AWTException ignore) {\n }\n while (!Thread.currentThread().isInterrupted()) {\n BufferedImage image = robot.createScreenCapture(new Rectangle(screenSize));\n try {\n byte[] imageBytes = serializeImage(image);\n for (Socket user : users) {\n try {\n ObjectOutputStream ops = new ObjectOutputStream(user.getOutputStream());\n Datapack sendPacket = new Datapack("photo", DATATYPE.PHOTO, imageBytes, "null");\n ops.writeObject(sendPacket);\n ops.flush();\n } catch (IOException e) {\n //users.remove(user);\n }\n\n }\n } catch (IOException ignore) {\n }\n }\n });\n sendPhoto.setDaemon(true);\n sendPhoto.start();\n Thread sendAudio = new Thread(() -> {\n while (!Thread.currentThread().isInterrupted()) {\n try {\n AudioFormat format = new AudioFormat(SAMPLE_RATE, SAMPLE_SIZE, 1, true, true);\n TargetDataLine line = AudioSystem.getTargetDataLine(format);\n line.open(format);\n line.start();\n byte[] buffer = new byte[4096];\n while (!Thread.currentThread().isInterrupted()) {\n int bytesRead = line.read(buffer, 0, buffer.length);\n for (Socket user : users) {\n ObjectOutputStream ops = new ObjectOutputStream(user.getOutputStream());\n ops.writeObject(new Datapack("audio", DATATYPE.AUDIO, buffer, Integer.toString(bytesRead)));\n ops.flush();\n }\n }\n } catch (Exception ignore) {\n }\n }\n });\n sendAudio.setDaemon(true);\n sendAudio.start();\n }\n}\n\n"""Client.java"""\nimport javax.imageio.ImageIO;\nimport javax.swing.;\nimport java.awt.*;\nimport java.awt.image.BufferedImage;\nimport java.io.ByteArrayInputStream;\nimport java.io.IOException;\nimport java.io.ObjectInputStream;\nimport java.io.ObjectOutputStream;\nimport java.net.Socket;\n\npublic class Client {\n public static int width;\n public static int height;\n public static ObjectOutputStream oos;\n public static ObjectInputStream ois;\n public static Socket socket;\n\n public static BufferedImage deserializeImage(byte[] imageBytes) throws IOException {\n ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);\n return ImageIO.read(bais);\n }\n\n public static void main(String[] args) throws IOException, ClassNotFoundException {\n String address = JOptionPane.showInputDialog("输入想要连接的服务器(地址):");\n String ip = address.split(":")[0];\n int port = Integer.parseInt(address.split(":")[1]);\n socket = new Socket(ip, port);\n JOptionPane.showMessageDialog(null, "连接成功建立!关闭此窗口继续!");\n oos = new ObjectOutputStream(socket.getOutputStream());\n oos.writeObject(new Datapack("request", DATATYPE.STRING, "ScreenSize", "null"));\n oos.flush();\n ois = new ObjectInputStream(socket.getInputStream());\n Datapack datapack = (Datapack) ois.readObject();\n String content = (String) datapack.getContent();\n width = Integer.parseInt(content.split(":")[0]);\n height = Integer.parseInt(content.split(":")[1]);\n oos.writeObject(new Datapack("request", DATATYPE.STRING, "registry", "null"));\n oos.flush();\n SwingUtilities.invokeLater(() -> {\n JFrame desktop = new JFrame("分享屏幕 " + ip + ":" + port);\n desktop.setLayout(null);\n Dimension screenSize = desktop.getToolkit().getScreenSize();\n desktop.setBounds((screenSize.width - width) / 2, (screenSize.height - height) / 2, width, height);\n desktop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n JPanel panel = new JPanel() {\n @Override\n public void paint(Graphics g) {\n super.paint(g);\n try {\n byte[] photo = new byte[4096];\n int bytesRead = 0;\n int offset = 0;\n while (offset < photo.length && (bytesRead = ois.read(photo, offset, photo.length - offset)) != -1) {\n offset += bytesRead;\n }\n BufferedImage image = deserializeImage(photo);\n g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);\n repaint();\n } catch (IOException e) {\n JOptionPane.showMessageDialog(null, "出现异常,强制退出!");\n System.exit(0);\n }\n }\n };\n panel.setBounds(0, 0, desktop.getWidth(), desktop.getHeight());\n desktop.add(panel);\n desktop.setVisible(true);\n });\n }\n