启用 WiFi 热点的设备上的服务器和客户端

发布于 2025-01-03 08:55:43 字数 65 浏览 1 评论 0原文

是否可以在 Android 手机上启用 WiFi 网络共享/热点,并通过两个不同的应用程序将其配置为服务器和客户端?

Is it possible to enable WiFi tethering/hotspot on an android phone and configure it to be a server as well as client through two different apps?

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

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

发布评论

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

评论(1

愛放△進行李 2025-01-10 08:55:43

您不需要两个不同的应用程序。在一个应用程序中集成两个功能。

使用java.net.Socket进行客户端实现,使用java.net.ServerSocket进行服务器端实现。

服务器端代码:

调用startServer()启动服务器侦听端口9809上的数据(输入为希望)。

void startServer() throws IOException {
        new Thread(() -> {
            try {
                serverSocket = new ServerSocket(9809);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Socket socket = null;
            try {
                socket = serverSocket.accept();
            } catch (IOException e) {
                e.printStackTrace();
            }
            DataInputStream stream = null;
            try {
                if (socket != null) {
                    stream = new DataInputStream(socket.getInputStream());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            String gotdata = null;
            try {
                if (stream != null) {
                    gotdata = stream.readUTF();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                assert socket != null;
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            System.out.println("THE DATA WE HAVE GOT :"+gotdata)

        }).start();

客户端代码:
在这里,您应该将充当服务器的设备的 IP 地址放在第 6 行中(对于我的情况,它是 192.168.1.100)。

调用sendData()将数据发送到充当服务器的设备。

void sendData() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Socket socket = new Socket("192.168.1.100", 9809);
                DataOutputStream stream = new DataOutputStream(socket.getOutputStream());
                stream.writeUTF("Some data here");
                stream.flush();
                stream.close();
                socket.close();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("Done!");
                    }
                });
            } catch (Exception e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("Fail!");
                    }
                });
                e.printStackTrace();
            }
        }
    }).start();
}

You don't need two different application. Integrate two functions in one app.

Use java.net.Socket for client-side implementation and java.net.ServerSocket for server-side implementation.

Server-Side Code:

Call startServer() to start server listening for data at port 9809(Put as you wish).

void startServer() throws IOException {
        new Thread(() -> {
            try {
                serverSocket = new ServerSocket(9809);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Socket socket = null;
            try {
                socket = serverSocket.accept();
            } catch (IOException e) {
                e.printStackTrace();
            }
            DataInputStream stream = null;
            try {
                if (socket != null) {
                    stream = new DataInputStream(socket.getInputStream());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            String gotdata = null;
            try {
                if (stream != null) {
                    gotdata = stream.readUTF();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                assert socket != null;
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            System.out.println("THE DATA WE HAVE GOT :"+gotdata)

        }).start();

Client-Side Code:
Here, You should put the IP address of the device acting as server in line 6(For my case, it was 192.168.1.100).

Call sendData() to send data to the device acting as server.

void sendData() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Socket socket = new Socket("192.168.1.100", 9809);
                DataOutputStream stream = new DataOutputStream(socket.getOutputStream());
                stream.writeUTF("Some data here");
                stream.flush();
                stream.close();
                socket.close();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("Done!");
                    }
                });
            } catch (Exception e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("Fail!");
                    }
                });
                e.printStackTrace();
            }
        }
    }).start();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文