| Name | Date | Size | #Lines | LOC | ||
|---|---|---|---|---|---|---|
| .. | - | - | ||||
| AppScope/ | 22-Oct-2025 | - | 35 | 32 | ||
| entry/ | 22-Oct-2025 | - | 1,428 | 1,328 | ||
| hvigor/ | 22-Oct-2025 | - | 20 | 19 | ||
| screenshots/devices/ | 22-Oct-2025 | - | ||||
| .gitignore | D | 22-Oct-2025 | 100 | 7 | 7 | |
| README.md | D | 22-Oct-2025 | 1.9 KiB | 38 | 19 | |
| README_zh.md | D | 22-Oct-2025 | 5.4 KiB | 91 | 63 | |
| build-profile.json5 | D | 22-Oct-2025 | 1.3 KiB | 57 | 56 | |
| hvigorfile.ts | D | 22-Oct-2025 | 848 | 22 | 5 | |
| oh-package.json5 | D | 22-Oct-2025 | 814 | 26 | 24 |
README.md
1# Network Management - Socket 2 3### Introduction 4 5This sample demonstrates the application of Socket in network communications, including connection authentication and chat communication between two devices. 6 7 8 9### Concepts 10 11UDP Socket is a connectionless protocol, which allows sending of datagrams to the peer without first establishing a connection. The protocol is applicable when a small amount of data is transmitted at a time and real-time performance takes precedence over reliability. 12 13### Required Permissions 14 151. Internet access permission: "ohos.permission.INTERNET" 16 172. Wi-Fi access permission: "ohos.permission.GET_WIFI_INFO" 18 19### Usage Guidelines 20 211. Start the application, tap **User** to select your user ID, enter the IP address of the peer device, and click **confirm**. (Perform this step on both devices.) 22 232. On one device, tap **create room** and enter a room number. On the other device, a dialog box containing the room number is displayed. Click **confirm**. The two devices enter the chat screen. 24 253. After entering a message on one device, tap **send message**. The message will be displayed on the chat page on the peer device. 26 274. Tap the **exit** icon on the right of the title bar to return to the login user page. 28 295. Tap the nickname bar on the chat screen, and select **offline** on the displayed menu. The icons of the two devices will change to the offline state, and the nickname bar turns gray. In this case, one device cannot receive messages from the peer device. 30 316. When you tap the nickname bar to switch to the online state, the icons of the two devices will change to the online state, and the nickname bar is displayed in blue. In this case, one device can receive messages from the peer device. 32 33### Constraints 34 351. This sample can only be run on standard-system devices. 36 372. This sample requires DevEco Studio 3.0 Beta4 (Build Version: 3.0.0.992, built on July 14, 2022) to compile and run. 38
README_zh.md
1# 网络管理-Socket连接 2 3### 介绍 4 5本示例主要演示了Socket在网络通信方面的应用,展示了Socket在两端设备的连接验证、聊天通信方面的应用。 6 7### 效果预览 8 9| 输入IP | 创建房间 | 聊天 | 10|----------------------------------------|---------------------------------------|--------------------------------| 11|  |  |  | 12 13使用说明 14 151.打开应用,点击**用户**文本框选择要登录的用户,并输入另一个设备的IP地址,点击**确定**按钮进入已登录的用户页面(两个设备都要依次执行此步骤)。 16 172.在其中一个设备上点击**创建房间**按钮,任意输入房间号,另一个设备会收到有房间号信息的弹框,点击**确定**按钮后,两个设备进入聊天页面。 18 193.在其中一个设备上输入聊天信息并点击**发送**按钮后,另一个设备的聊天页面会收到该聊天消息。 20 214.点击顶部标题栏右侧的**退出**图标按钮,则返回已登录的用户页面。 22 235.点击聊天页面中的昵称栏,会弹出一个菜单,选择离线选项后,两端设备的状态图标都会切换为离线图标,并且昵称栏都会变成灰色,此时任何一端发送消息另一端都接收不到消息。 24 256.当点击昵称栏再次切换为在线状态,则两端的己方账号状态会切换为在线图标,同时两端的昵称栏会显示蓝色,此时可正常收发消息。 26 27### 工程目录 28``` 29entry/src/main/ets/entryability 30|---app.ets 31|---model 32| |---chatBox.ets // 聊天页面 33| |---DataSource.ets // 数据获取 34| |---DataType.ets // 类型封装 35| |---Logger.ets // 日志工具 36|---pages 37| |---Index.ets // 监听消息页面 38| |---Login.ets // 首页登录页面 39|---Utils 40| |---Utils.ets 41``` 42### 具体实现 43 44* 本示例分为三个模块 45 * 输入对端IP模块 46 * 使用wifi.getIpInfo()方法获取IP地址,constructUDPSocketInstance方法创建一个UDPSocket对象 47 * 源码链接:[Login.ets](entry/src/main/ets/pages/Login.ets),[Util.ets](entry/src/main/ets/utils/Util.ets) 48 * 接[README_zh.md](README_zh.md)口参考:[@ohos.wifi](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-connectivity-kit/js-apis-wifi.md),[@ohos.net.socket](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-network-kit/js-apis-socket.md),[@ohos.router](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-router.md) 49 50 * 创建房间模块 51 * 点击创建房间按钮,弹出创建房间框,输入房间号,点击确定,进入聊天页面 52 * 源码链接:[Login.ets](entry/src/main/ets/pages/Login.ets),[Util.ets](entry/src/main/ets/utils/Util.ets) 53 * 接口参考:[@ohos.wifi](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-connectivity-kit/js-apis-wifi.md),[@ohos.net.socket](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-network-kit/js-apis-socket.md),[@ohos.router](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-router.md) 54 55 * 聊天模块 56 * bindOption方法监听消息,设置在线与离线 57 * 源码链接:[ChatBox.ets](entry/src/main/ets/model/ChatBox.ets),[DataSource.ets](entry/src/main/ets/model/DataSource.ets),[Index.ets](entry/src/main/ets/pages/Index.ets) 58 * 接口参考:[@ohos.wifi](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-connectivity-kit/js-apis-wifi.md),[@ohos.net.socket](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-network-kit/js-apis-socket.md),[@ohos.router](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-router.md) 59 60#### 相关概念 61 62UDP Socket是面向非连接的协议,它不与对方建立连接,而是直接把我要发的数据报发给对方,适用于一次传输数据量很少、对可靠性要求不高的或对实时性要求高的应用场景。 63 64### 相关权限 65 661.允许使用Internet网络权限:[ohos.permission.INTERNET](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissioninternet) 67 682.允许应用获取WLAN信息权限:[ohos.permission.GET_WIFI_INFO_INTERNAL](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-system-apps.md#ohospermissionget_wifi_info_internal) 69 70### 依赖 71 72不涉及。 73 74### 约束与限制 75 761.本示例仅支持标准系统上运行,支持设备:RK3568。 77 782.本示例已适配API version 14版本SDK,版本号:5.0.2.123。 79 803.本示例需要使用DevEco Studio NEXT Release (Build Version: 5.0.3.900, built on October 8, 2024)及以上版本才可编译运行。 81 82### 下载 83 84如需单独下载本工程,执行如下命令: 85``` 86git init 87git config core.sparsecheckout true 88echo code\BasicFeature\Connectivity\Socket > .git/info/sparse-checkout 89git remote add origin https://gitee.com/openharmony/applications_app_samples.git 90git pull origin master 91```