C# 自动刷新tcp流不会';行不通

C# 自动刷新tcp流不会';行不通,c#,tcp,tcpclient,flush,C#,Tcp,Tcpclient,Flush,正如您可能看到的,我使用C#编写的一个小型SMTP服务器。 我包括了整个代码(不包括一个类),但我希望您能很好地了解细节。 我在客户的数据帖子上苦苦挣扎,我认为问题在于“自动刷新”不起作用。 客户端向我的服务器发送“数据”,告诉我准备接收电子邮件数据。 我需要回答“354开始邮件输入”,我需要回答,我的问题是: 发送“354开始邮件输入”后,我需要在此功能中接收来自客户端的消息 using System; using System.Text; using System.Net; usin

正如您可能看到的,我使用C#编写的一个小型SMTP服务器。 我包括了整个代码(不包括一个类),但我希望您能很好地了解细节。 我在客户的数据帖子上苦苦挣扎,我认为问题在于“自动刷新”不起作用。 客户端向我的服务器发送“数据”,告诉我准备接收电子邮件数据。 我需要回答“354开始邮件输入”,我需要回答,我的问题是: 发送“354开始邮件输入”后,我需要在此功能中接收来自客户端的消息

 using System;
 using System.Text;
 using System.Net;
 using System.Net.Sockets;
 using System.Threading;

namespace FakeSMTP
{
public class SMTPServer //: IDisposable
{
    TcpClient client;
    NetworkStream stream;
    System.IO.StreamReader reader;
    System.IO.StreamWriter writer;
    //public void Dispose()
    //{
    //    writer.Dispose();
    //    reader.Dispose();
    //    stream.Dispose();
    //}


    public SMTPServer(TcpClient client)
    {
        this.client = client;
        stream = client.GetStream();
        reader = new System.IO.StreamReader(stream);
        writer = new System.IO.StreamWriter(stream);
        writer.NewLine = "\r\n";
        writer.AutoFlush = true;
    }

    static void Main(string[] args)
    {
        TcpListener listener = new TcpListener(IPAddress.Loopback, 25);
        listener.Start();
        //using (SMTPServer handler = new SMTPServer(listener.AcceptTcpClient()))
        while (true)
        {
            SMTPServer handler = new SMTPServer(listener.AcceptTcpClient());
            Thread thread = new System.Threading.Thread(new ThreadStart(handler.Run));
            thread.Start();
        }
    }

