Use IPC to implement inter-process communication (implemented by the server using the Windows Service)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.