客户端报的错误信息是:
Message "System.ArgumentNullException: h0g( DispatchChannelSink d9\ve\t\rf2!f\te\re:e\be\fd;;d=f6\bf/c\0\r\nef0e\r: requestMsg\r\n at System.Runtime.Remoting.Channels.DispatchChannelSink.ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, IMessage& responseMsg, ITransportHeaders& responseHeaders, Stream& responseStream)\r\n at System.Runtime.Remoting.Channels.BinaryServerFormatterSink.ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, IMessage& responseMsg, ITransportHeaders& responseHeaders, Stream& responseStream)\r\n at System.Runtime.Remoting.Channels.Http.HttpServerTransportSink.ServiceRequest(Object state)\r\n at System.Runtime.Remoting.Channels.SocketHandler.ProcessRequestNow()\r\n" string
实例:
using System;
namespace MyLibrary
{
public class MsgBoard:MarshalByRefObject
{
public delegate void EventDelegate( string msg );
public event EventDelegate OnAdded;
public void AddMessage( string msg )
{
Console.WriteLine("Msg= " + msg);
if( OnAdded != null )
OnAdded( msg );
}
}
public class eventClass:MarshalByRefObject
{
public void msg_OnAdded( string msg )
{
Console.WriteLine( "Get Msg = " + msg );
}
}
}
Server 代码:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Serialization.Formatters;
using System.Collections;
namespace Server
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Program
{
static void Main(string[] args)
{
try
{
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
BinaryClientFormatterSinkProvider clientProv = new BinaryClientFormatterSinkProvider();
IDictionary props = new Hashtable();
props["port"] = 2000;
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(MyLibrary.MsgBoard),"MyUri",WellKnownObjectMode.Singleton);
HttpChannel chan = new HttpChannel(props, clientProv, provider);
ChannelServices.RegisterChannel(chan);
Console.WriteLine( "Server Started " );
Console.ReadLine();
}
catch( Exception ex )
{
Console.WriteLine( ex.Message );
Console.ReadLine();
}
}
}
}
Client 代码:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Serialization.Formatters;
namespace Client
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Program
{
static void Main(string [] args )
{
try
{
RemotingConfiguration.RegisterWellKnownClientType(
typeof(MyLibrary.MsgBoard), @"Http://localhost:2000/MyUri");
Console.WriteLine("Client Start");
MyLibrary.MsgBoard msg = new MyLibrary.MsgBoard();
MyLibrary.eventClass evclass = new MyLibrary.eventClass();
msg.OnAdded +=new MyLibrary.MsgBoard.EventDelegate(evclass.msg_OnAdded);
msg.AddMessage( "Hello All");
Console.ReadLine();
}
catch( Exception ex )
{
Console.WriteLine( ex.Message );
Console.ReadLine();
}
}
public static void msg_OnAdded(string msg)
{
Console.WriteLine( "Get Msg = " + msg );
}
}
}
3 个解决方案
#1
帮你up
#2
LG
#3
代码已试过,很奇怪,用HTTP无论如何都不行,但不是你的错误,好象是接收有问题,后来改为TCP就可以了,莫名其妙,可能是我的PC有问题(装了XP SP2后就觉得有点怪)。
另外由于CLIENT端要响应SERVER的事件,所以需要定义回调的入口和将CLIENT.EXE复制到SERVER的运行目录下。
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
namespace Client
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Program
{
static void Main(string [] args )
{
try
{
TcpChannel chnl=new TcpChannel(2001);
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(chnl);
RemotingConfiguration.RegisterWellKnownClientType(
typeof(MyLibrary.MsgBoard), @"Tcp://localhost:2000/MyUri");
Console.WriteLine("Client Start");
MyLibrary.MsgBoard msg = new MyLibrary.MsgBoard();
MyLibrary.eventClass evclass = new MyLibrary.eventClass();
msg.OnAdded +=new MyLibrary.MsgBoard.EventDelegate(evclass.msg_OnAdded);
msg.AddMessage( "Hello All");
Console.ReadLine();
}
catch( Exception ex )
{
Console.WriteLine( ex.Message );
Console.ReadLine();
}
}
public static void msg_OnAdded(string msg)
{
Console.WriteLine( "Get Msg = " + msg );
}
}
}
另外由于CLIENT端要响应SERVER的事件,所以需要定义回调的入口和将CLIENT.EXE复制到SERVER的运行目录下。
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
namespace Client
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Program
{
static void Main(string [] args )
{
try
{
TcpChannel chnl=new TcpChannel(2001);
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(chnl);
RemotingConfiguration.RegisterWellKnownClientType(
typeof(MyLibrary.MsgBoard), @"Tcp://localhost:2000/MyUri");
Console.WriteLine("Client Start");
MyLibrary.MsgBoard msg = new MyLibrary.MsgBoard();
MyLibrary.eventClass evclass = new MyLibrary.eventClass();
msg.OnAdded +=new MyLibrary.MsgBoard.EventDelegate(evclass.msg_OnAdded);
msg.AddMessage( "Hello All");
Console.ReadLine();
}
catch( Exception ex )
{
Console.WriteLine( ex.Message );
Console.ReadLine();
}
}
public static void msg_OnAdded(string msg)
{
Console.WriteLine( "Get Msg = " + msg );
}
}
}
#1
帮你up
#2
LG
#3
代码已试过,很奇怪,用HTTP无论如何都不行,但不是你的错误,好象是接收有问题,后来改为TCP就可以了,莫名其妙,可能是我的PC有问题(装了XP SP2后就觉得有点怪)。
另外由于CLIENT端要响应SERVER的事件,所以需要定义回调的入口和将CLIENT.EXE复制到SERVER的运行目录下。
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
namespace Client
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Program
{
static void Main(string [] args )
{
try
{
TcpChannel chnl=new TcpChannel(2001);
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(chnl);
RemotingConfiguration.RegisterWellKnownClientType(
typeof(MyLibrary.MsgBoard), @"Tcp://localhost:2000/MyUri");
Console.WriteLine("Client Start");
MyLibrary.MsgBoard msg = new MyLibrary.MsgBoard();
MyLibrary.eventClass evclass = new MyLibrary.eventClass();
msg.OnAdded +=new MyLibrary.MsgBoard.EventDelegate(evclass.msg_OnAdded);
msg.AddMessage( "Hello All");
Console.ReadLine();
}
catch( Exception ex )
{
Console.WriteLine( ex.Message );
Console.ReadLine();
}
}
public static void msg_OnAdded(string msg)
{
Console.WriteLine( "Get Msg = " + msg );
}
}
}
另外由于CLIENT端要响应SERVER的事件,所以需要定义回调的入口和将CLIENT.EXE复制到SERVER的运行目录下。
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
namespace Client
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Program
{
static void Main(string [] args )
{
try
{
TcpChannel chnl=new TcpChannel(2001);
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(chnl);
RemotingConfiguration.RegisterWellKnownClientType(
typeof(MyLibrary.MsgBoard), @"Tcp://localhost:2000/MyUri");
Console.WriteLine("Client Start");
MyLibrary.MsgBoard msg = new MyLibrary.MsgBoard();
MyLibrary.eventClass evclass = new MyLibrary.eventClass();
msg.OnAdded +=new MyLibrary.MsgBoard.EventDelegate(evclass.msg_OnAdded);
msg.AddMessage( "Hello All");
Console.ReadLine();
}
catch( Exception ex )
{
Console.WriteLine( ex.Message );
Console.ReadLine();
}
}
public static void msg_OnAdded(string msg)
{
Console.WriteLine( "Get Msg = " + msg );
}
}
}