waiting for a request??

jorge

Well-known member
Joined
Jul 13, 2003
Messages
239
Location
Belgium
Ok, i have no id how i wait fro a request.
I have setup a tcplistner
but now i want to start a thread(i know how)
then tell the thread to wait till there is a request, now how do i do that? and will be able to turminate the thread when i pushe a stop button?

thanx in advance
 
Heres a little asynchronous service that I wrote to listen for requests. Hope it helps (I took it from an example but I cant find it again)

public class SlipLicenceService : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public SlipLicenceService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
}

// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new SlipLicenceService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// SlipLicenceService
//
this.CanShutdown = true;
this.ServiceName = "SlipLicenceManager";
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}


AsynNetServer ans;
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
ans = new AsynNetServer();
ans.Run();
}

protected override void OnStop()
{
ans.Stop();
}
}

public class AsynNetServer
{

StreamWriter sw;
public void Run()
{
sw = new StreamWriter(@"C:\pbServer.txt");
sw.WriteLine("Server Started");
listenerThread = new Thread(new ThreadStart(this.Listener));
listenerThread.Start();
}

public void Stop()
{
sw.WriteLine("Server Stopped");
sw.Close();
listener.Stop();
}

public void Suspend()
{
sw.WriteLine("Server Suspended");
listenerThread.Suspend();
}

public void Resume()
{
sw.WriteLine("Server Resumed");
listenerThread.Resume();
}

protected void Listener()
{
listener = new TcpListener(65000);
listener.Start();

while(true)
{
Socket ClientSocket = listener.AcceptSocket();
if (ClientSocket.Connected)
{
sw.WriteLine("Client Connected");
ClientHandler handler = new ClientHandler(ClientSocket, sw);
handler.StartRead();
}
}
}
private TcpListener listener;
private Thread listenerThread;

class ClientHandler
{
private byte[] buffer;
private Socket socket;
private NetworkStream networkStream;
private AsyncCallback callbackRead;
private AsyncCallback callbackWrite;

private StreamWriter sw;
public ClientHandler (Socket ClientSocket, StreamWriter nsw)
{
sw = nsw;
socket = ClientSocket;
buffer = new byte[256];
networkStream = new NetworkStream(ClientSocket);
callbackRead = new AsyncCallback(this.OnReadComplete);
callbackWrite = new AsyncCallback(this.OnWriteComplete);
}

public void StartRead()
{
networkStream.BeginRead(buffer, 0, buffer.Length, callbackRead, null);
}

private void OnReadComplete (IAsyncResult ar )
{
int bytesRead = networkStream.EndRead(ar);

if (bytesRead > 0)
{
string s = System.Text.Encoding.ASCII.GetString(buffer, 0, bytesRead);
ASCIIEncoding encoder = new ASCIIEncoding();

if (String.Compare(s,0,"Connect Request",0,15,true)==0)
{
buffer = encoder.GetBytes("Connected\r\n");
networkStream.BeginWrite(buffer, 0, buffer.Length, callbackWrite, null);
}
else if (String.Compare(s,0,"Legal",0,5,true)==0)
{
if (String.Compare(s,0,"Legal: Phil",0,11,true)==0)
buffer = encoder.GetBytes("Yes Legal\r\n");
else
buffer = encoder.GetBytes("No Legal\r\n");
networkStream.BeginWrite(buffer, 0, buffer.Length, callbackWrite, null);
}
else
{
buffer = encoder.GetBytes("No Connect\r\n");
networkStream.BeginWrite(buffer, 0, buffer.Length, callbackWrite, null);
}
sw.WriteLine(String.Format("Received {0} bytes from client: {1}", bytesRead, s ));
networkStream.BeginWrite(buffer, 0, bytesRead, callbackWrite, null);
}
else
{
sw.WriteLine("Read connection dropped");
networkStream.Close();
socket.Close();
networkStream = null;
socket = null;
}
}

private void OnWriteComplete (IAsyncResult ar)
{
networkStream.EndWrite(ar);
sw.WriteLine("Write Complete");
networkStream.BeginRead(buffer, 0, buffer.Length, callbackRead, null);
}
}
}
 
Back
Top