Re: Threading a server
- From: "David" <david.colliver.NEWS@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 24 Apr 2009 10:22:56 +0100
Hhhmmmm
I had it fixed and working, then I put in a watchdog...
this.tcpListener.Start();
while (true)
{
//blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();
//create a thread to handle communication
//with connected client
Thread clientThread = new Thread(new
ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
// Watchdog.
// If the thread is still going after 3 minutes, abort the
thread.
//clientThread.Join(180000);
clientThread.Join(30000);
if (clientThread.IsAlive)
{
clientThread.Abort();
}
}
However, this then appears to block again. Perhaps this is what was causing
my problem before.
How do I get around this problem? (I have verified it is the watchdog by
removing it)
--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
"David" <david.colliver.NEWS@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:encVDtKxJHA.1236@xxxxxxxxxxxxxxxxxxxxxxx
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/index.php?view=article&catid=42:development&id=46:a-simple-c-multi-threaded-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
.
- Follow-Ups:
- Re: Threading a server
- From: David
- Re: Threading a server
- References:
- Threading a server
- From: David
- Threading a server
- Prev by Date: Re: Threading a server
- Next by Date: Re: How to change the frame of a form by a image?
- Previous by thread: Re: Threading a server
- Next by thread: Re: Threading a server
- Index(es):
Relevant Pages
|
Loading