C # Asynchronous socket send rib class, support udp,tcp

Source: Internet
Author: User

Because of the pooling technology, you need to specify the maximum pool capacity to ensure that the memory does not leak.

In addition, the large number of SocketAsyncEventArgs created in C # can cause memory to not be recycled


Using system;using system.collections.concurrent;using system.net;using system.net.sockets;using System.Text;using    System.collections.generic;using System.Linq;    <summary>//////Ver1.2 adds the pool management for Asyncsendresult, because it consumes too much memory when sending data.    Added an uncontrolled pair of image count functions. </summary> public class Asyncsocketsendhelper {#region client Join pool///<summary>//  /Receive Saea Pool collection//</summary> private concurrentstack<socketasynceventargs> Receivesocketargspool =        New Concurrentstack<socketasynceventargs> (); <summary>///Initialize receive Socketargs object pool///</summary> protected void INITRECEIVESOCKETARGSP        Ool () {}//<summary> Create a Receive Saea object, set the maximum number of bytes Received///</summary>            <returns></returns> protected Virtual SocketAsyncEventArgs Createreceivesocketargs () { SocketasynceventArgs e = new SocketAsyncEventArgs ();            e.completed + = io_sendcomleted;        return e; }///<summary>//Leasing a receiving Saea object///</summary>//<returns></returns&gt        ;            Protected virtual SocketAsyncEventArgs Rentreveivesocketargs () {SocketAsyncEventArgs E;                     Increase the number of pairs of data System.Threading.Interlocked.Increment (ref SS) that have been emitted; Return Receivesocketargspool.trypop (out e)?        E:createreceivesocketargs ();        } private ushort _socketargspoolnumber = 200; <summary>//SocketAsyncEventArgs pool Maximum length limit///</summary> public ushort Socketargspoo            Lnumber {get {return _socketargspoolnumber;}        set {_socketargspoolnumber = value;}        }//Set buffer private byte[] arr = new byte[1];        Private long SS; <summary>///The number of pairs of images that have been created but not yet recycled, if this data exceeds 1000, indicates a risk of potential memory leaks. Asynchronous mode appears///</summary> public long Outcontrolsocketargsnumber {get {return SS;} }///<summary>///Return a receive Saea parameter///</summary>//<param name= "E" >  Also pool </param> protected virtual void Givebackreceivesocketargs (SocketAsyncEventArgs e) {if (E                 ! = NULL) {//reduce the System.Threading.Interlocked.Decrement (ref SS) that has been emitted by the pair like data;                E.usertoken = null;                    Save up to 200 buffered receiver functions if (Receivesocketargspool.count > _socketargspoolnumber) {                    Remove Event e.completed-= io_sendcomleted; E.setbuffer (arr, 0, arr.)                    Length);                    E.dispose ();                Return            } receivesocketargspool.push (e); }} #endregion///<summary>///UDP Send data Service///&LT;/Summary>//<param name= "socket" > Socket pair for sending </param>//<param name= "Udptarget" >udp far Terminal point target </param>//<param name= "Buff" > Data to be sent </param>//<param name= "CallBack" > Callback Letter The number parameters are: Send result, target node, send data </param>//<param name= "usertoken" > User data </param> public virtual void SENDTOASYNCBYUDP (socket socket, byte[] buff, EndPoint udptarget = null, action<asyncsendresult> callBack = null, obj ECT usertoken = null) {if (socket = = null) {throw new NullReferenceException            ();            } if (buff = = null) {throw new NullReferenceException ();            } if (udptarget = = null) {throw new NullReferenceException ();            } SocketAsyncEventArgs e = Rentreveivesocketargs ();            E.remoteendpoint = Udptarget; Set Send buffer E.setbuffer (buff, 0, bUff.            Length);                    try {//user ID var token = new Asyncsendresult {                    Result = False, Remoteendpoint = Udptarget.tostring (), sendtime = DateTime.Now,                    SendData = buff, Resulttime = datetime.now, CALLBAKC = CallBack,                Usertoken = Usertoken,};                E.usertoken = token; Send data if (!socket.                Sendtoasync (e)) {io_sendcomleted (socket, e); }} catch (SocketException) {//also pool Givebackreceivesocketarg            S (e);             } catch (ObjectDisposedException) {//also pool Givebackreceivesocketargs (e); }}///<summary>//Send data Service///</summary>//&LT;PARam name= "socket" > Socket pair for sending </param>//<param name= "Buff" > Data to be sent </param>//<         param name= "CallBack" > callback function parameters are: Send result, target node, send data </param>//<param name= "usertoken" > User Data </param> <returns></returns> public virtual void Sendtoasync (socket socket, byte[] buff, action<a                Syncsendresult> CallBack = NULL, object usertoken = null) {if (socket = = NULL) {            throw new NullReferenceException ();            } if (buff = = null) {throw new NullReferenceException ();            } SocketAsyncEventArgs e = Rentreveivesocketargs (); Set Send buffer E.setbuffer (buff, 0, buff.            Length);                    try {//user ID var token = new Asyncsendresult { Result = false, Remoteendpoint = socket.    Remoteendpoint.tostring (),                Sendtime = DateTime.Now, SendData = buff, Resulttime = DateTime.Now,                CALLBAKC = callBack, Usertoken = Usertoken,};                E.usertoken = token; Send data if (!socket.                SendAsync (e)) {io_sendcomleted (socket, e); }} catch (SocketException) {//also pool Givebackreceivesocketarg            S (e);             } catch (ObjectDisposedException) {//also pool Givebackreceivesocketargs (e); }}///<summary>///////<param N Ame= "Sender" ></param>//<param name= "E" ></param> protected virtual void Io_sendcomlet                Ed (object sender, SocketAsyncEventArgs e) {try {if (E.usertoken! = null) {Asyncsendresult token = E.usertoken as Asyncsendresult; if (token! = null) {//sets the result time token.                        Resulttime = DateTime.Now;                            Send data ok if (E.socketerror = = socketerror.success) { Token.                            Result = true; if (token. CALLBAKC = null) {token.                            CALLBAKC (token);                            }} else {//Send data failed Token.                            Result = false; if (token. CALLBAKC = null) {token.                            CALLBAKC (token);       }                        }                    }                }     } finally {//also pool Givebackreceivesocketargs (e); }}}///<summary>///Asynchronous Send results///</summary> public class Asyncsendresult {/        <summary>//Results///</summary> public bool result {get; set;}        <summary>///target node///</summary> public string Remoteendpoint {get; set;}        <summary>///Send data///</summary> public byte[] SendData {get; set;}        <summary>///Send time///</summary> public DateTime sendtime {get; set;}        <summary>///Results return time///</summary> public DateTime resulttime {get; set;}        <summary>////Gets or sets the user or Application object associated with this action.        </summary> public object Usertoken {get; set;} <summary>///Delegate for callback/</summary> internal action<asyncsendresult> CALLBAKC {get; set;} }


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C # Asynchronous socket send rib class, support udp,tcp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.