zoukankan      html  css  js  c++  java
  • 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;
    }
    }
    }

      

  • 相关阅读:
    爬虫
    Django
    python多线程
    python基础
    深度学习_1_Tensorflow_1
    人工智能_5_决策树_随机森林
    人工智能_4_k近邻_贝叶斯_模型评估
    人工智能_3_机器学习_概述
    Python re 模块
    Python函数式编程
  • 原文地址:https://www.cnblogs.com/LinFx/p/2160825.html
Copyright © 2011-2022 走看看