I recently learned about Remoting and found that Remoting is really a good thing.
We usually have three methods to use remoting, one is
First: Publishing a public object
The public object is created locally.
Type 2: Remote creation of a public object (SAO)
Object created in client request
Third: Remote creation of a private object (CAO)
The object is created on the HOST, and the client references the object on the server.
I didn't quite understand the essential differences between the three. The three remoting creation methods are also different.
Method 1
Host:
ChannelServices. RegisterChannel (new TcpChannel (1500 ));
CTransfer Trans = new cTransfer ();
RemotingServices. Marshal (Trans, "TestService"); Client:
CTransfer T = (cTransfer) Activator. GetObject (typeof (cTransfer ),
"Tcp: // host: 1500/TestService ");
Method 2
Host:
ChannelServices. RegisterChannel (new TcpChannel (1500 ));
RemotingConfiguration. RegisterWellKnownServiceType (typeof (cTransfer ),
"TestService", WellKnownObjectMode. Singleton); Client:
CTransfer T = (cTransfer) Activator. GetObject (typeof (cTransfer ),
"Tcp: // host: 1500/TestService ");
Method 3
Host:
ChannelServices. RegisterChannel (new TcpChannel (1500 ));
RemotingConfiguration. RegisterActivatedServiceType (typeof (cTransfer); Client:
Object [] attr = {new UrlAttribute ("tcp: // host: 1500 ")};
Object [] args = {"Sample constructor argument "};
CTransfer T = (cTransfer) Activator. CreateInstance (typeof (cTransfer), args, attr );
If we need an object to allow remote call and processing, this object must inherit from the class albyrefobject.
How to transfer files in remoting? The basic idea is to open the client file in the client and convert the object that calls the host after the Byte [] type.
Objects transmitted between Client and Host
[Serializable]
Public struct kAction
{
Public string filename;
Public byte [] context;
}; Open the file and save the stream byte to the Context
Stream fileStream = File. Open (this. transFileName. Text, FileMode. Open );
FileStream. Position = 0;
Byte [] Content = new byte [(int) fileStream. Length) + 1];
FileStream. Read (Content, 0, Content. Length );
After reading the Kaction, the Host saves it to the specified folder.
MemoryStream meoeryStream = new MemoryStream (k_Action.context );
FileStream fileStream = new FileStream (@ "d: \" + k_Action.filename, FileMode. Create );
MeoeryStream. WriteTo (fileStream );
FileStream. Close ();
MeoeryStream. Close ();
New objects cannot be defined in the object. The system prompts "containing potentially dangerous types" when preparing to be sent to the HOST ".
[Serializable]
Public struct kAction
{
Public string filename;
Public byte [] context;
Public FineInfo fileInfo; // here
};
Record your experiences. If you have time, I will sort it out and write a complete article.
Cnzc's blogs