Recently, I am learning about Remoting, just to understand it, and I found that Remoting is indeed a good thing.
We usually have three ways to use remoting, one is
the first one: Publishing a public object
Public objects are created locally. Second type: Remote creation of a public object (SAO)
The third type of object creation in client requests: Remote creation of a private object (CAO)
The object is created on HOST, and the client refers to the object directory on the server.
I don't have a good understanding of the essential differences between these three types. The remoting creation methods of these three methods are also different.
first way
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");
Second way
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");
The third way
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 processing, then this object needs to inherit from the MarshalByRefObject class.
How to transfer files during remoting? The basic idea is to open the client's file in the client, convert the Byte[] type and then call the host object.
Objects transferred between Client and Host
[Serializable]
public struct kAction
{
public string filename;
public byte[] context;
};Open the file and save the stream bytes 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 the Host reads the Kaction, it 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();
It was found that new objects cannot be defined in the object. When preparing to send to HOST, it will prompt "contains potentially dangerous types".
[Serializable]
public struct kAction
{
public string filename;
public byte[] context;
public FineInfo fileInfo;//here
};
Record your thoughts. When I have time, I will sort it out and make a complete article next time.
cnzc's blogs