标签:
UDP(User Datagram Protocol,用户数据报协议)是在互联网中常用的传输层协议,该协议提供了向另一用户程序发送的消息的最简便的协议机制。与TCP一样,其默认的下层协议是IP.UDP是面向操作的,不提供提交和复制保护,因此不能保证数据的可靠性传输。UDP一般用在可靠性较高的局域网中。
.NET 下可使用Socket 类编写基于UDP的网络程序,只要在创建Socket时将构造函数函数的第三个参数选为枚举值ProtocolType.Udp即可.此外,.NET的System.Net.Sockets命名空间下还有一个UdpClient类,可专门用于UDP的网络编程。-----摘自< C#.NET 框架高级编程技术案例教程 郭文夷等 P163>
第一种,使用Socket类
1 IPEndPoint ip = new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text)); 2 Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 3 serverSocket.Bind(ip);
第二种,使用UdpClient类
1 IPAddress address = IPAddress.Parse(txtLocalIp.Text); 2 int port = Int32.Parse(txtLocalPort.Text); 3 IPEndPoint endPoint = new IPEndPoint(address, port); 4 UdpClient udpClient = new UdpClient(endPoint);
-------------------------------------------------------------------这里是分割线-----------------------------------------------
本文主要采用第一种,基于Socket编写一个Udp发包程序。服务器端不断发包,客户端根据接收到的点数据在窗体上绘制点,效果如下所示:
一.服务器端
1.创建一个Socket对象,绑定服务器端Ip和端口号。
2.创建一个线程用户获取客户机的Ip和端口号。大致是这样的,客户机连接到服务器,同时发送一个"你好"的消息,服务器端这样就可以获取到客户机的Ip和端口。
1 private void btnStart_Click(object sender, EventArgs e) 2 { 3 if (serverSocket != null) 4 { 5 return; 6 } 7 IPEndPoint ip = new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text)); 8 serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 9 serverSocket.Bind(ip); 10 11 AppendTextToTxtLog(string.Format("服务器端是{0},正在等待客户端的连接",ip.ToString())); 12 Thread thread = new Thread(ReceiveData); 13 thread.IsBackground = true; 14 thread.Start(); 15 16 }
3.发送数据
1 private void SendData() 2 { 3 byte[] data = new byte[1024 * 1024]; 4 int j = 1000; 5 Random r = new Random(100); 6 while (j >0) 7 { 8 j--; 9 int x = r.Next(500); 10 int y = r.Next(500); 11 string msg = x.ToString() +","+ y.ToString(); 12 data = Encoding.Default.GetBytes(msg); 13 for (int k = 0; k < RemoteList.Count; k++) 14 { 15 int i = serverSocket.SendTo(data, data.Length, SocketFlags.None,RemoteList[k]); 16 AppendTextToTxtLog(string.Format("发送给客户端{0}的消息是{1}", RemoteList[k].ToString(), msg)); 17 } 18 } 19 }
客户端
1.连接到服务器端
1 private void btnConnected_Click(object sender, EventArgs e) 2 { 3 byte[] data = new byte[1024]; 4 5 IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.10.179"), 50000); 6 ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 7 string welcome = "你好!"; 8 data = Encoding.Default.GetBytes(welcome); 9 ServerSocket.SendTo(data, data.Length, SocketFlags.None, ip); 10 Thread thread = new Thread(ReceiveData); 11 thread.IsBackground = true; 12 thread.Start(); 13 }
2.接受数据
1 /// <summary> 2 /// 接受消息 3 /// </summary> 4 private void ReceiveData() 5 { 6 byte[] data = new byte[1024]; 7 IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); 8 EndPoint remote = (EndPoint)endPoint; 9 while (true) 10 { 11 int len = 0; 12 try 13 { 14 len = ServerSocket.ReceiveFrom(data, ref remote); 15 string str = Encoding.Default.GetString(data, 0, len); 16 string[] points = str.Split(‘,‘); 17 int x = int.Parse(points[0]); 18 int y = int.Parse(points[1]); 19 Point p = new Point(x, y); 20 Thread thread = new Thread(new ParameterizedThreadStart(drawPoint)); 21 thread.IsBackground = true; 22 thread.Start(p); 23 } 24 catch (Exception ex) 25 { 26 27 28 } 29 } 30 31 }
3.绘制
1 /// <summary> 2 /// 绘制 3 /// </summary> 4 /// <param name="p"></param> 5 public void drawPoint(object p) 6 { 7 8 Point point = (Point)p; 9 Graphics g = this.panel1.CreateGraphics(); 10 Pen pen = new Pen(Color.Black); 11 g.DrawEllipse(pen, point.X, point.Y, 5, 5); 12 }
源码下载: http://download.csdn.net/detail/gongchao1212/9591239
标签:
原文地址:http://www.cnblogs.com/vegetable/p/5723835.html