Android屏幕作为内网中的PC触摸板

发布于 2024-12-20 07:07:07 字数 81 浏览 0 评论 0原文

我想使用android屏幕作为内网中的电脑触摸板, 因此,对于我应该使用哪种协议以及如何在java中创建pc服务器。

帮我。 谢谢

I want to use android screen as a pc touchpad in intranet,
so for that which protocol i should use and how to create pc server in java.

Help me.
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疾风者 2024-12-27 07:07:07

它非常容易使用。使用 UDP 进行 Intranet 内的连接,因为它更快、无连接,因此无需刷新缓冲区并等待传输。您的应用程序应该实时运行,因此您需要 UDP 套接字连接。

在服务器端java中,只需创建一个带有端口号的数据报包服务器。仅使用字节流来实现更快的连接。

分享了示例代码。这里我使用8字节数据包大小:

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

}

//use reverse of this logic in your android to convert co-ordinate int to byte[]
public static final int byteArrayToInt1(byte[] b)
{
    return (b[0] << 24)
            + ((b[1] & 0xFF) << 16)
            + ((b[2] & 0xFF) << 8)
            + (b[3] & 0xFF);
}

public static final int byteArrayToInt2(byte[] b)
{
    return (b[4] << 24)
            + ((b[5] & 0xFF) << 16)
            + ((b[6] & 0xFF) << 8)
            + (b[7] & 0xFF);
    }
}

Its very easy to use. Use UDP for connectivity within your intranet, because it's faster, connectionless, so there's no need to flush the buffer and wait for transmit. Your application should run in real time, so you require UDP socket connectivity.

In Server side java, just create a Datagram packet server with a port number. Use only byte stream for faster connectivity.

Shared a sample code. Here I used 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();
    }

}

//use reverse of this logic in your android to convert co-ordinate int to byte[]
public static final int byteArrayToInt1(byte[] b)
{
    return (b[0] << 24)
            + ((b[1] & 0xFF) << 16)
            + ((b[2] & 0xFF) << 8)
            + (b[3] & 0xFF);
}

public static final int byteArrayToInt2(byte[] b)
{
    return (b[4] << 24)
            + ((b[5] & 0xFF) << 16)
            + ((b[6] & 0xFF) << 8)
            + (b[7] & 0xFF);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文