It is very easy to use. Use UDP to connect within your intranet because it is faster without a connection, so there is no need to clear the buffer and wait for the transfer. Your application should work in real time, so you need to connect UDP sockets.
On the java server side, just create a datagram packet server with a port number. Use only byte stream for faster connection.
General example code. Here I used an 8-byte packet size:
public class Server extends Thread { LinkedList<String[]> queue = new LinkedList<String[]>(); public static void main(String[] args) { new Server().start(); } @Override public void run() { try { int server_port = 12345; byte[] message = new byte[8]; DatagramPacket p = new DatagramPacket(message, message.length); DatagramSocket s = new DatagramSocket(server_port); Robot r = new Robot(); PointerInfo a = MouseInfo.getPointerInfo(); Point b = a.getLocation(); int curx = (int) b.getX(); int cury = (int) b.getY(); int prex = 0; int prey = 0; while (true) { p = new DatagramPacket(message, 8); s.receive(p); System.out.println(p.getAddress()); int x = byteArrayToInt1(message); int y = byteArrayToInt2(message); if (x == 0 && y == 0) { prex = 0; prey = 0; a = MouseInfo.getPointerInfo(); b = a.getLocation(); curx = (int) b.getX(); cury = (int) b.getY(); r.mouseMove(curx, cury); } else { curx = curx - (prex - x); cury = cury - (prey - y); r.mouseMove(curx, cury); prex = x; prey = y; } } } catch (Exception e) { e.printStackTrace(); } }
source share