程序主要两个部分,一个发送消息,一个监听消息,具体看代码吧。
可用于两个程序之间的通讯,也可以实现简单的聊天。
public class UDPListen
{
private string state = "close";
public string State
{
get { return state; }
}
//自定义事件,当收到请求数据时
public event msgReceiptHandler msgReceiptEvent;
System.Net.IPAddress IP; //要进行监听的IP
System.Net.IPEndPoint S_Point; //要进行监听的端口
System.Net.Sockets.UdpClient sv;//用于监听的UDP实列
System.Net.IPEndPoint C_Point;//请求信息的主机信息
bool con = true;//是否继承监听
//构造函数
public UDPListen(string hostIP, int point)
{
IP = System.Net.IPAddress.Parse(hostIP);
S_Point = new System.Net.IPEndPoint(IP, point);
sv = new System.Net.Sockets.UdpClient(S_Point);
}
//开始监听
public void open()
{
if (con)
{
state = "open";
try
{
//Receive方法将阻塞进行,直到得到请求信息
//参数ref C_Point将得到请求信息的主机信息
byte[] msg = sv.Receive(ref C_Point);
//事件参数
msgReceiptEventArg e = new msgReceiptEventArg();
e.data = System.Text.Encoding.UTF8.GetString(msg);
e.requestIP = C_Point.Address.ToString();
e.requestPoint = C_Point.Port.ToString();
//如果有事件绑定,就触发事件
if (msgReceiptEvent != null)
{ msgReceiptEvent(this, e); }
}
catch
{
close();
}
open();//回调本方法,进行下一监听
}
}
//停止监听
public void close()
{
con = false;//是否继承监听标志为假
sv.Close(); //关闭用于监听的UDP实列
state = "close";
}
}
//自定义事件,当收到请求数据时,的委托
public delegate void msgReceiptHandler(object sender, msgReceiptEventArg e);
//自定义事件参数
public class msgReceiptEventArg : System.EventArgs
{
public string data;
public string requestIP;
public string requestPoint;
}
//***********************************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Hyey.Components.Configuration;
namespace Hyey.Components.UDPCommunication
{
public class UDPSend
{
System.Net.Sockets.UdpClient UDP_C; //用于请求的UDP实列
//构造函数
public UDPSend(string host, int point)
{
UDP_C = new System.Net.Sockets.UdpClient();
UDP_C.Connect(host, point);
}
//发送请求信息
public void sendMsg(string s)
{
byte[] msg = System.Text.Encoding.UTF8.GetBytes(s);
UDP_C.Send(msg, msg.Length);
}
#region
private static readonly UDPSend _udpsend = null;
static UDPSend()
{
XmlNode UDPNode = HyeyConfiguration.GetConfig().GetConfigSection("hyeyFrame/UDPCommunication");
XmlAttribute hostAttribute = UDPNode.Attributes["Host"];
XmlAttribute pointAttribute = UDPNode.Attributes["Point"];
_udpsend = new UDPSend(hostAttribute.Value, Convert.ToInt32( pointAttribute.Value ) );
}
public static UDPSend Instance()
{
return _udpsend;
}
private UDPSend()
{
}
#endregion
}
}
//************************************************************************************
使用,建立一个win工程,下面是Form1的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace wxwinter.wf.UDPCommunication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
send.sendMsg(this.textBox1.Text);
}
UDPListen listen;
UDPSend send;
System.Threading.Thread 线程;
private void Form1_Load(object sender, EventArgs e)
{
listen = new UDPListen("192.168.1.20",7788);
System.Threading.ThreadStart 线程方法;
线程方法 = new System.Threading.ThreadStart(listen.open);
listen.msgReceiptEvent += new msgReceiptHandler(listen_msgReceiptEvent);
线程 = new System.Threading.Thread(线程方法);
线程.Start();
send = new UDPSend("192.168.1.20", 7788);
}
//当收到请求数据时,事件
private void listen_msgReceiptEvent(object sender, msgReceiptEventArg e)
{
this.Invoke(new ThreadUIHandle(ThreadUIHandleMethod), new object[] { e.data });
}
delegate void ThreadUIHandle(object data);
void ThreadUIHandleMethod(object data)
{
this.textBox2.Text =this.textBox2.Text + data.ToString() +"\n";
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
listen.close();
线程.Abort();
}
}
}
浙公网安备 33010602011771号