Net to see the convenient implementation of inter-process communication through netremoting, the following is a specific summary:
1. First define the communication interface and encapsulate it into a library:
NOTE: If custom types are involved in communication, you must add the [serializable] attribute to serializable the class.
[Serializable]
Public class ipcinfo
{
...
}
Public interface ipcdll
{
Void setinfo (pcinfo );
Pcinfo getinfo ();
}
2. Server
It is usually used by the server in the background, so it is very suitable for using Windows Services.
1) Implementation Interface
Public class ipcsrvinfo:Marshalbyrefobject, Ipcdll // required albyrefobject is required and must be the first
{
Public void setinfo (pcinfo)
{
...
}
Pcinfo getinfo ();
{
...
}
}
2) create an IPC Server
System. Collections. idictionary dicprop = new system. Collections. hashtable ();
Dicprop ["Priority"] = "20 ";
Dicprop ["portname"] = "myipc ";
Dicprop ["authorizedgroup"] = "everyone"; // This statement is required if it runs as a service; otherwise, the client has no connection permission.
Ipcserverchannel srvchannel = new ipcserverchannel (dicprop, null );
Channelservices. registerchannel (srvchannel, false );
Remotingconfiguration. registerwellknownservicetype (
Typeof (ipcsrvinfo ),
"Srvinfo ",
Wellknownobjectmode. Singleton
);
For more information about how to install and start a service, see 'C # compile a Windows Service and install and start ';
3) Client
After the server is started, the client can connect
Ipcclientchannel clientchan = new ipcclientchannel ();
Channelservices. registerchannel (clientchan, false );
String logurl = logipc. getfullurl (logipc. addloguriname );
Ipcdll logfun = (ipcdll) remotingservices. Connect (
Typeof (ipcdll ),
"IPC: // myipc/srvinfo"
);
After obtaining it, you can use logfun to call methods in the interface.