Client.ReceiveBufferSize = 1024 * 4 * 1024 * 256;//1G,modified 02.02
public void Start()
{if (!_started)
{
_started = true;//已经开始
ReceiveInternal();
}
}
protected void ReceiveInternal()
{
if (!_started)//判断开始没有
{
return;
}
try
{
UdpClient.BeginReceive(
new AsyncCallback(ReceiveCallback),
null);
}
catch (SocketException ex)
{
throw ex;
}
}
private void ReceiveCallback(IAsyncResult result)
{
if (!_started)
{
return;
}
IPEndPoint remoteIP = new IPEndPoint(IPAddress.Any, 0);
byte[] buffer = null;
try
{
buffer = UdpClient.EndReceive(result, ref remoteIP);
// 触发ReceiveData事件
OnReceiveData(new ReceiveDataEventArgs(buffer, remoteIP));
}
catch (SocketException ex)
{
throw ex;
}
finally
{
//modified 0220
Array.Clear(buffer, 0, buffer.Length);
ReceiveInternal();
}
}
protected virtual void OnReceiveData(ReceiveDataEventArgs e)
{
if (ReceiveData != null)
{
ReceiveData(this, e);//引发事件,处理数据
}
}