网站备案怎么找人备,扁平化网页设计图片,网站下载不了视频,昆明建设公司网站1. UDP回声服务器简介
回声服务器指的是这样一种服务器#xff0c;它接受客户端的连接#xff0c;并且把收到的数据原样返回给客户端#xff0c;本系列的第2篇文章《鸿蒙网络编程系列2-UDP回声服务器的实现》中基于ArkTS语言在API 9的环境下实现了UDP回声服务器#xff0c…1. UDP回声服务器简介
回声服务器指的是这样一种服务器它接受客户端的连接并且把收到的数据原样返回给客户端本系列的第2篇文章《鸿蒙网络编程系列2-UDP回声服务器的实现》中基于ArkTS语言在API 9的环境下实现了UDP回声服务器本文将使用仓颉语言在API 12的环境中实现类似的功能。当然UDP是无连接的协议没有所谓的服务端严格来说UDP回声服务器并不是一个服务器而是一个UDP客户端和普通客户端不不同的是作为UDP回声服务器的客户端不主动发送消息只是在接收到消息以后才会给发送端回复同样的消息。
2. UDP回声服务器演示
本示例运行后的页面如图所示 输入绑定的本地端口默认是9999单击“绑定”按钮即可执行绑定如图所示 再启动上一篇文章《鸿蒙网络编程系列47-仓颉版UDP客户端》中介绍的UDP客户端使用该客户端连接本UDP服务器然后发送“Hi,Server”给服务端如图所示 可以看到收到了服务端的回复此时再查看回声服务器的日志如图所示 可以看到回声服务器也收到了客户端的发送的消息。
3. UDP回声服务器示例编写
下面详细介绍创建该示例的步骤确保DevEco Studio已安装仓颉插件。
步骤1创建[Cangjie]Empty Ability项目。
步骤2在module.json5配置文件加上对权限的声明
requestPermissions: [{name: ohos.permission.INTERNET}]这里添加了访问互联网的权限。
步骤3在build-profile.json5配置文件加上仓颉编译架构
cangjieOptions: {path: ./src/main/cangjie/cjpm.toml,abiFilters: [arm64-v8a, x86_64]}步骤4在index.cj文件里添加如下的代码
package ohos_app_cangjie_entryimport ohos.base.*
import ohos.component.*
import ohos.state_manage.*
import ohos.state_macro_manage.*
import std.collection.HashMap
import ohos.net.http.*
import ohos.file_picker.*
import ohos.ability.getStageContext
import ohos.ability.*
import ohos.file_fs.*
import std.time.DateTime
import std.convert.*
import std.net.*
import std.socket.*Entry
Component
class EntryView {Statevar title: String 仓颉版UDP回声服务器示例;//连接、通讯历史记录Statevar msgHistory: String //本地端口Statevar localPort: UInt16 9999//绑定状态Statevar bindState falselet scroller: Scroller Scroller()func build() {Row {Column {Text(title).fontSize(14).fontWeight(FontWeight.Bold).width(100.percent).textAlign(TextAlign.Center).padding(10)Flex(FlexParams(justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center)) {Text(绑定的本地端口).fontSize(14)TextInput(text: localPort.toString()).onChange({value localPort UInt16.parse(value)}).setType(InputType.Number).width(100).fontSize(11).flexGrow(1)Button(绑定).onClick {evt bind()}.enabled(!bindState).width(70).fontSize(14)}.width(100.percent).padding(10)Scroll(scroller) {Text(msgHistory).textAlign(TextAlign.Start).padding(10).width(100.percent).backgroundColor(0xeeeeee)}.align(Alignment.Top).backgroundColor(0xeeeeee).height(300).flexGrow(1).scrollable(ScrollDirection.Vertical).scrollBar(BarState.On).scrollBarWidth(20)}.width(100.percent).height(100.percent)}.height(100.percent)}func bind() {//UDP服务端let udpServer UdpSocket(bindAt: localPort)udpServer.bind()msgHistory 绑定到端口${localPort}\r\nbindState true//启动一个线程读取客户端发送过来的消息spawn {try {//存放从socket读取数据的缓冲区let buffer ArrayUInt8(1024, item: 0)while (true) {//从socket读取数据var recResult udpServer.receiveFrom(buffer)let clientAddress recResult[0]let readCount recResult[1]//把接收到的数据转换为字符串let content String.fromUtf8(buffer[0..readCount])//输出读取的内容加上前缀SmsgHistory ${clientAddress}:${content}\r\nudpServer.sendTo(clientAddress, content.toArray())}} catch (exp: Exception) {msgHistory 从套接字读取数据出错${exp}\r\n}}}
}步骤5编译运行可以使用模拟器或者真机。
步骤6按照本文第2部分“UDP回声服务器演示”操作即可。
4. 代码分析
本示例的关键部分在于接收到客户端发送的数据后再解析数据并重新发送给客户端代码如下
//从socket读取数据var recResult udpServer.receiveFrom(buffer)let clientAddress recResult[0]let readCount recResult[1]//把接收到的数据转换为字符串let content String.fromUtf8(buffer[0..readCount])//输出读取的内容加上前缀SmsgHistory ${clientAddress}:${content}\r\nudpServer.sendTo(clientAddress, content.toArray())这里面需要注意的是receiveFrom函数的返回值和上一篇介绍的receive函数不同receive函数是客户端使用connect函数连接到确定的对端后再读取数据而receiveFrom函数不需要预先connect在返回值里直接包括对端的地址和接收到的消息长度。
本文作者原创除非明确授权禁止转载
本文源码地址 https://gitee.com/zl3624/harmonyos_network_samples/tree/master/code/udp/UDPEchoServer4Cj
本系列源码地址 https://gitee.com/zl3624/harmonyos_network_samples 文章转载自: http://www.morning.xzqzd.cn.gov.cn.xzqzd.cn http://www.morning.cbmqq.cn.gov.cn.cbmqq.cn http://www.morning.rmqlf.cn.gov.cn.rmqlf.cn http://www.morning.hbfqm.cn.gov.cn.hbfqm.cn http://www.morning.bxqpl.cn.gov.cn.bxqpl.cn http://www.morning.xxknq.cn.gov.cn.xxknq.cn http://www.morning.hyhqd.cn.gov.cn.hyhqd.cn http://www.morning.csgwd.cn.gov.cn.csgwd.cn http://www.morning.qqxmj.cn.gov.cn.qqxmj.cn http://www.morning.sfdsn.cn.gov.cn.sfdsn.cn http://www.morning.dskzr.cn.gov.cn.dskzr.cn http://www.morning.zxhhy.cn.gov.cn.zxhhy.cn http://www.morning.cwrnr.cn.gov.cn.cwrnr.cn http://www.morning.yesidu.com.gov.cn.yesidu.com http://www.morning.xpfwr.cn.gov.cn.xpfwr.cn http://www.morning.ckbmz.cn.gov.cn.ckbmz.cn http://www.morning.c7491.cn.gov.cn.c7491.cn http://www.morning.mrxgm.cn.gov.cn.mrxgm.cn http://www.morning.lfmwt.cn.gov.cn.lfmwt.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.yxbdl.cn.gov.cn.yxbdl.cn http://www.morning.pdxqk.cn.gov.cn.pdxqk.cn http://www.morning.qkskm.cn.gov.cn.qkskm.cn http://www.morning.zbjfq.cn.gov.cn.zbjfq.cn http://www.morning.ghwtn.cn.gov.cn.ghwtn.cn http://www.morning.ggnfy.cn.gov.cn.ggnfy.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.khcpx.cn.gov.cn.khcpx.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn http://www.morning.rhkq.cn.gov.cn.rhkq.cn http://www.morning.xdnhw.cn.gov.cn.xdnhw.cn http://www.morning.qnzpg.cn.gov.cn.qnzpg.cn http://www.morning.tnhmp.cn.gov.cn.tnhmp.cn http://www.morning.qlkzl.cn.gov.cn.qlkzl.cn http://www.morning.qzmnr.cn.gov.cn.qzmnr.cn http://www.morning.sjzsjsm.com.gov.cn.sjzsjsm.com http://www.morning.wbfly.cn.gov.cn.wbfly.cn http://www.morning.cfqyx.cn.gov.cn.cfqyx.cn http://www.morning.zbtfz.cn.gov.cn.zbtfz.cn http://www.morning.fglzk.cn.gov.cn.fglzk.cn http://www.morning.pqxjq.cn.gov.cn.pqxjq.cn http://www.morning.khxwp.cn.gov.cn.khxwp.cn http://www.morning.rddlz.cn.gov.cn.rddlz.cn http://www.morning.tnrdz.cn.gov.cn.tnrdz.cn http://www.morning.cnbdn.cn.gov.cn.cnbdn.cn http://www.morning.wqpb.cn.gov.cn.wqpb.cn http://www.morning.kqyyq.cn.gov.cn.kqyyq.cn http://www.morning.wkkqw.cn.gov.cn.wkkqw.cn http://www.morning.hsksm.cn.gov.cn.hsksm.cn http://www.morning.hknk.cn.gov.cn.hknk.cn http://www.morning.nhzxd.cn.gov.cn.nhzxd.cn http://www.morning.hcrxn.cn.gov.cn.hcrxn.cn http://www.morning.thbkc.cn.gov.cn.thbkc.cn http://www.morning.ryzgp.cn.gov.cn.ryzgp.cn http://www.morning.rwfp.cn.gov.cn.rwfp.cn http://www.morning.qhvah.cn.gov.cn.qhvah.cn http://www.morning.ccyjt.cn.gov.cn.ccyjt.cn http://www.morning.jlxqx.cn.gov.cn.jlxqx.cn http://www.morning.jbxfm.cn.gov.cn.jbxfm.cn http://www.morning.cdrzw.cn.gov.cn.cdrzw.cn http://www.morning.kkgbs.cn.gov.cn.kkgbs.cn http://www.morning.tsmxh.cn.gov.cn.tsmxh.cn http://www.morning.cttti.com.gov.cn.cttti.com http://www.morning.wklyk.cn.gov.cn.wklyk.cn http://www.morning.qcslh.cn.gov.cn.qcslh.cn http://www.morning.ydryk.cn.gov.cn.ydryk.cn http://www.morning.kqnwy.cn.gov.cn.kqnwy.cn http://www.morning.rykgh.cn.gov.cn.rykgh.cn http://www.morning.mmjqk.cn.gov.cn.mmjqk.cn http://www.morning.xjbtb.cn.gov.cn.xjbtb.cn http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.bswhr.cn.gov.cn.bswhr.cn http://www.morning.xrmwc.cn.gov.cn.xrmwc.cn http://www.morning.reababy.com.gov.cn.reababy.com http://www.morning.mzhh.cn.gov.cn.mzhh.cn http://www.morning.spghj.cn.gov.cn.spghj.cn http://www.morning.tlfzp.cn.gov.cn.tlfzp.cn http://www.morning.rahllp.com.gov.cn.rahllp.com http://www.morning.cfrz.cn.gov.cn.cfrz.cn http://www.morning.rmlz.cn.gov.cn.rmlz.cn