private void readCallback(IAsyncResult ar) { StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; //Read data from the client socket. int read_bytes = handler.EndReceive(ar); // Data was read from the client socket. if (read_bytes > 0) { DataBuilder databuilder = null; List <DataBuilder> to_remove = new List <DataBuilder>(); //Check if it's in the queue foreach (DataBuilder db in readqueue) { if (db == null) { to_remove.Add(db); } else if (db.socket == handler) { databuilder = db; } } foreach (DataBuilder db in to_remove) { readqueue.Remove(db); } //Variable that receives the buffer without tags byte[] realContent; if (databuilder == null) { realContent = new byte[read_bytes - MessageTags.tagLength]; //Get inicial tag byte[] start = new byte[MessageTags.tagLength]; Array.Copy(state.buffer, start, MessageTags.tagLength); DataType type; if (start.SequenceEqual(MessageTags.bTextStartTag)) { type = DataType.text; } else if (start.SequenceEqual(MessageTags.bImageStartTag)) { type = DataType.image; } else { throw new Exception("Invalid data"); } databuilder = new DataBuilder(handler, type); readqueue.Add(databuilder); //Remove inicial tag and zeros Array.Copy(state.buffer, MessageTags.tagLength - 1, realContent, 0, read_bytes - MessageTags.tagLength); } else { realContent = new byte[read_bytes]; //Remove zeros Array.Copy(state.buffer, 0, realContent, 0, read_bytes); } if (realContent.Length > 0) { byte[] end = new byte[MessageTags.tagLength]; Array.Copy(realContent, realContent.Length - MessageTags.tagLength, end, 0, MessageTags.tagLength); //Check if it contains the end tag if (end.SequenceEqual(MessageTags.bEndTag)) { //Remove end tag byte[] newcontent = new byte[realContent.Length - MessageTags.tagLength]; Array.Copy(realContent, 0, newcontent, 0, newcontent.Length); realContent = newcontent; databuilder.receivedAllData = true; try { if (readqueue.Contains(databuilder)) { readqueue.Remove(databuilder); } } catch (Exception e) { Output.sendMsg("Server", MessageType.Error, "Exeption: " + e.ToString()); } } //Add realcontent to dadabuilder buffer databuilder.addPackage(realContent); } Output.sendMsg("Server", MessageType.Process, "Received " + read_bytes + " bits from the client(" + handler.LocalEndPoint.ToString() + ")"); if (databuilder.receivedAllData) { Output.sendMsg("Server", MessageType.Process, "All data received from the client(" + handler.LocalEndPoint.ToString() + ")"); if (databuilder.datatype == DataType.text) { //Convert data to string string message = Encoding.ASCII.GetString(databuilder.buffer); Output.sendMsg("Server", MessageType.ContentMessage, "Received all text: " + message); if (onreceivedata != null) { onreceivedata(message, databuilder.socket); } } else if (databuilder.datatype == DataType.image) { Output.sendMsg("Server", MessageType.ContentMessage, "Received all Bitmap"); //Convert data to bitmap Bitmap bitmap = ImageManager.byteToImage(databuilder.buffer); if (onreceivebitmap != null) { onreceivebitmap(bitmap, databuilder.socket); } } } handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(readCallback), state); } else { clients_connected.Remove(handler); Output.sendMsg("Server", MessageType.Process, "Client disconnected(" + handler.LocalEndPoint.ToString() + ")"); if (onclientdisconnect != null) { onclientdisconnect(handler); } handler.Close(); } }