Using System; Using System.Collections.Generic; Using System.Text; Using System.Net; Using System.Net.Sockets; Using Wintellect.Threading.AsyncProgModel; Namespace Extensiontest { /* * No exception handling before the two have not yet implemented continuous receive * AsyncEnumerator from Jeffrey Rich's Wintellect */ public class Network { Private Const string IP = "localhost"; Private Const int port = 10101; Private IPEndPoint EndPoint; Private Socket server; Public network () { EndPoint = new IPEndPoint (Dns.gethostentry (IP). Addresslist[0], port); Server = new Socket (AddressFamily.InterNetwork, SocketType.Stream, Protocoltype.ip); Server. Bind (EndPoint); Server. Listen (10); } public void startlistening () { Server. BeginAccept (acceptcallback, server); } public void Startlisteningwithiterator () { AsyncEnumerator ASYCN = new AsyncEnumerator (); ASYCN. Execute (this. Startasync (ASYCN)); } private void Acceptcallback (IAsyncResult asyncresult) { Socket Server = asyncresult.asyncstate as Socket; if (server!= null) { Socket receivesocket = server. Endaccept (asyncresult); byte[] buffer = new BYTE[256]; Receivesocket.beginreceive (buffer, 0, buffer. Length, Socketflags.none, ReceiveCallback, New state (Receivesocket, buffer)); // This. Startasync (Receivesocket); } } private void ReceiveCallback (IAsyncResult asyncresult) { state state = asyncresult.asyncstate as State; if (state!= null) { int recebytes = state. Socket.endreceive (asyncresult); if (Recebytes > 0) state. Socket.beginsend (state. Bytes, 0, Recebytes, Socketflags.none, Sendcallback, state. Socket); } } private void Sendcallback (IAsyncResult asyncresult) { Socket socket = asyncresult.asyncstate as socket; if (socket!= null) { Socket. Endsend (asyncresult); } } Private ienumerator<int> Startasync (AsyncEnumerator enumerator) { Server. BeginAccept (Enumerator. End (), null); Yield return 1; Socket receivesocket = server. Endaccept (Enumerator. Dequeueasyncresult ()); byte[] buffer = new BYTE[256]; Receivesocket.beginreceive (buffer, 0, buffer. Length, Socketflags.none, enumerator. End (), null); Yield return 1; int recelength = receivesocket.endreceive (enumerator. Dequeueasyncresult ()); if (Recelength > 0) { Receivesocket.beginsend (buffer, 0, Recelength, Socketflags.none, enumerator. End (), null); Yield return 1; Receivesocket.endsend (Enumerator. Dequeueasyncresult ()); } Else Yield break; } private void Startasync (socket socket) { AsyncCallback callback = null; byte[] buffer = new BYTE[256]; callback = Receresult => { int recelength = socket. EndReceive (Receresult); if (Recelength > 0) { Socket. BeginSend (buffer, 0, recelength, Socketflags.none, Sendresult => { Socket. Endsend (Sendresult); Socket. BeginReceive (buffer, 0, buffer. Length, Socketflags.none, callback, NULL); }, NULL); } }; Socket. BeginReceive (buffer, 0, buffer. Length, Socketflags.none, callback, NULL); } Private class State { Public socket sockets {get; private set;} Public byte[] Bytes {get; private set;} public State (socket socket, byte[] bytes) { this. socket = socket; this. Bytes = Bytes; } } } } |