这个例子我也做了。我这里没有问题的。
我把程序端写成是winform了。
不是原来的控制台程序。
我把程序端写成是winform了。
不是原来的控制台程序。
解决方案 »
- 报表参数传递
- 关于DrawEllipse画圆的问题,高结贴率,解决马上结贴
- 帮忙写个sql语句
- 用AnimateWindow做Form淡出问题
- 有点难,想要在两个控件(ListBox)之间画线!
- ASP.NET 列前缀 'Products' 与查询中所用的表名或别名不匹配。
- TabControl与datagridview的问题?(在线)
- 问一个很菜的问题,TreeView中的TreeNode的属性ID与NodeData有什么不同?
- = 实在搞不定,SQL 又不熟,求高人给一个 SQL 语句 =
- 怎样可以实现DATAGRID的列长不可变动.
- 请问,C#如何调用windows api?
- 请问:关于数据库中添加记录的问题!!
server:
/// <summary>
/// 远程调用对象
/// </summary>
public class RemotingClass:MarshalByRefObject
{
public RemotingClass()
{
Console.WriteLine("HelloServer activated");
} public string HelloMethod(string name)
{
Console.WriteLine("Hello.HelloMethod : {0}", name);
return "Hi there " + name;
}
} //改方法是由窗体load时调用
private void GetRemotObj()
{
try
{
TcpServerChannel channel=new TcpServerChannel(8085); ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingClass),"TestHello",WellKnownObjectMode.SingleCall);
Console.WriteLine("hit to exit");
}
catch(Exception w)
{ Console.WriteLine(w.Message);
}
}
client:首先引用了封装RemotingClass的DLL文件,然后在按钮事件中:
private void button1_Click(object sender, System.EventArgs e)
{
try
{
TcpClientChannel chan = new TcpClientChannel();
ChannelServices.RegisterChannel(chan);
RemotingClass obj = (RemotingClass)Activator.GetObject(
typeof(RemotingClass),
"tcp://localhost:8085/TestHello");
string str="";
if (obj == null)
System.Console.WriteLine("Could not locate server");
else
//这是出错的地方,报的错误消息见上面
str=obj.HelloMethod("ni hao");
//Console.WriteLine(c); textBox1.Text=str;
return;
}
catch(Exception w)
{ Console.WriteLine(w.Message);
} }