C# Socket 异步 UDP

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Runtime.InteropServices;

namespace ConsoleApplication2
{
class Program
{
static Socket m_sListen;

static void Main(string[] args)
{
new Thread(new ThreadStart(ListenThreadMothed)).Start();

Console.WriteLine(
"请按回车键结束!");
Console.ReadKey();
}

private static void ListenThreadMothed()
{
IPAddress ip
= IPAddress.Parse("222.222.222.187");
IPEndPoint ipe
= new IPEndPoint(ip, 60000);

m_sListen
= new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
m_sListen.Bind(ipe);

IPEndPoint sender
= new IPEndPoint(IPAddress.Any, 0);
EndPoint remote
= (EndPoint)sender;

for (int i = 0; i < 5; i++)
{
PostRecv(remote);
}
}

private static void PostRecv(EndPoint endPoint)
{
byte[] buffer = new byte[516];
m_sListen.BeginReceiveFrom(buffer,
0,
buffer.Length,
SocketFlags.None,
ref endPoint,
EndRecv,
new AsyncState(buffer, 0, buffer.Length, endPoint));
}

private static void PostSend(byte[] buffer, int offset, int size, EndPoint endPoint)
{
m_sListen.BeginSendTo(buffer,
offset,
size,
SocketFlags.None,
endPoint,
EndSend,
new AsyncState(buffer, offset, size, endPoint));
}

private static void EndRecv(IAsyncResult asyncResult)
{
AsyncState state
= (AsyncState)asyncResult.AsyncState;
int readBytes = m_sListen.EndReceiveFrom(asyncResult, ref state.EndPoint);

PostRecv(state.EndPoint);

//echo
//Console.WriteLine(Encoding.Default.GetString(state.buffer));
PostSend(state.Buffer, state.Offset, state.Size, state.EndPoint);
}

private static void EndSend(IAsyncResult asyncResult)
{
AsyncState state
= (AsyncState)asyncResult.AsyncState;
byte[] buffer = state.Buffer;
int sendBytes = m_sListen.EndSendTo(asyncResult);
int remainBytes = state.Size - sendBytes;
if (remainBytes <= 0)
return;
PostSend(buffer, buffer.Length
- remainBytes, remainBytes, state.EndPoint);
}

[Serializable, StructLayout(LayoutKind.Sequential)]
internal struct AsyncState
{
private readonly byte[] buffer;
private readonly int offset;
private readonly int size;

public AsyncState(byte[] buffer, EndPoint endPoint)
{
if (endPoint == null)
{
throw new ArgumentNullException("endPoint");
}
this.buffer = buffer;
this.offset = 0;
this.size = buffer.Length;
this.EndPoint = endPoint;
}

public AsyncState(byte[] buffer, int offset, int size, EndPoint endPoint)
{
if (buffer == null)
{
throw new ArgumentNullException("array");
}
if (offset < 0)
{
throw new ArgumentOutOfRangeException("offset", "ArgumentOutOfRange_NeedNonNegNum");
}
if (size < 0)
{
throw new ArgumentOutOfRangeException("count", "ArgumentOutOfRange_NeedNonNegNum");
}
if ((buffer.Length - offset) < size)
{
throw new ArgumentException("Argument_InvalidOffLen");
}
if (endPoint == null)
{
throw new ArgumentNullException("endPoint");
}
this.buffer = buffer;
this.offset = offset;
this.size = size;
this.EndPoint = endPoint;
}

public byte[] Buffer
{
get { return buffer; }
}

public int Offset
{
get { return offset; }
}

public int Size
{
get { return size; }
}

public EndPoint EndPoint;
}
}
}

  

posted @ 2011-08-31 15:56  一浩瀚星空一  阅读(2909)  评论(0编辑  收藏  举报