void OnDataReceived(IAsyncResult async) { SocketPacket socketData = (SocketPacket)async.AsyncState; int iRx = 0; try { iRx = socketData.m_currentSocket.EndReceive(async); if (iRx == 0) { socketData.m_currentSocket.Close(); m_beingUsed[socketData.SocketIndex] = false; Console.WriteLine("Freed:: " + socketData.SocketIndex); } else { char[] chars = new char[iRx + 1]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0); String szData = new String(chars); WaitForData(socketData.m_currentSocket, socketData.SocketIndex); Console.Write(szData); } } catch (SocketException err) { Console.WriteLine(err.Message); } }
void WaitForData(Socket theSocket, int socketIndex) { SocketPacket thePacket = new SocketPacket(); thePacket.m_currentSocket = theSocket; thePacket.SocketIndex = socketIndex; theSocket.BeginReceive(thePacket.dataBuffer, 0, thePacket.dataBuffer.Length, SocketFlags.None, m_workerCallback, thePacket); }