开发者

How can I use WCF instead of manual C# using tcplistenr?

开发者 https://www.devze.com 2023-02-17 23:32 出处:网络
I have the following code and was wondering if there is a be开发者_JAVA百科nefit to moving this to WCF?

I have the following code and was wondering if there is a be开发者_JAVA百科nefit to moving this to WCF?

Basically, I am listening for smtp messages over some port and use thread pooling to parse each tcp connection asynchronously.

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

精彩评论

暂无评论...
验证码 换一张
取 消