# Region receives socket data asynchronously /// <summary> // receives socket data asynchronously /// </Summary> /// <Param name = "socket"> </Param >/// <returns> </returns> Public static byte [] receivedata (Socket socket) {receiveobject state = new receiveobject () {client = socket}; socket. beginreceive (state. buffer, 0, state. buffer. length, socketflags. none, new asynccallback (readcallback), State); State. receivedone. waitone (1000*10); // 10 seconds timeout byte [] result = state. stream. toarray (); return result ;}/// <summary> /// synchronous receiving object /// </Summary> private class receiveobject {public Socket Client; public byte [] buffer = new byte [4096]; public system. io. memorystream stream = new system. io. memorystream (); public system. threading. manualresetevent receivedone = new system. threading. manualresetevent (false );} /// <summary> /// read callback /// </Summary> /// <Param name = "Ar"> </param> Private Static void readcallback (iasyncresult ar) {receiveobject state = (receiveobject) ar. asyncstate; int bytesread = state. client. endreceive (AR); If (bytesread> 0) {try {state. stream. write (state. buffer, 0, bytesread); State. client. beginreceive (state. buffer, 0, state. buffer. length, socketflags. none, new asynccallback (readcallback), State);} catch (exception ex) {log. error (ex. message); State. receivedone. set () ;}} else {state. receivedone. set () ;}# endregion