D
David
Hi all,
A while ago, I asked about threading, but I am new to it and really
struggling.
I thought I had it cracked and the project has gone live, but quickly
presented problems due to the threading.
I have the basic smtp mailserver from here, that I have modified to suit me.
http://forums.whirlpool.net.au/forum-replies-archive.cfm/654973.html
However, it has a blocking AcceptTcpClient which is not good for me. There
are potentially many connections required at a time.
Here is my code so far...
*******************************************************************************
private void ReceiveSMTP()
{
System.Net.IPAddress ipaddress = System.Net.IPAddress.Any;
int PortNo =
Convert.ToInt32(ConfigurationManager.AppSettings["SMTPPort"]);
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += delegate(object sender, DoWorkEventArgs e)
{
Thread runThread = new Thread(new ThreadStart(RunSMTP));
runThread.Start();
};
bw.RunWorkerCompleted += delegate(object sender,
RunWorkerCompletedEventArgs e)
{
string MailContent = (string)e.Result;
};
bw.RunWorkerAsync();
}
private void RunSMTP()
{
System.Net.IPAddress ipaddress = System.Net.IPAddress.Any;
int PortNo =
Convert.ToInt32(ConfigurationManager.AppSettings["SMTPPort"]);
TcpListener listener = new TcpListener(ipaddress, PortNo);
listener.Start();
while (true)
{
SMTP.SMTPServer handler = new
SMTP.SMTPServer(listener.AcceptTcpClient());
handler.FilePath =
ConfigurationManager.AppSettings["TempDir"];
handler.LogToFile = LogToFile;
handler.LogVerbose = LogVerbose;
handler.ExpectedSubject =
ConfigurationManager.AppSettings["ExpectedSubjectStart"];
handler.LimitIPAddress =
ConfigurationManager.AppSettings["AllowedIPAddress"];
handler.PenHandlerURL =
ConfigurationManager.AppSettings["PenHandlerURL"];
Thread thread = new System.Threading.Thread(new
ThreadStart(handler.Run));
thread.Start();
// Watchdog.
// If the thread is still going after 3 minutes, abort the
thread.
thread.Join(180000);
if (thread.IsAlive)
{
thread.Abort();
}
}
}
*******************************************************************************
The bw.RunWorkerCompleted is no longer being used. My original question was
about getting information out of the thread. I have gone the other way and
am now passing information into the thread and letting the thread do the
remaining part of hte work.
The handler.Run calls the Run in the mailserver from the link above. (There
are a few modifications that the mailserver.)
However, the problem is that even though I have attempted to put RunSMTP
into its own thread, I now realise that this wouldn't work, but don't have
enough knowledge to fix it.
If I send one email at a time to the server, it works fine, and as long as
that email finishes before the next email, everything is hunky dorey
(thereby leading me into a false sense of security).
However, while one mail is being handled, then if another comes in, then it
is sat waiting until the first one finishes, and if the first one fails to
send a quit command, it is waiting until my thread.Abort is done. (3
minutes).
I have also tried
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.beginaccepttcpclient.aspx
to work around the blocking issue, but I REALLY don't understand it. I have
also tried
http://thesoftwareeconomist.com/ind...eaded-tcp-server&tmpl=component&print=1&page=
and putting my handler into the ListenerThreadDelegate
protected static void ListenerThreadDelegate(object obj)
{
IPAddress ipAddress = (IPAddress)obj;
TcpListener listener = null;
try
{
if (ipAddress != null)
{
listener = new TcpListener(((IPAddress)ipAddress),
portToListenOn); //_port);
TCPListenerCollection.Add(listener);
listener.Start();
while (true)
{
try
{
if (listener.Pending())
{
Socket clientSocket =
listener.AcceptSocket();
// Old SMTP
SMTP.SMTPServer handler = new
SMTP.SMTPServer(listener.AcceptTcpClient());
handler.FilePath =
ConfigurationManager.AppSettings["TempDir"];
handler.LogToFile = LogToFile;
handler.LogVerbose = LogVerbose;
handler.ExpectedSubject =
ConfigurationManager.AppSettings["ExpectedSubjectStart"];
handler.LimitIPAddress =
ConfigurationManager.AppSettings["AllowedIPAddress"];
handler.PenHandlerURL =
ConfigurationManager.AppSettings["PenHandlerURL"];
// Old
//Thread thread = new
System.Threading.Thread(new ThreadStart(handler.Run));
//thread.Start();
// New
//Thread newThread = new Thread(new
ParameterizedThreadStart(SimpleTCPServer.ProcessMessages));
Thread newThread = new Thread(new
ParameterizedThreadStart(handler.Run));
newThread.Start(clientSocket);
// Watchdog.
// If the thread is still going after 3
minutes, abort the thread.
newThread.Join(180000);
if (newThread.IsAlive)
{
newThread.Abort();
}
// End Old SMTP
}
}
catch (SocketException ex)
{
//Log error
}
}
}
else
{
return;
}
}
catch (System.Net.Sockets.SocketException exception)
{
//Log error
}
catch (Exception exception)
{
//Log error
}
}
but I am just getting high processor usage AND for some reason, it is not
even picking up my connection.
Any help on how I should make this work would be VERY MUCH appreciated. I
have a live server offline at the moment. (Oh, I am using .NET 3.5, C#)
--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
A while ago, I asked about threading, but I am new to it and really
struggling.
I thought I had it cracked and the project has gone live, but quickly
presented problems due to the threading.
I have the basic smtp mailserver from here, that I have modified to suit me.
http://forums.whirlpool.net.au/forum-replies-archive.cfm/654973.html
However, it has a blocking AcceptTcpClient which is not good for me. There
are potentially many connections required at a time.
Here is my code so far...
*******************************************************************************
private void ReceiveSMTP()
{
System.Net.IPAddress ipaddress = System.Net.IPAddress.Any;
int PortNo =
Convert.ToInt32(ConfigurationManager.AppSettings["SMTPPort"]);
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += delegate(object sender, DoWorkEventArgs e)
{
Thread runThread = new Thread(new ThreadStart(RunSMTP));
runThread.Start();
};
bw.RunWorkerCompleted += delegate(object sender,
RunWorkerCompletedEventArgs e)
{
string MailContent = (string)e.Result;
};
bw.RunWorkerAsync();
}
private void RunSMTP()
{
System.Net.IPAddress ipaddress = System.Net.IPAddress.Any;
int PortNo =
Convert.ToInt32(ConfigurationManager.AppSettings["SMTPPort"]);
TcpListener listener = new TcpListener(ipaddress, PortNo);
listener.Start();
while (true)
{
SMTP.SMTPServer handler = new
SMTP.SMTPServer(listener.AcceptTcpClient());
handler.FilePath =
ConfigurationManager.AppSettings["TempDir"];
handler.LogToFile = LogToFile;
handler.LogVerbose = LogVerbose;
handler.ExpectedSubject =
ConfigurationManager.AppSettings["ExpectedSubjectStart"];
handler.LimitIPAddress =
ConfigurationManager.AppSettings["AllowedIPAddress"];
handler.PenHandlerURL =
ConfigurationManager.AppSettings["PenHandlerURL"];
Thread thread = new System.Threading.Thread(new
ThreadStart(handler.Run));
thread.Start();
// Watchdog.
// If the thread is still going after 3 minutes, abort the
thread.
thread.Join(180000);
if (thread.IsAlive)
{
thread.Abort();
}
}
}
*******************************************************************************
The bw.RunWorkerCompleted is no longer being used. My original question was
about getting information out of the thread. I have gone the other way and
am now passing information into the thread and letting the thread do the
remaining part of hte work.
The handler.Run calls the Run in the mailserver from the link above. (There
are a few modifications that the mailserver.)
However, the problem is that even though I have attempted to put RunSMTP
into its own thread, I now realise that this wouldn't work, but don't have
enough knowledge to fix it.
If I send one email at a time to the server, it works fine, and as long as
that email finishes before the next email, everything is hunky dorey
(thereby leading me into a false sense of security).
However, while one mail is being handled, then if another comes in, then it
is sat waiting until the first one finishes, and if the first one fails to
send a quit command, it is waiting until my thread.Abort is done. (3
minutes).
I have also tried
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.beginaccepttcpclient.aspx
to work around the blocking issue, but I REALLY don't understand it. I have
also tried
http://thesoftwareeconomist.com/ind...eaded-tcp-server&tmpl=component&print=1&page=
and putting my handler into the ListenerThreadDelegate
protected static void ListenerThreadDelegate(object obj)
{
IPAddress ipAddress = (IPAddress)obj;
TcpListener listener = null;
try
{
if (ipAddress != null)
{
listener = new TcpListener(((IPAddress)ipAddress),
portToListenOn); //_port);
TCPListenerCollection.Add(listener);
listener.Start();
while (true)
{
try
{
if (listener.Pending())
{
Socket clientSocket =
listener.AcceptSocket();
// Old SMTP
SMTP.SMTPServer handler = new
SMTP.SMTPServer(listener.AcceptTcpClient());
handler.FilePath =
ConfigurationManager.AppSettings["TempDir"];
handler.LogToFile = LogToFile;
handler.LogVerbose = LogVerbose;
handler.ExpectedSubject =
ConfigurationManager.AppSettings["ExpectedSubjectStart"];
handler.LimitIPAddress =
ConfigurationManager.AppSettings["AllowedIPAddress"];
handler.PenHandlerURL =
ConfigurationManager.AppSettings["PenHandlerURL"];
// Old
//Thread thread = new
System.Threading.Thread(new ThreadStart(handler.Run));
//thread.Start();
// New
//Thread newThread = new Thread(new
ParameterizedThreadStart(SimpleTCPServer.ProcessMessages));
Thread newThread = new Thread(new
ParameterizedThreadStart(handler.Run));
newThread.Start(clientSocket);
// Watchdog.
// If the thread is still going after 3
minutes, abort the thread.
newThread.Join(180000);
if (newThread.IsAlive)
{
newThread.Abort();
}
// End Old SMTP
}
}
catch (SocketException ex)
{
//Log error
}
}
}
else
{
return;
}
}
catch (System.Net.Sockets.SocketException exception)
{
//Log error
}
catch (Exception exception)
{
//Log error
}
}
but I am just getting high processor usage AND for some reason, it is not
even picking up my connection.
Any help on how I should make this work would be VERY MUCH appreciated. I
have a live server offline at the moment. (Oh, I am using .NET 3.5, C#)
--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available