17,740
社区成员
发帖
与我相关
我的任务
分享
public class RemotingObj:MarshalByRefObject
{
public void SendAndSerialize(Student stu)
{
Stream stream = File.Open("c:\\Student.ser", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, stu);
stream.Close();
}
}
[Serializable]
public class Student:ISerializable
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
public Student()
{ }
// For deserialize
public Student(SerializationInfo info, StreamingContext ctxt)
{
this.name = (string)info.GetValue("Name", typeof(string));
this.age = (int)info.GetValue("Age", typeof(int));
}
public override string ToString()
{
return "Age is:" + this.age.ToString() + ",Name is:" + this.name;
}
#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Name", this.name);
info.AddValue("Age", this.age);
}
#endregion
}
static void Main(string[] args)
{
try
{
BinaryClientFormatterSinkProvider clientProvider = null;
BinaryServerFormatterSinkProvider serverProvider =
new BinaryServerFormatterSinkProvider();
serverProvider.TypeFilterLevel =
System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
System.Collections.IDictionary props = new System.Collections.Hashtable();
props["port"] = 5008;
props["typeFilterLevel"] = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
props["CustomErrorsEnabled"] = false;
TcpChannel chan = new TcpChannel(props, clientProvider, serverProvider);
ChannelServices.RegisterChannel(chan, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ObjLib.RemotingObj),
"ObjLib.RemotingObj", WellKnownObjectMode.Singleton);
Console.WriteLine("Remoting server is started \r\n");
}
catch (RemotingException re)
{
Console.WriteLine(re.Message);
}
// Wait for Quit to terminate program
while (!(Console.In.ReadLine().Equals("Quit")))
{
;
}
}
static void Main(string[] args)
{
try
{
BinaryClientFormatterSinkProvider clientProvider =
new BinaryClientFormatterSinkProvider();
BinaryServerFormatterSinkProvider serverProvider =
new BinaryServerFormatterSinkProvider();
serverProvider.TypeFilterLevel =
System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
System.Collections.IDictionary props = new System.Collections.Hashtable();
props["port"] = 0;
props["name"] = System.Guid.NewGuid().ToString();
props["typeFilterLevel"] = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
props["CustomErrorsEnabled"] = false;
TcpChannel chan =
new TcpChannel(props, clientProvider, serverProvider);
ChannelServices.RegisterChannel(chan, false);
string strURL = String.Format("{0}{1}:{2}/{3}", "tcp://", "9.123.74.36", 5008, "ObjLib.RemotingObj");
ObjLib.RemotingObj rmtObj = (ObjLib.RemotingObj)Activator.GetObject(typeof(ObjLib.RemotingObj), strURL);
ObjLib.Student stu = new ObjLib.Student();
stu.Age = 30;
stu.Name = "Tommy";
rmtObj.SendAndSerialize(stu);
Console.WriteLine("Send object finish.\r\n");
}
catch (RemotingException re)
{
Console.WriteLine(re.Message);
}
// Wait for Quit to terminate program
while (!(Console.In.ReadLine().Equals("Quit")))
{
;
}
}