    public void Run()
    {

        string sadress;
        string radress;
        string rserver;
        bool auth = false;
        writer.WriteLine("220 smtp.localsmtp.de ESMTP server ready");
        for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
        {
            Console.Error.WriteLine("Read line {0}", line);

            if (line.StartsWith("EHLO"))
                {
                writer.WriteLine("250-smtp.localsmtp.de");
                //Auth ankuendigen
                writer.WriteLine("250 AUTH PLAIN");
                }

            if (line.StartsWith("QUIT"))
                {
                writer.WriteLine("221 Bye Sweetie see ya");
                client.Close();
                }

            #region auth

            if (line.StartsWith("AUTH PLAIN"))
            {
                Console.WriteLine("client sendet Auth: " + line);
                string [] pw = line.Split(new string[] { "PLAIN " }, StringSplitOptions.None);
                byte[] bytes = Convert.FromBase64String(pw[1]);
                string result = Encoding.BigEndianUnicode.GetString(bytes);

                if (result == "12")
                    {
                        writer.WriteLine("235 2.7.0 Authentication successful");
                        auth = true;
                    }
                else
                    {
                        Console.WriteLine("Falsche AUTH Daten");
                        writer.WriteLine("535 – Incorrect authentication data");

                    }
            }
                #endregion

            #region sender
            if (line.StartsWith("MAIL FROM") && auth == true)
                 {
                 string[] sadressa = line.Split(new string[] { "FROM:" }, StringSplitOptions.None);
                 sadress = sadressa[1];
                 //Absender
                 sadress = sadress.Replace("<","").Replace(">","");
                 //Debug
                 Console.WriteLine("Absender: " + sadress);
                 writer.WriteLine("250 OK");
                 }

            #endregion

            #region receiver
            if (line.StartsWith("RCPT TO:") && auth == true)
                {
                    string[] radressa = line.Split(new string[] { "RCPT TO:" }, StringSplitOptions.None);
                    radress = radressa[1];
                    //Empfänger
                    radress = radress.Replace("<", "").Replace(">", "");
                    if (samplesmtp.getMX.GetMXRecord(radress) != "invalid")
                    {
                        rserver = samplesmtp.getMX.GetMXRecord(radress);
                        Console.WriteLine("MX Record: " + rserver);
                    }
                    else
                        Console.WriteLine("ALARM");


                    //Debug
                    Console.WriteLine("Empfänger: " + radress);
                    writer.WriteLine("250 OK");
                }
            #endregion

            #region data

            if (line.StartsWith("DATA") && auth == true)
            {
               writer.WriteLine("354 start mail input");

               var emailLine = reader.ReadLine();
               while (!emailLine.Equals("."))
               {
                   // add emailLine to the email body
                   string[] emailbody = new string[] {emailLine};
                   Console.WriteLine("Emailbody: " + emailbody[0]); 
               }
               reader.Close();
               writer.Close();
               stream.Dispose();
               writer.WriteLine("250 OK");
            }

            #endregion

        }
        }
    }
}
使用系统;
使用系统文本;
Net系统;
使用System.Net.Sockets;
使用系统线程;
名称空间FakeSMTP
{
公共类SMTPServer/:IDisposable
{
TCP客户机;
网络流;
System.IO.StreamReader;
System.IO.StreamWriter;
//公共空间处置()
//{
//writer.Dispose();
//reader.Dispose();
//stream.Dispose();
//}
公共SMTPServer(TcpClient客户端)
{
this.client=client;
stream=client.GetStream();
reader=新系统.IO.StreamReader(流);
writer=新系统.IO.StreamWriter(流);
writer.NewLine=“\r\n”;
writer.AutoFlush=true;
}
静态void Main(字符串[]参数)
{
TcpListener侦听器=新的TcpListener(IPAddress.Loopback,25);
listener.Start();
//使用(SMTPServer处理程序=新的SMTPServer(listener.AcceptCpcClient())
while(true)
{
SMTPServer处理程序=新的SMTPServer(listener.AcceptCpcClient());
Thread-Thread=new-System.Threading.Thread(new-ThreadStart(handler.Run));
thread.Start();
}
}
公开募捐
{
弦女;
弦杆;
字符串服务器;
bool auth=false;
WriteLine(“220 smtp.localsmtp.de ESMTP服务器就绪”);
for(string line=reader.ReadLine();line!=null;line=reader.ReadLine())
{
WriteLine(“读取行{0}”,第行);
if(第行开始使用(“EHLO”))
{
WriteLine(“250 smtp.localsmtp.de”);
//作者安肯迪根
writer.WriteLine(“250 AUTH平原”);
}
如果(第行开始使用(“退出”))
{
作家。WriteLine(“221再见,亲爱的,再见”);
client.Close();
}
#区域验证
if(行StartsWith(“AUTH PLAIN”))
{
Console.WriteLine(“客户端发送端验证:+行”);
string[]pw=line.Split(新字符串[]{“普通”},StringSplitOptions.None);
byte[]bytes=Convert.FromBase64String(pw[1]);
字符串结果=Encoding.bigendianucode.GetString(字节);
如果(结果=“12”)
{
writer.WriteLine(“235 2.7.0身份验证成功”);
auth=true;
}
其他的
{
Console.WriteLine(“Falsche AUTH Daten”);
writer.WriteLine(“535–不正确的身份验证数据”);
}
}
#端区
#区域发送器
if(line.StartsWith(“邮件发件人”)&&auth==true)
{
string[]sadressa=line.Split(新字符串[]{“FROM:”},StringSplitOptions.None);
sadressa=sadressa[1];
//吊杆
sadress=sadress.Replace(“,”);
//调试
控制台写入线(“Absender:+sadress”);
writer.WriteLine(“250 OK”);
}
#端区
#区域接收器
if(line.StartsWith(“RCPT TO:”)&auth==true)
{
string[]radressa=line.Split(新字符串[]{“RCPT TO:”},StringSplitOptions.None);
radress=radressa[1];
//恩普芬格
radress=radress.Replace(“,”);
if(samplesmtp.getMX.GetMXRecord(radress)!=“无效”)
{
rserver=samplesmtp.getMX.GetMXRecord(radress);
Console.WriteLine(“MX记录:+rserver”);
}
其他的
控制台写入线(“报警”);
//调试
控制台写入线(“Empfänger:+radress”);
writer.WriteLine(“250 OK”);
}
#端区
#区域数据
if(line.StartsWith(“数据”)&&auth==true)
{
writer.WriteLine(“354开始邮件输入”);
var emailLine=reader.ReadLine();
而(!emailLine.Equals(“.”)
{
//将emailLine添加到电子邮件正文
字符串[]emailbody=新字符串[]{emailLine};
Console.WriteLine(“Emailbody:+Emailbody[0]);
}
reader.Close();
writer.Close();
stream.Dispose();
writer.WriteLine(“250 OK”);
}
#端区
}
}
}
}

尝试在代码中手动调用.Flush()根本不会改变问题。无效。

针对您对我的评论:

public class SMTPServer : IDisposable
{
    // all the other stuff

    public void Dispose()
    {
        writer.Dispose();
        reader.Dispose();
        stream.Dispose();
    }
}
呼叫代码:

static void Main(string[] args)
{
    TcpListener listener = new TcpListener(IPAddress.Loopback, 25);
    listener.Start();
    using (SMTPServer handler = new SMTPServer(listener.AcceptTcpClient()))
    {
        while (true)
        {
            Thread thread = new System.Threading.Thread(new ThreadStart(handler.Run));
            thread.Start();
        }
    }
}

在回答你的实际问题时,你需要阅读所有的行,直到你收到一封信为止。在自己的一条线上(请参见),类似这样的内容:-

var emailLine = reader.ReadLine();
while (!emailLine.Equals("."))
{
   // add emailLine to the email body
   emailLine = reader.readLine();
}
writer.WriteLine("250 OK");
reader.Close();
writer.Close();
stream.Dispose();

您的类需要实现
IDisposable
,因为它包含了相当多的对象,这些对象也实现了
IDisposable
(即其所有成员)。如何在我的上下文中使用.Dispose方法?首先感谢Jesse的回答。我在第一篇文章中输入了代码。使用您的解决方案不起作用