如何使用tcplistenr使用WCF而不是手动C#?

时间:2011-03-21 15:07:35

标签: wcf tcplistener

我有以下代码,并想知道将其移至WCF是否有好处?

基本上,我正在通过某个端口侦听smtp消息,并使用线程池来异步解析每个tcp连接。

static void Main(string[] args)
{
    try
    {
    TcpListener listener = new TcpListener(IPAddress.Any, 8000);
    TcpClient client;
    listener.Start();

    while (true) // Add your exit flag here
    {
        client = listener.AcceptTcpClient();
        ThreadPool.QueueUserWorkItem(ThreadProc, client);
    }
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    Console.ReadLine();
    }

}//Main


private static void ThreadProc(object obj)
{
    var client = (TcpClient)obj;
    // Do your work here...

    NetworkStream ns = client.GetStream();

    using (StreamWriter writer = new StreamWriter(ns))
    {
       writer.WriteLine("220 SMTP server ready.");
       writer.Flush();

       using (StreamReader reader = new StreamReader(ns))
       {
          //parse + persist smtp message here...
       }
    }
}//ThreadProc

0 个答案:

没有答案