UDP协议开发.ppt_第1页
UDP协议开发.ppt_第2页
UDP协议开发.ppt_第3页
UDP协议开发.ppt_第4页
UDP协议开发.ppt_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

UDP协议开发 1UDP协议概述2UDP协议的套接字编程技术3使用UdpClient简化UDP编程4使用UDP协议进行广播和组播 1UDP协议概述 UDP协议的特点UDP是一个简单的 面向数据报的无连接协议 提供了快速但不一定可靠的传输服务 UDP与TCP的比较优点无连接 速度快 可用于广播 组播 通信量小 有消息边界缺点不可靠 安全性差 不保证报文顺序交付 SendTo 方法和ReceiveFrom 方法UDP无需建立连接 可在任何时候直接向网络中的任意主机发送UDP数据包 当然须指明目标地址 可以直接用SendTo 方法完成这个功能 将创建的套接字与本地IPEndPoint进行绑定后 也可以用ReceiveFrom 方法接收远程主机发来的数据 使用这种方法 发送和接收UDP数据包的一般步骤为 1 创建一个Socket对象 2 如果要接收UDP数据包 需要将创建的套接字与本地IPEndPoint进行绑定 如果仅仅为了发送 则不需要绑定 3 使用SendTo 方法和ReceiveFrom 方法发送和接收数据包 2UDP协议的套接字编程技术 Send 方法和Receive 方法为了和某一个远程主机通信 也可以在创建套接字后 使用Connect 方法先和远程主机建立连接 然后直接用Send 方法和Receive 方法发送和接收数据 由于使用的是UDP数据包套接字 在通信中并没有实际的链路 但是Connect 方法将套接字信息设置到IPEndPoint对象后 Send 方法和Receive 方法就可以自动使用IPEndPoint对象的信息 解决UDP数据丢失问题UDP不保证数据被真正传送到目的地 解决方法要求远程主机返回一个应答信息 表明已经接收到发送的数据 如果在规定时间内没有收到返回的应答信息 就认为该数据包已经丢失 然后重新发送刚才发送的数据包 实现步骤为1 向远程主机发送一条消息 2 等待远程主机回送应答信息 3 如果收到应答信息 则退出本循环 继续发送下一条消息 如果在规定时间内没有收到应答信息 则重新发送 4 检查重复发送次数 如果达到规定的重复次数 则终止发送 并显示相关错误信息 程序实例一 使用SendTo 和ReceiveFrom publicstaticvoidMain intlength byte bytes newbyte 1024 Socketsocket newSocket AddressFamily InterNetwork SocketType Dgram ProtocolType Udp IPEndPointmyHost newIPEndPoint IPAddress Any 6789 socket Bind myHost IPEndPointremote newIPEndPoint IPAddress Parse 127 0 0 1 6789 EndPointremoteHost EndPoint remote Console Write 输入发送的信息 stringstr Console ReadLine bytes System Text Encoding Unicode GetBytes str socket SendTo bytes bytes Length SocketFlags None remoteHost while true Console WriteLine 等待接收 length socket ReceiveFrom bytes refremoteHost str System Text Encoding Unicode GetString bytes 0 length Console WriteLine 接收到信息 0 str if str bye break Console Write 输入回送信息 bye退出 str Console ReadLine bytes System Text Encoding Unicode GetBytes str socket SendTo bytes remoteHost socket Close Console WriteLine 对方已经byebye了 请按回车键结束 Console ReadLine 程序实例二 使用Send 和Receive publicstaticvoidMain intlength byte bytes newbyte 1024 Socketsocket newSocket AddressFamily InterNetwork SocketType Dgram ProtocolType Udp IPEndPointmyHost newIPEndPoint IPAddress Any 6789 socket Bind myHost IPEndPointremote newIPEndPoint IPAddress Parse 127 0 0 1 6789 socket Connect remote Console Write 输入发送的信息 stringstr Console ReadLine bytes System Text Encoding Unicode GetBytes str socket Send bytes while true Console WriteLine 等待接收 length socket Receive bytes str System Text Encoding Unicode GetString bytes 0 length Console WriteLine 接收到信息 0 str if str bye break Console Write 输入回送信息 bye退出 str Console ReadLine bytes System Text Encoding Unicode GetBytes str socket Send bytes socket Close Console WriteLine 对方已经byebye了 请按回车键结束 Console ReadLine 程序实例三 解决UDP协议数据丢失问题publicstaticvoidMain boolexit false intlength byte bytes newbyte 1024 Socketsocket newSocket AddressFamily InterNetwork SocketType Dgram ProtocolType Udp socket SetSocketOption SocketOptionLevel Socket SocketOptionName ReceiveTimeout 2000 IPEndPointmyHost newIPEndPoint IPAddress Any 6789 socket Bind myHost IPEndPointiep newIPEndPoint IPAddress Parse 127 0 0 1 6789 EndPointremote EndPoint iep while true intretry 0 while true try Console Write 输入发送的信息 bye退出 stringstr Console ReadLine if str bye exit true break bytes System Text Encoding Unicode GetBytes str socket SendTo bytes remote socket ReceiveFrom bytes refremote str System Text Encoding Unicode GetString bytes Console WriteLine 接收到信息 0 str catch if retry 3 retry continue else Console WriteLine 发送失败 break if exit break socket Close Console WriteLine 请按回车键结束 Console ReadLine 3使用UdpClient简化UDP编程 UdpClient的构造函数UdpClient UdpClient intport UdpClient IPEndPointiep UdpClient stringremoteHost intport UdpClient的常用方法和属性Send 发送数据报Receive 接收数据报JoinMulticastGroup 添加多地址发送 用于连接一个多播组DropMulticastGroup 除去多地址发送 用于断开UdpClient与一个多播组的连接Close 关闭 使用UdpClient发送数据发送数据要调用Send 方法来实现 但是在将数据发送到远程主机后 不接受任何形式的确认 该方法返回数据的长度 可用于检查数据是否已被正确发送 Send 方法Send byte data intlength IPEndPointiep Send byte data intlength stringremoteHostName intport Send byte data intlength 使用UdpClient接收数据Receive 方法用于在指定的本地接口和端口上接收数据 并将接收到的数据作为byte数组返回 关闭连接udpClient Close 程序实例 使用UdpClient服务器端staticvoidMain StartListener Console ReadLine privatestaticvoidStartListener UdpClientserver newUdpClient 8080 IPEndPointmyhost null try while true Console WriteLine 等待接收 byte bytes server Receive refmyhost stringstr Encoding Unicode GetString bytes 0 bytes Length Console WriteLine 接收到信息 0 str if str byebye break Console WriteLine 发送应答信息 你好 我也爱你 bytes Encoding Unicode GetBytes 你好 我也爱你 server Send bytes bytes Length 127 0 0 1 8081 server Close Console WriteLine 对方已经byebye了 请按回车键退出 catch Exceptionerr Console WriteLine err ToString 程序实例 使用UdpClient客户端编程staticvoidMain string args Send 你好 朋友 Iloveyou Send byebye Console ReadLine privatestaticvoidSend stringmessage UdpClientclient newUdpClient 8081 try Console WriteLine 向8080发送数据 0 message byte bytes System Text Encoding Unicode GetBytes message client Send bytes bytes Length 127 0 0 1 8080 if message byebye Console WriteLine 已经向对方发送byebye 请按回车退出程序 return IPEndPointhost null byte response client Receive refhost Console WriteLine 接收到返回信息 0 System Text Encoding Unicode GetString response client Close catch Exceptionerr Console WriteLine err ToString 4使用UDP协议进行广播和组播 基本概念广播组播地址广播地址组播地址 程序实例一 向子网发送广播数据包创建一个Windows应用程序 设计界面如图 程序实例一 向子网发送广播数据包 发送 按钮的Click事件代码privatevoidbutton1 Click objectsender System EventArgse 只能用UDP协议发送广播 所以ProtocolType设置为UdpSocketsocket newSocket AddressFamily InterNetwork SocketType Dgram ProtocolType Udp 程序实例一 向子网发送广播数据包 让其自动提供子网中的IP广播地址IPEndPointiep newIPEndPoint IPAddress Broadcast 6788 设置Broadcast值为1表示允许套接字发送广播信息 该值默认为0 不允许 socket SetSocketOption SocketOptionLevel Socket SocketOptionName Broadcast 1 将发送内容转换为字节数组byte bytes System Text Encoding Unicode GetBytes this textBox1 Text 向子网发送信息socket SendTo bytes iep socket Close 程序实例二 接收广播数据包创建一个Windows应用程序 设置Form1的属性 Text 属性值为 提示 FormBorderStyle 属性值为 FixedDialog MaximizeBox 属性值为 false MinimizeBox 属性值为 false 向窗体拖放一个Label控件 两个Button控件 界面如图 调用的方法 privatevoidAcceptMessage Socketsocket newSocket AddressFamily InterNetwork SocketType Dgram ProtocolType Udp IPEndPointiep newIPEndPoint IPAddress Any 6788 socket Bind iep EndPointep EndPoint iep Byte bytes newbyte 1024 while true socket ReceiveFrom bytes refep stringreceiveData System Text Encoding Unicode GetString bytes receiveData receiveData TrimEnd u0000 n n你想继续接收此类消息吗 n stringmessage 来自 ep ToString 的消息 DialogResultresult MessageBox Show receiveData message MessageBoxButtons YesNo if result DialogResult No break socket Close 重新接

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论