实现了一个简单的remoting机制,问题出现在客户端调用远程对象的函数时,远程对象调用了自己的函数而没有调用我希望他调用的服务器端的函数,请问这是什么问题?要如何改进呢?客户端:using System;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text.RegularExpressions;
using RemotingService;
namespace RemotingClient
{
class DataGetter
{
static public QueryServiceLib remotingObj = new QueryServiceLib(); // public DataGetter()
static void Main(string[] args)
{
try
{
remotingObj = (QueryServiceLib)Activator.GetObject(typeof(QueryServiceLib)
, "tcp://127.0.0.1:7777/RemotingQuery");
if (remotingObj == null)
Console.WriteLine("cannot locate server!");
else
{
Console.WriteLine("Input: ");
string rawInput;
string mdInput;
rawInput = Console.ReadLine(); string result = remotingObj.SearchQuery(rawInput);
Console.WriteLine(result);
Console.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.ReadLine();
}
}
}
}
服务器端:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.IO;
using SimpleKeyValueLookup;
using System.Text.RegularExpressions;
using System.Collections;
using RemotingService;namespace RemotingServer
{
class Program
{ static SimpleKeyValueLookup.SimpleKeyValueLookup indexer = null; static void Main(string[] args)
{
try
{
TcpServerChannel tcpChannel = new TcpServerChannel(7777);
ChannelServices.RegisterChannel(tcpChannel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(QueryServiceLib)
, "RemotingQuery", WellKnownObjectMode.Singleton); string indexdir = @"D:\v-zijian\adChemy\sd";
indexer = new SimpleKeyValueLookup.SimpleKeyValueLookup(indexdir); //Load index file into memory
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
string mdInput;
Console.WriteLine("Please input 'exit' to end the program.");
while (!(mdInput = Console.ReadLine()).Equals("exit", StringComparison.OrdinalIgnoreCase))
{
} }
public string SearchQuery(string query)
{
string result = indexer.Lookup(query);
return result;
}
}
}
远程对象:using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;namespace RemotingService
{
public class QueryServiceLib : MarshalByRefObject
{
public QueryServiceLib()
{
Console.WriteLine("QueryService started");
} public string SearchQuery(string query)
{
string result = "11";
return result;
}
}
}
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text.RegularExpressions;
using RemotingService;
namespace RemotingClient
{
class DataGetter
{
static public QueryServiceLib remotingObj = new QueryServiceLib(); // public DataGetter()
static void Main(string[] args)
{
try
{
remotingObj = (QueryServiceLib)Activator.GetObject(typeof(QueryServiceLib)
, "tcp://127.0.0.1:7777/RemotingQuery");
if (remotingObj == null)
Console.WriteLine("cannot locate server!");
else
{
Console.WriteLine("Input: ");
string rawInput;
string mdInput;
rawInput = Console.ReadLine(); string result = remotingObj.SearchQuery(rawInput);
Console.WriteLine(result);
Console.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.ReadLine();
}
}
}
}
服务器端:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.IO;
using SimpleKeyValueLookup;
using System.Text.RegularExpressions;
using System.Collections;
using RemotingService;namespace RemotingServer
{
class Program
{ static SimpleKeyValueLookup.SimpleKeyValueLookup indexer = null; static void Main(string[] args)
{
try
{
TcpServerChannel tcpChannel = new TcpServerChannel(7777);
ChannelServices.RegisterChannel(tcpChannel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(QueryServiceLib)
, "RemotingQuery", WellKnownObjectMode.Singleton); string indexdir = @"D:\v-zijian\adChemy\sd";
indexer = new SimpleKeyValueLookup.SimpleKeyValueLookup(indexdir); //Load index file into memory
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
string mdInput;
Console.WriteLine("Please input 'exit' to end the program.");
while (!(mdInput = Console.ReadLine()).Equals("exit", StringComparison.OrdinalIgnoreCase))
{
} }
public string SearchQuery(string query)
{
string result = indexer.Lookup(query);
return result;
}
}
}
远程对象:using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;namespace RemotingService
{
public class QueryServiceLib : MarshalByRefObject
{
public QueryServiceLib()
{
Console.WriteLine("QueryService started");
} public string SearchQuery(string query)
{
string result = "11";
return result;
}
}
}
解决方案 »
- System.NullReferenceException: 未将对象引用设置到对象的实例
- 做一个下拉框显示年龄,除了用CONBOBOX NUMERICUPDOMN LISTBOX还有别的吗
- 如何计算graphics.DrawString写到Control上的文字的Rectangle
- datagrid中如何获得crrentcell的值?
- RSS的问题,不能自动生成,大侠救命(急 100分)
- 一个不错的电子书网站
- 各位谈谈对属性和索引器的理解,来者有分.
- 关于指示灯
- ListView重绘问题~~!!
- 有关在VS.NET2005里设计窗体设计器的问题
- 图像读取时报错——参数无效
- 大家帮忙看看这个SQL插入函数有问题不?(ACESS数据库2010)
Client<--dll服务器和客户端使用同一个dll,连接后,你调用的是服务器上对象的函数,如果想调用服务器程序中的方法。你可以这样
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;namespace RemotingService
{
public class QueryServiceLib : MarshalByRefObject
{
public static Func<string,string> fun=null;
public QueryServiceLib()
{
Console.WriteLine("QueryService started");
} public string SearchQuery(string query)
{
if(fun!=null) return fun(query);
else return string.Empty;
}
}
}
服务器代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.IO;
using SimpleKeyValueLookup;
using System.Text.RegularExpressions;
using System.Collections;
using RemotingService;namespace RemotingServer
{
class Program
{ static SimpleKeyValueLookup.SimpleKeyValueLookup indexer = null; static void Main(string[] args)
{
try
{
TcpServerChannel tcpChannel = new TcpServerChannel(7777);
ChannelServices.RegisterChannel(tcpChannel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(QueryServiceLib)
, "RemotingQuery", WellKnownObjectMode.Singleton);
QueryServiceLib.fun = SearchQuery; string indexdir = @"D:\v-zijian\adChemy\sd";
indexer = new SimpleKeyValueLookup.SimpleKeyValueLookup(indexdir); //Load index file into memory
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
string mdInput;
Console.WriteLine("Please input 'exit' to end the program.");
while (!(mdInput = Console.ReadLine()).Equals("exit", StringComparison.OrdinalIgnoreCase))
{
} }
public string SearchQuery(string query)
{
string result = indexer.Lookup(query);
return result;
}
}
}
其他一样。