private void AcceptCallback(IAsyncResult ar) { this.allDone.Set(); if (!this.work) { return; } Socket socket = ((Socket)ar.AsyncState).EndAccept(ar); Server.StateObject stateObject = new Server.StateObject(); stateObject.workSocket = socket; socket.BeginReceive(stateObject.buffer, 0, 1024, SocketFlags.None, new AsyncCallback(this.ReadCallback), (object)stateObject); }
public void ReadCallback(IAsyncResult ar) { Server.StateObject asyncState = (Server.StateObject)ar.AsyncState; Socket workSocket = asyncState.workSocket; int count = workSocket.EndReceive(ar); if (count <= 0) { return; } asyncState.sb.Append(Encoding.ASCII.GetString(asyncState.buffer, 0, count)); string input = asyncState.sb.ToString(); if (input.IndexOf("<ENDRECIVE_SERVER>") > -1) { string str = Regex.Replace(input, "(<ENDRECIVE_SERVER>)", string.Empty); if (str != "") { string[] strArray = str.Split("|".ToCharArray()); string s = "0"; if (!Server.files_hash.Contains(strArray[0])) { if (Server.OnReceived != null) { Server.OnReceived(strArray[1]); } Server.files_hash.Add(strArray[0]); s = "1"; } byte[] bytes = Encoding.UTF8.GetBytes(s); workSocket.Send(bytes); } } else { workSocket.BeginReceive(asyncState.buffer, 0, 1024, SocketFlags.None, new AsyncCallback(this.ReadCallback), (object)asyncState); } }