[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

Re: A simple mul-thread server .

Kudzu

6/4/2004 2:53:00 PM

> above is my code the tcplistener is listening on port 8080 in a single
> thread when a client connects to it the server starts a new thread to
> serve ,the server is work is vey simple ,just to record the client's
> command to an .txt file,the clients' is simple too,send a command to the

You might try Indy - it does all this low level work for you and its free.

www.indyproject.org

There is a server demo in C# here:
www.atozed.com/indy/


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower....
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com...
1 Answer

=?Utf-8?B?VG9kZCBCcmlnaHQ=?=

6/16/2004 11:37:00 PM

0

You've probably got this one figured out by now, but here goes. I think what you're wanting is a way to tell the listening thread(s) to stop listening from within the main thread. One good way to do this is to implement a class with a static property that all threads look at. Simply set the property to indicate that all threads should exit. In the worker threads read the static property to see if they should exit (probably in an idle loop or during processing).

"Amber" wrote:

> using System;
> using System.IO;
> using System.Text;
> using System.Threading;
> using System.Net.Sockets;
> using System.Net;
>
> namespace NetWork
> { public class Worker
> {
> private Thread thWorker;
> private FileStream logFile;
> private StreamWriter logWriter;
> private NetworkStream netStrm;
> private TcpClient tcpServer;
> private string cmd;
> private byte[] cmdbyte=new byte[1024];
>
> public Worker(TcpClient pserver)
> {
> this.tcpServer=pserver;
> this.netStrm=this.tcpServer.GetStream();
> }
> public void StartWork()
> {
> this.thWorker=new Thread(new ThreadStart(this.DoWork));
> this.thWorker.Start();
> }
> public void DoWork()
> {
> int bytes=this.netStrm.Read(this.cmdbyte,0,this.cmdbyte.Length);
> cmd=Encoding.Unicode.GetString(this.cmdbyte,0,bytes);
> try
> {
> this.logFile=new FileStream("S"+cmd+".txt",FileMode.OpenOrCreate);
> this.logWriter=new StreamWriter(this.logFile);
> }
> catch(Exception ex)
> {
> Console.WriteLine(ex.Message);
> this.tcpServer.Close();
> Thread.CurrentThread.Abort();
> return;
> }
> while(true)
> {
> bytes=this.netStrm.Read(cmdbyte,0,cmdbyte.Length);
> this.cmd=Encoding.Unicode.GetString(cmdbyte,0,bytes);
> this.logWriter.WriteLine(DateTime.Now.ToString()+": "+cmd);
> this.logWriter.Flush();
> if(cmd.ToLower().Equals("exit"))
> {
> break;
> }
> }
> this.logWriter.Close();
> this.logFile.Close();
> this.tcpServer.Close();
> }
> public void StopWork()
> {
> if(this.thWorker.IsAlive)
> {
> this.thWorker.Abort();
> this.logWriter.Close();
> this.logFile.Close();
> this.tcpServer.Close();
> }
> }
> }
> }
>
> using System;
> using System.Collections;
> using System.Net.Sockets;
> using System.Net;
> using System.Threading;
> using System.Text;
> using System.IO;
> namespace NetWork
> {
>
> class Manager
> {
> public const int listenedport=8080;
> private TcpListener listener;
> private Hashtable workers=new Hashtable();
> private Thread thListener;
> private int MaxWorkers=2;
> private int CurrentWorkers=1;
>
> public void AcceptClients()
> {
> IPHostEntry he=Dns.Resolve(Dns.GetHostName());
> if(he.AddressList.Length<1)
> return ;
> try
> {
> IPEndPoint ep=new IPEndPoint(he.AddressList[0],Manager.listenedport );
> this.listener =new TcpListener(ep);
> this.listener.Start();
> for(;this.CurrentWorkers<=this.MaxWorkers;this.CurrentWorkers++)
> {
> TcpClient client=this.listener.AcceptTcpClient();
> Worker wker=new Worker(client);
> this.workers.Add(this.CurrentWorkers,wker);
> wker.StartWork();
> }
>
> }
> catch(Exception ex)
> {
> Console.WriteLine(ex.Message);
> return ;
> }
> }
> public void StartWork()
> {
> this.thListener=new Thread(new ThreadStart(this.AcceptClients));
> this.thListener.Start();
> Thread.Sleep(1000);
>
> }
> public void StopWork()
> {
> try
> {
> this.listener.Stop();
> }
> catch(SocketException ex)
> {
> Console.WriteLine(ex.Message);
> }
> try
> {
> if(this.thListener.IsAlive)
> {
> this.thListener.Abort();
> }
> }
> catch(ThreadAbortException tex)
> {
> Console.WriteLine(tex.Message);
> }
> catch(Exception ex)
> {
> Console.WriteLine(ex.Message);
> }
> }
>
> [STAThread]
> static void Main(string[] args)
> {
> Manager manager=new Manager();
> manager.StartWork();
> string cmd;
> while(true)
> {
> cmd=Console.ReadLine();
> if(cmd.ToLower().Equals("exit"))
> {
> break;
> }
> }
> manager.StopWork();
> if(manager.workers.Count!=0)
> {
> IDictionaryEnumerator clients = manager.workers.GetEnumerator();
> while(clients.MoveNext())
> {
> ((Worker)clients.Value).StopWork();
> }
> }
> }
> }
> }
> using System;
> using System.Net;
> using System.Net.Sockets;
> using System.Text;
> using System.IO;
> namespace NetWorkC
> {
>
> class Client
> {
> private const int intServerport=8080;
> [STAThread]
> static void Main(string[] args)
> {
> if(args.Length<1)
> {
> Console.WriteLine("Client should be started with a parameter");
> return;
> }
> string command;
> byte[] cmdbyte=new byte[1024];
> TcpClient client=new TcpClient();
> NetworkStream strm;
>
> FileStream log=new FileStream(args[0]+".txt",FileMode.OpenOrCreate);
> StreamWriter sw=new StreamWriter(log);
> try
> {
> client.Connect(new IPEndPoint(IPAddress.Parse("192.168.0.1"),Client.intServerport));
> strm=client.GetStream();
> int bytes=Encoding.Unicode.GetBytes(args[0],0,args[0].Length,cmdbyte,0);
> strm.Write(cmdbyte,0,bytes);
>
> while(true)
> {
> command=Console.ReadLine();
> sw.WriteLine(DateTime.Now.ToString()+":"+command);
> sw.Flush();
> bytes=Encoding.Unicode.GetBytes(command,0,command.Length ,cmdbyte,0);
> strm.Write(cmdbyte,0,bytes);
> if(command.ToLower().Equals("exit"))
> break;
> }
> }
> catch(Exception ex)
> {
> Console.WriteLine(ex.Message);
> }
> finally
> {
> client.Close();
> sw.Close();
> log.Close();
> }
> }
> }
> }
>
> above is my code the tcplistener is listening on port 8080 in a single thread when a client connects to it the server starts a new thread to serve ,the server is work is vey simple ,just to record the client's command to an .txt file,the clients' is simple too,send a command to the server an record to a .txt file.when the server starts it's main thread keeps receieving command from the console when comes exit,it stop's the listener and it's thread and all the serve threads.Problems come here,when i typed exit in the server main thread ,the code this.listener.stop() throws a exception .I know the thread listener listenes is suspend because i a call the this.listener.acceptTcpClient function .But how can achieve the goal to control the behavior the listener ,I want it to stop ,it stops regardless it is block in it's thread.