博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#UDP协议
阅读量:4641 次
发布时间:2019-06-09

本文共 2730 字,大约阅读时间需要 9 分钟。

UDP协议是不可靠的协议,传输速率快

服务器端:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Net.Sockets;using System.Net;using System.Threading;namespace UDPServer{    class Server    {        private Socket _ServerSocket;                       //服务器监听套接字        private bool _IsListionContect;                     //是否在监听        public Server()        {            //定义网络终节点(封装IP和端口)            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),1000);            //实例化套接字            _ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);            //服务端绑定地址            _ServerSocket.Bind(endPoint);            EndPoint ep = (EndPoint)endPoint;            while (true)            {                //准备一个数据缓存                byte[] msyArray = new byte[0124 * 0124];                //接受客户端发来的请求,返回真实的数据长度                int TrueClientMsgLenth = _ServerSocket.ReceiveFrom(msyArray,ref ep);                //byte数组转字符串                string strMsg = Encoding.UTF8.GetString(msyArray, 0, TrueClientMsgLenth);                //显示客户端数据                Console.WriteLine("客户端数据:" + strMsg);            }        }        static void Main(string[] args)        {            Server obj = new Server();        }    }}

客户端:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading;using System.Net;using System.Net.Sockets;namespace UDPClient{    class Client    {        private Socket _ClientSocket;                       //客户端通讯套接字        private IPEndPoint SeverEndPoint;                   //连接到服务器端IP和端口        public Client()        {            //服务器通信地址            SeverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1000);            //建立客户端Socket            _ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);            EndPoint ep =(EndPoint) SeverEndPoint;            while (true)            {                //输入信息                string strMsg = Console.ReadLine();                //退出                if (strMsg == "exit")                {                    break;                }                //字节转换                Byte[] byeArray = Encoding.UTF8.GetBytes(strMsg);                //发送数据                _ClientSocket.SendTo(byeArray,ep);                Console.WriteLine("我:" + strMsg);            }            //关闭连接            _ClientSocket.Shutdown(SocketShutdown.Both);            //清理连接资源            _ClientSocket.Close();        }        static void Main(string[] args)        {            Client obj = new Client();        }    }}

 

转载于:https://www.cnblogs.com/Optimism/p/10519127.html

你可能感兴趣的文章
1035等差数列末项计算
查看>>
CDMA鉴权
查看>>
ASP.NET MVC Identity 兩個多個連接字符串問題解決一例
查看>>
过滤器与拦截器区别
查看>>
USACO 1.5.4 Checker Challenge
查看>>
第二阶段站立会议7
查看>>
[18]Debian Linux Install GNU GCC Compiler and Development Environment
查看>>
JAVA多线程
查看>>
ACE(Adaptive Communication Environment)介绍
查看>>
delphi 更改DBGrid 颜色技巧
查看>>
python编码问题
查看>>
POJ 2031 Building a Space Station
查看>>
面向对象1
查看>>
编程开发之--java多线程学习总结(5)
查看>>
register_globals(全局变量注册开关)
查看>>
as3调用外部swf里的类的方法
查看>>
如何让 zend studio 10 识别 Phalcon语法并且进行语法提示
查看>>
任意阶幻方(魔方矩阵)C语言实现
查看>>
视频教程--ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
查看>>
第五次作业
查看>>