Remoting问题紧急求助

pcqpzq 2010-08-18 10:49:54
.net remoting,单机调试通过,两台机器(都是WIN7,防火墙全部关闭,Ping测试也通过)调试就不行
客户端代码

System.Runtime.Remoting.Channels.Tcp.TcpChannel ch;
System.Runtime.Remoting.Lifetime.ClientSponsor cp;
RMObject.RMObject rmobj;
private void btnLink_Click(object sender, EventArgs e)
{
System.Runtime.Remoting.Channels.BinaryServerFormatterSinkProvider bsfs =
new System.Runtime.Remoting.Channels.BinaryServerFormatterSinkProvider() { TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full };
System.Runtime.Remoting.Channels.BinaryClientFormatterSinkProvider bcfs =
new System.Runtime.Remoting.Channels.BinaryClientFormatterSinkProvider();
System.Collections.IDictionary props = new System.Collections.Hashtable();
props["port"] = 0;
ch = new System.Runtime.Remoting.Channels.Tcp.TcpChannel(props, bcfs, bsfs);
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(ch,true);
//rmobj = new RMObject.RMObject();

string url = "tcp://192.168.1.101:9998";//tcp://127.0.0.1:9998 没问题
System.Runtime.Remoting.Activation.UrlAttribute urlattr = new System.Runtime.Remoting.Activation.UrlAttribute(url);
object[] args = null; //这里是构造函数传的参数
object[] attrs = { urlattr };
rmobj = (RMObject.RMObject)Activator.CreateInstance(typeof(RMObject.RMObject), args, attrs);

cp.Register(rmobj);
}

服务器代码:


System.Runtime.Remoting.Channels.Tcp.TcpChannel ch;
private void btnStart_Click(object sender, EventArgs e)
{
System.Runtime.Remoting.Channels.BinaryServerFormatterSinkProvider bsfs =
new System.Runtime.Remoting.Channels.BinaryServerFormatterSinkProvider()
{ TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full };
System.Runtime.Remoting.Channels.BinaryClientFormatterSinkProvider bcfs =
new System.Runtime.Remoting.Channels.BinaryClientFormatterSinkProvider();
System.Collections.IDictionary props = new System.Collections.Hashtable();
props["port"] = 9998;
ch = new System.Runtime.Remoting.Channels.Tcp.TcpChannel(props, bcfs, bsfs);
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(ch,true);
}

错误: 调用的目标发生了异常。
...全文
96 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
pcqpzq 2010-08-18
  • 打赏
  • 举报
回复
谢谢各位,解决了
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(ch,true);
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(ch,true);
把这两处true改false就行,谢谢各位
Peter200694013 2010-08-18
  • 打赏
  • 举报
回复
用下边这个简单例子测试下
------------------------------------
share.dll
如下:
using System;
namespace helloremoting
{
public class sayhello:System.MarshalByRefObject
{
public string hello(string greeting)
{
Console.WriteLine( "now is remoting transfer the value:{0} ",greeting);
return "hello ";
}
}
}

server.cs如下:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class simpletcpsrvr
{
public static void Main()
{
int recv;
byte[] data=new byte[1024];
IPEndPoint ipep=new IPEndPoint(IPAddress.Any,9050);

Socket newsock=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

newsock.Bind(ipep);
newsock.Listen(10);
Console.WriteLine( "waiting for a client ");
Socket client=newsock.Accept();
IPEndPoint clientep=(IPEndPoint)client.RemoteEndPoint;
Console.WriteLine( "Connected with {0} at port{1} ",clientep.Address,clientep.Port);

string welcome= "welcome to my test server ";
data=Encoding.ASCII.GetBytes(welcome);
client.Send(data,data.Length,SocketFlags.None);

while(true)
{
data=new byte[1024];
recv=client.Receive(data);
if(recv==0)
break;

Console.WriteLine(Encoding.ASCII.GetString(data,0,recv));
client.Send(data,recv,SocketFlags.None);

}

Console.WriteLine( "disconnected from {0} ",clientep.Address);
client.Close();
newsock.Close();


}
}


client.cs
如下:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using helloremoting;

namespace remotingsample
{
public class client
{
public static int Main(string[] args)
{
TcpChannel chan=new TcpChannel();
ChannelServices.RegisterChannel(chan);
sayhello obj=(sayhello)Activator.GetObject(typeof(sayhello), "tcp://localhost:8085/sayhello ");
if(obj==null)
Console.WriteLine( "cant not set the place to the remote server ");
else
Console.WriteLine(obj.hello( "hello,i am client,i have conect to the server "));
Console.ReadLine();

return 0;

}
}
}


测试的时候一定要先打开服务器server.exe,然后再运行client.exe
bdmh 2010-08-18
  • 打赏
  • 举报
回复
检查路由,是否做了限制
gxingmin 2010-08-18
  • 打赏
  • 举报
回复
System.Runtime.Remoting.Channels.Tcp.TcpChannel(props, bcfs, bsfs);
把这个改成HttpChannel试试
pcqpzq 2010-08-18
  • 打赏
  • 举报
回复
哪位大侠帮忙,分全送上,不够再加,要不请吃饭

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