Servidor
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 |
import java.net.DatagramPacket import java.net.DatagramSocket import java.awt.Robot import java.awt.MouseInfo import java.awt.PointerInfo fun main() { val port = 2020 val serverSocket = DatagramSocket(port) val buffer = ByteArray(1024) val packet = DatagramPacket(buffer, buffer.size) serverSocket.receive(packet) val receivedData = String(packet.data, 0, packet.length) val positions = receivedData.split("*") val x = positions[0].toInt() val y = positions[1].toInt() // Aquí debes implementar la lógica para mover el mouse a las coordenadas (x, y) val robot = Robot() val mouseInfo: PointerInfo = MouseInfo.getPointerInfo() val location = mouseInfo.location // Calcula la diferencia entre la ubicación actual y las coordenadas recibidas val deltaX = x - location.x val deltaY = y - location.y // Mueve el ratón a las coordenadas recibidas robot.mouseMove(location.x + deltaX, location.y + deltaY) serverSocket.close() } |
Cliente
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.net.DatagramPacket import java.net.DatagramSocket import java.net.InetAddress import java.awt.MouseInfo fun main() { val serverAddress = InetAddress.getLoopbackAddress() val serverPort = 2020 val clientSocket = DatagramSocket() val mouseLocation = MouseInfo.getPointerInfo().location val x = mouseLocation.x val y = mouseLocation.y val message = "$x*$y" val sendData = message.toByteArray() val packet = DatagramPacket(sendData, sendData.size, serverAddress, serverPort) clientSocket.send(packet) clientSocket.close() } |