/// <summary> /// /// </summary> /// <param name="callback">收到的字符串,处理一下,再传出相应信息</param> public void run(ProcessedDelegate callback) { Byte[] bytes = new Byte[512]; String data = null; while (true) { try { TcpClient client = server.AcceptTcpClient(); NetworkStream stream = client.GetStream(); int i; ServerStatus = true; while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { data = Encoding.ASCII.GetString(bytes, 0, i); string str = callback(data); byte[] bytes1 = System.Text.Encoding.ASCII.GetBytes(str); stream.Write(bytes1, 0, bytes1.Length); } } catch (Exception ex) { Log.Default.Error("TcpListenerServer_run", ex.Message); ServerStatus = false; } } }
static void Main(string[] args) { //DelegateEx.Example(); var eventEx = new EventEx(); eventEx.WorkPerformed += eventEx_WorkPerformed; eventEx.WorkComplete += (x, y) => Console.WriteLine("The work has complete."); //eventEx.WorkComplete += eventEx_WorkComplete; //eventEx.DoWork(5, WorkType.PaperWork); ProcessedDelegate AddDel = (x, y) => { return(x + y); }; ProcessedDelegate DelDel = (x, y) => { return(x + y); }; var result = ProcessData.ProcessSomeData(5, 1, AddDel); Console.WriteLine(result); //This removes the need to create a delegate like the one above Program. Action <int, int> ADel = (x, y) => { Console.WriteLine(x + y); }; Func <int, int, int> reAdee = (x, y) => { return(x + y); }; ProcessData.FuncAdd(3, 4, reAdee); ProcessData.OtherData(5, 1, ADel); Console.Read(); }
public static int ProcessSomeData(int x, int y, ProcessedDelegate del) { return(del(x, y)); }