1、发送方代码

void SendMsg(string toip, int port )
{
try
{
string message="发送内容";
UdpClient udpclient = new UdpClient();
IPEndPoint ipendpoint = new IPEndPoint(IPAddress.Parse(ip), port); byte[] data = Encoding.Default.GetBytes(message);
udpclient.Send(data, data.Length, ipendpoint);
udpclient.Close(); }
catch (Exception ex)
{ MessageBox.Show("UDP发送数据" + ex.ToString());
} }

2、接收方代码

 public UdpService udp;

a>实例化、初始化接收事件

udp = new UdpService(fromip, , "127.0.0.1", );
udp.EvtGetValues+=new UdpService.GetRecevice(udp_EvtGetValues); b>启动UDP udp.TurnOn(); c>停止UDP udp.TurnOff(); d>接收事件处理
void udp_EvtGetValues(byte[] ReceviceBuff)
{
//string message = Encoding.UTF8.GetString(ReceviceBuff, 0, ReceviceBuff.Length);
//可接收中文内容
Encoding ei=Encoding.GetEncoding();
string message = ei.GetString(ReceviceBuff, , ReceviceBuff.Length);
if (message.Length > )
{
// 处理接收逻辑 } }

3、UdpService代码

 public  class UdpService
{
#region 内部变量 string devIP = "127.0.0.1";
int devPort = ;
UdpClient mySocket;
string meIP = "127.0.0.1";
int mePort = ;
IPEndPoint RemotePoint;
bool isRunning = false;
bool isOpen = false;
List<Thread> listThread = new List<Thread>();
string icId = "";
string cardContent = "";
byte[] cardContentBuf;
#endregion public UdpService(string ServerIP, int ServerPort, string DevIP, int DevPort)
{
this.meIP = ServerIP;
this.mePort = ServerPort;
this.devIP = DevIP;
this.devPort = DevPort; } #region public void TurnOn()
{
try
{
if (isOpen) return;
mySocket = new UdpClient(mePort);
IPEndPoint ipLocalPoint = new IPEndPoint(IPAddress.Parse(meIP), mePort); RemotePoint = new IPEndPoint(IPAddress.Any, devPort); isRunning = true;
Thread thread = new Thread(new ThreadStart(this.ReceiveHandle));
thread.IsBackground = true;
thread.Start();
listThread.Add(thread);
isOpen = true; }
catch (Exception ex)
{
isOpen = false;
throw new Exception(ex.Message);
}
} public void TurnOff()
{
try
{
isOpen = false;
isRunning = false; for (int i = ; i < listThread.Count; i++)
{
try
{
listThread[i].Abort();
}
catch (Exception) { }
} if (mySocket != null)
{
mySocket.Close();
}
}
catch (Exception)
{
}
} public delegate void GetRecevice(byte[] ReceviceBuff);
public event GetRecevice EvtGetValues;
private void ReceiveHandle()
{
byte[] sendbuf = new byte[];
byte[] sendwritbuf = new byte[]; while (isRunning)
{
try
{
if (mySocket == null || mySocket.Available < )
{
Thread.Sleep();
continue;
}
//接收UDP数据报,引用参数RemotePoint获得源地址
byte[] buf = mySocket.Receive(ref RemotePoint);
if (devIP == null || devIP.Length < )
{
devIP = RemotePoint.Address.ToString();
devPort = RemotePoint.Port;
}
if (EvtGetValues != null)
{
EvtGetValues(buf);
}
}
catch (Exception)
{
}
}
}
#endregion
}

最新文章

  1. div中设置滚动条的问题
  2. 51nod p1201 整数划分
  3. hibernate-DetachedCriteria实现关联表条件复查
  4. C# 非UI线程对控件的控制
  5. 桐桐的贸易--WA
  6. (17)odoo方法和修饰器
  7. leetcode:Intersection of Two Linked Lists(两个链表的交叉点)
  8. C#反编译工具 ILSPY-x64可动态调试-君临汉化版
  9. Universal-Image-Loader源码解解析---display过程 + 获取bitmap过程
  10. i春秋官网4.0上线啦 文末有福利
  11. DeeplabV3+ 训练自己的遥感数据
  12. 关系网络数据可视化:2. Python数据预处理
  13. java.lang.NumberFormatException: multiple points错误问题
  14. 转 利用 Console 来学习、调试JavaScript
  15. SSL编程(2).NET最简单的客户端
  16. &lt;摘录&gt;开源软件架构-ZeroMQ
  17. linux小白
  18. UISearchDisplayController
  19. Python学习---匿名函数和闭包的学习
  20. 通过SectionIndexer实现微信通讯录

热门文章

  1. java连接sqlserver数据简单操作
  2. 吴裕雄--天生自然KITTEN编程:拾金币
  3. IDEA工具java.io.IOException: Could not find resource SqlMapConfig.xml
  4. 我们为什么不愿意相信AI?
  5. 【年度开源、工具合集】牛津计划,DMTK,Graph Engine…提高你的工作效率!
  6. Git常用的操作指令
  7. 【WPF学习】第五十章 故事板
  8. 30分钟学会Objective-C
  9. 【转】css样式自动换行(强制换行)
  10. 前端面试题(HTML、CSS部分)