Hi I'm trying to create a tcp-server and running into issues getting the message "The operation is not allowed on non-connected sockets"
The project is an AvaloniaUI project...when I create my MainWindow I start up a TcpListener and run it on a background thread
StartSmtpLocal looks like
SmtpServer -
Line 93 in SmtpServer is where the error is happening
I have no idea what is causing the Client to disconnect so quickly after it's accepted. Any guidance in the right direction is greatly appreciated!
The project is an AvaloniaUI project...when I create my MainWindow I start up a TcpListener and run it on a background thread
Code:
private TcpListener _listener;
public MainWindow()
{
InitializeComponent();
DataContext = _vm;
_listener = new TcpListener(IPAddress.Any, 1025);
_listener.Start();
Thread smtpThread = new Thread(StartSmtpLocal);
smtpThread.IsBackground = true;
smtpThread.Start();
}
Code:
void StartSmtpLocal()
{
while (true)
{
Console.WriteLine("Waiting for connection...");
TcpClient client = _listener.AcceptTcpClient();
SMTPServer handler = new SMTPServer();
handlers.Add(handler);
handler.Initialize(client, SmtpMessageComplete);
Thread thread = new Thread(handler.Run);
// thread.IsBackground = true;
thread.Start();
}
}
Code:
public class SMTPServer
{
private TcpClient _client;
private Action<MimeMessage> _finishAction;
private MimeMessage _messageRead;
public void Initialize(TcpClient client, Action<MimeMessage> finishAction)
{
_client = client;
_finishAction = finishAction;
}
public void Run()
{
Write("220 localhost -- Fake proxy server");
// string message = String.Empty;
// MimeMessage message;
string message;
while (true)
{
message = Read();
// message = Read();
if (message.Length > 0)
{
if (message.StartsWith("QUIT"))
{
Console.WriteLine("Closing TCP Client");
_client.Close();
break;
}
if (message.StartsWith("EHLO"))
{
Write("250 OK");
}
if (message.StartsWith("RCPT TO"))
{
Write("250 OK");
}
if (message.StartsWith("MAIL FROM"))
{
Write("250 OK");
}
if (message.StartsWith("DATA"))
{
Write("354 Start mail input; end with");
string msg = Read();
MemoryStream stream = new MemoryStream();
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(msg);
writer.Flush();
stream.Position = 0;
// https://github.com/jstedfast/MimeKit
_messageRead = MimeMessage.Load(stream);
Console.WriteLine();
}
stream.Close();
Console.WriteLine();
Console.WriteLine($"Message from client {message}");
Write("250 OK");
}
}
}
Console.WriteLine("Invoking finish action");
_finishAction.Invoke(_messageRead);
}
private void Write(string message)
{
using (NetworkStream clientStream = _client.GetStream())
{
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes(message + "\r\n");
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
}
// NetworkStream clientStream = _client.GetStream();
}
private string Read()
{
byte[] messageBytes = new byte[8192];
int bytesRead = 0;
NetworkStream clientStream = _client.GetStream();
ASCIIEncoding encoder = new ASCIIEncoding();
bytesRead = clientStream.Read(messageBytes, 0, 8192);
string strMessage = encoder.GetString(messageBytes, 0, bytesRead);
return strMessage;
}
I have no idea what is causing the Client to disconnect so quickly after it's accepted. Any guidance in the right direction is greatly appreciated!