C# SOCKET UDP通信实例

转自:http://blog.csdn.net/adamchin/article/details/6295010


服务端:

[c-sharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.Net;  
  9. using System.Net.Sockets;  
  10. using System.Threading;  
  11.   
  12. namespace UdpServer  
  13. {  
  14.     public partial class UdpServer : Form  
  15.     {  
  16.         //创建一个Thread类  
  17.         private Thread thread1;  
  18.         //创建一个UdpClient对象,来接收消息  
  19.         private UdpClient udpReceive;  
  20.         private UdpClient udpSend;  
  21.   
  22.         public UdpServer()  
  23.         {  
  24.             InitializeComponent();  
  25.         }  
  26.         private void BtnSend_Click(object sender, EventArgs e)  
  27.         {  
  28.             //初始化UdpClient  
  29.             udpSend = new UdpClient();  
  30.             //允许发送和接收广播数据报  
  31.             udpSend.EnableBroadcast = true;  
  32.             //必须使用组播地址范围内的地址  
  33.             IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.10"), 8001);  
  34.             //将发送内容转换为字节数组  
  35.             byte[] bytes = System.Text.Encoding.UTF8.GetBytes(TbxMessage.Text);  
  36.             //设置重传次数   
  37.             int retry = 0;  
  38.             while (true)  
  39.             {  
  40.                 try  
  41.                 {  
  42.                     //发送组播信息  
  43.                     udpSend.Send(bytes, bytes.Length, iep);  
  44.                     break;  
  45.                 }  
  46.                 catch  
  47.                 {  
  48.                     if (retry < 3)  
  49.                     {  
  50.                         retry++; continue;  
  51.                     }  
  52.                     else  
  53.                     {  
  54.                         DialogResult result = MessageBox.Show("发送失败!");  
  55.                         break;  
  56.                     }  
  57.                 }  
  58.             }  
  59.             //清空TbxMesage中的内容  
  60.             TbxMessage.Clear();  
  61.             //光标还原  
  62.             TbxMessage.Focus();  
  63.         }  
  64.         private void ReceiveMessage()  
  65.         {  
  66.             //在本机指定的端口接收  
  67.             IPEndPoint remoteIpEndIPoint = new IPEndPoint(IPAddress.Any, 8002);  
  68.             udpReceive = new UdpClient(remoteIpEndIPoint);  
  69.             IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);  
  70.             //接收从远程主机发送过来的信息  
  71.             while (true)  
  72.             {  
  73.                 //ref表示引用类型的 IPPoint实例接收消息  
  74.                 byte[] receiveBytes = udpReceive.Receive(ref iep);  
  75.                 string returnData = Encoding.UTF8.GetString(receiveBytes);  
  76.                 string message = "来自" + iep.ToString() + "的消息";  
  77.                 //显示消息 并以message为消息的标题  
  78.                 DialogResult result = MessageBox.Show(returnData, message);  
  79.             }  
  80.         }  
  81.         private void UdpServer_Load(object sender, EventArgs e)  
  82.         {  
  83.             //初始化该线程并指定线程执行时要调用的方法  
  84.             thread1 = new Thread(new ThreadStart(ReceiveMessage));  
  85.             //启动线程  
  86.             thread1.Start();  
  87.         }  
  88.         private void UdpServer_FormClosing(object sender, FormClosingEventArgs e)  
  89.         {  
  90.             //关闭UdpClient连接  
  91.             udpReceive.Close();  
  92.             udpSend.Close();  
  93.             //终止线程  
  94.             thread1.Abort();  
  95.         }  
  96.   
  97.     }  
  98. }  

 

客户端:

[c-sharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.Net;  
  9. using System.Net.Sockets;  
  10. using System.Threading;  
  11.   
  12. namespace myUdpClient  
  13. {  
  14.     public partial class myUdpClient : Form  
  15.     {  
  16.         //创建一个Thread实例     
  17.         private Thread thread1;  
  18.         //创建一个UdpClient实例  
  19.         private UdpClient udpReceive;  
  20.         private UdpClient udpSend;  
  21.         private byte[] bytes;  
  22.         //private DialogResult result;  
  23.   
  24.         public myUdpClient()  
  25.         {  
  26.             InitializeComponent();  
  27.         }  
  28.         private void myUdpClient_Load(object sender, EventArgs e)  
  29.         {  
  30.             thread1 = new Thread(new ThreadStart(ReceiveMessage));  
  31.             thread1.Start();  
  32.         }  
  33.         private void ReceiveMessage()  
  34.         {  
  35.             //在本机指定的端口接收  
  36.             udpReceive = new UdpClient(8001);  
  37.             //将套接字加入组播组  
  38.             udpReceive.JoinMulticastGroup(IPAddress.Parse("224.100.0.10"), 50);  
  39.             //接收从远程主机发送过来的信息  
  40.             IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);  
  41.             while (true)  
  42.             {  
  43.                 //ref表示引用类型的 IPPoint实例接收消息  
  44.                 try  
  45.                 {  
  46.                     bytes = udpReceive.Receive(ref iep);  
  47.                 }  
  48.                 catch (SocketException e)  
  49.                 {  
  50.                     DialogResult result = MessageBox.Show("发送失败!");  
  51.                 }  
  52.                 string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);  
  53.                 string message = "来自" + iep.ToString() + "的消息";  
  54.                 //显示消息 并以message为消息的标题  
  55.                 DialogResult res = MessageBox.Show(str, message);  
  56.             }  
  57.         }  
  58.         private void BtnSend_Click(object sender, EventArgs e)  
  59.         {  
  60.             //初始化UdpClient  
  61.             udpSend = new UdpClient();  
  62.             //实际使用时应将127.0.0.1改为服务器的远程IP  
  63.             IPAddress remoteIPAddress = IPAddress.Parse("127.0.0.1");  
  64.             IPEndPoint remoteIPEndPoint = new IPEndPoint(remoteIPAddress, 8002);  
  65.             //将发送内容转换为字节数组  
  66.             byte[] bytes = System.Text.Encoding.UTF8.GetBytes(TbxMessage.Text);  
  67.             //设置重传次数   
  68.             int retry = 0;  
  69.             while (true)  
  70.             {  
  71.                 try  
  72.                 {  
  73.                     udpSend.Send(bytes, bytes.Length, remoteIPEndPoint);  
  74.                     break;  
  75.                 }  
  76.                 catch (SocketException se)  
  77.                 {  
  78.                     if (retry < 3)  
  79.                     {  
  80.                         retry++; continue;  
  81.                     }  
  82.                     else  
  83.                     {  
  84.                         DialogResult result = MessageBox.Show("发送失败!");  
  85.                         break;  
  86.                     }  
  87.                 }  
  88.             }  
  89.             //清空TbxMesage中的内容  
  90.             TbxMessage.Clear();  
  91.             //光标还原  
  92.             TbxMessage.Focus();  
  93.             //关闭UdpClient  
  94.         }  
  95.   
  96.         private void myUdpClient_FormClosing(object sender, FormClosingEventArgs e)  
  97.         {  
  98.             //关闭连接  
  99.             udpReceive.Close();  
  100.             udpSend.Close();  
  101.             //终止线程  
  102.             thread1.Abort();  
  103.         }  
  104.     }  
  105. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值