using System;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Text;
using System.Threading;
using System.IO;
namespace BaseStationPDA
{
/// <summary>
/// Summary description for UdpCli.
/// </summary>
public class UdpCli
{
#region 字段
private UdpClient client ;
private IPEndPoint clientPoint ;
//定义端口号
private int ip = 127001 ;
//设定本地IP地址
private Thread startclient ;
/// <summary>
/// 客户端是否已经连接服务器
/// </summary>
private bool _isConnected = false;
/// <summary>
/// 通讯格式编码解码器
/// </summary>
private Coder _coder;
/// <summary>
/// 接收字符
/// </summary>
private string receivedData;
#endregion
#region 属性
/// <summary>
/// 返回客户端与服务器之间的连接状态
/// </summary>
public bool IsConnected
{
get
{
return _isConnected;
}
}
public Coder ServerCoder
{
get
{
return _coder;
}
}
#endregion
#region 公有方法
/// <summary>
/// 默认构造函数,使用默认的编码格式
/// </summary>
public UdpCli()
{
_coder = new Coder( Coder.EncodingMothord.Default );
}
/// <summary>
/// 构造函数,使用一个特定的编码器来初始化
/// </summary>
/// <param name="_coder">报文编码器</param>
public UdpCli( Coder coder )
{
_coder = coder;
}
#endregion
public event UDPEventHandler OnUDPMessage;
public void List(int port)
{
if(IsConnected)
{
//重新连接
Close();
}
client = new UdpClient ( port ) ;
clientPoint = new IPEndPoint ( new IPAddress ( ip ) , port ) ; //利用本地8080端口号来初始化一个UDP网络服务
startclient = new Thread ( new ThreadStart ( start_client )) ; //开一个线程
startclient.Start( ) ;//启动线程
_isConnected=true;
}
private void start_client()
{
while ( true )
{
IPEndPoint newpoint=null;
byte[] recData = client.Receive ( ref newpoint) ;//接收从远程主机发送到本地8080端口的数据
string Read_str = System.Text.Encoding.Default.GetString(recData,0,recData.Length) ;//获得客户端请求数据
string revipaddress=newpoint.Address.ToString();
string revpoint=newpoint.Port.ToString();
receivedData=Read_str;//提取客户端的信息,存放到定义为temp的字符串数组中
OnUDPMessage(this,new UDPEventArgs(receivedData,revipaddress,revpoint));
}
}
/// <summary>
/// 发送数据
/// </summary>
public static void Send(IPAddress ipaddress,int point,string content)
{
UdpClient sendclient=new UdpClient();
byte[] sendData = System.Text.Encoding.Default.GetBytes(content) ;
IPEndPoint host= new IPEndPoint(ipaddress,point) ;
sendclient.Send (sendData,sendData.Length,host ) ;
System.Windows.Forms.Application.DoEvents();
sendclient.Close() ;
}
/// <summary>
/// 关闭连接
/// </summary>
public void Close()
{
// startclient.Abort( ) ;//关闭线程
Thread.Sleep(500) ;
_isConnected = false;
client.Close ( ) ;//清除资源
}
/// <summary>
/// 获取本机IP
/// </summary>
public static string GetLocatIP()
{
string strHostName = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostByName( strHostName );
string locatip = ipEntry.AddressList[0].ToString();
return locatip;
}
}
}
using System;
namespace BaseStationPDA
{
/// <summary>
/// Summary description for UdpEventArgs.
/// </summary>
public delegate void UDPEventHandler(object sender, UDPEventArgs arg) ;
public class UDPEventArgs : EventArgs
{
public readonly string revmessage;
public readonly string revipaddress;
public readonly string revpoint;
internal UDPEventArgs(string message,string ipaddress,string point)
{
revmessage=message;
revipaddress=ipaddress;
revpoint=point;
}
}
}
前端调用
UdpCli.Send(udpip,udppoint,tstr);