Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call back in Remoting

Status
Not open for further replies.

tomcoombs

Programmer
Aug 14, 2003
65
GB
How can I tell the client if an event occurs at the sever. Without polling?



Tom
 

On the client side use Socket and TcpClient classes.
On the server side use Socket class for listener and System.ServiceProcess.ServiceBase to derive the service(s) associated. You can define events and rise events and notify the client without problem.
Let be an example:
The client connects to the server/service using a port and requires to start a process( using the Process class). When the process exits, it rises the Exit event and in the associated handler you can notify back the client about the process termination.
The listener reads the client data and passes it to the service. It takes actions and send back data to the client that requested the action (using the listener) if some events happen on the "server/service" side.

Code:
-public class Mytcp_client  // used by Mytcp_server
{
	private Socket m_sockClient;
	// define events
        public delegate void ReadEventHandler(object sender, EventArgs e);
	public event ReadEventHandler BeginRead;
        public delegate void DisconnectEventHandler(object sender, EventArgs e);
	public event DisconnectEventHandler BeginDisconnect;
        // ...

}
- public class Mytcp_server
 {
        // Events
       
	public event EventHandler event1;
        public event EventHandler event2;
        private Socket listener;
        private Mytcp_client[] clients;
        private iPort;
        public tcp_server(int port) 
		{
                //...
		this.event1+= new EventHandler(this.myEvent1);			
		}
       public void WriteLine(,)
       {
         // write data to the socket
       }
        
	

}
- public class MyServerService : System.ServiceProcess.ServiceBase 
{

    private Mytcp_server srvx ;
    protected override void OnStart(string[] args)
    {    //..
         srvx = new Mytcp_server(1434);
         //...	
        			
    }
    protected override void OnStop(){}
    protected override void OnPause(){}
    protected override void OnContinue(){}

    //
    private void Connect(object sender, EventArgs e) {}
    private void Disconnect(object sender, EventArgs e) {}
    private void Read(object sender, CPEventArgs e) 
    {
     // process client request here 
     srvx.WriteLine();
     System.Diagnostics.Process p = new System.Diagnostics.Process();
     //...
     p.Start();
     P.EnableRaisingEvents = true;
     p.StartInfo.UseShellExecute = false;
     p.Exited += new EventHandler(captureExit);
    }
    private void captureExit(object sender, EventArgs e) 
    {
      //
      srvx.WriteLine("...");
      //
    }      
    
}
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top