小弟想用remoting 方法编写一个程序。可是每次在server 运行完后都要给server 重新指定端口
不然的话启动程序就会出现socket 异常。如果重新分配一个端口就好了
我仔细检查了服务器的程序,channel 确实已经释放过了,可是就是不行。请大侠们指教,小弟在此写过。Hello:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Test
{
public class Hello:System.MarshalByRefObject
{ private TcpServerChannel _channel;
public Hello()
{
Console.WriteLine("construct called");
} ~Hello()
{
Console.WriteLine("Destructor called");
} public string Greeting(string name)
{
Console.WriteLine("Greeting called");
return "hello" + name;
} public void start()
{
this._channel = new TcpServerChannel(20006);
ChannelServices.RegisterChannel(this._channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "Hi", WellKnownObjectMode.SingleCall);
} public void stop()
{
try
{ this._channel.StopListening(null);
ChannelServices.UnregisterChannel(this._channel); }
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine("Can't Unregister channel! " + ex.ToString());
}
}
}
}Server:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;namespace Test
{
class Program
{
static void Main(string[] args)
{
Hello m_hello = new Hello(); m_hello.start(); System.Console.WriteLine("press return to exit");
System.Console.ReadLine(); m_hello.stop();
Console.WriteLine("server stop");
}
}
}Client
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using Test;namespace Test
{
class Program
{
static void Main(string[] args)
{
ChannelServices.RegisterChannel(new TcpClientChannel()); Hello obj = (Hello)Activator.GetObject(typeofHello), "tcp://localhost:20006/Hi"); for (int i = 0; i < 10; i++)
Console.WriteLine(obj.Greeting("howard")); Console.ReadLine(); }
}
}
不然的话启动程序就会出现socket 异常。如果重新分配一个端口就好了
我仔细检查了服务器的程序,channel 确实已经释放过了,可是就是不行。请大侠们指教,小弟在此写过。Hello:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Test
{
public class Hello:System.MarshalByRefObject
{ private TcpServerChannel _channel;
public Hello()
{
Console.WriteLine("construct called");
} ~Hello()
{
Console.WriteLine("Destructor called");
} public string Greeting(string name)
{
Console.WriteLine("Greeting called");
return "hello" + name;
} public void start()
{
this._channel = new TcpServerChannel(20006);
ChannelServices.RegisterChannel(this._channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "Hi", WellKnownObjectMode.SingleCall);
} public void stop()
{
try
{ this._channel.StopListening(null);
ChannelServices.UnregisterChannel(this._channel); }
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine("Can't Unregister channel! " + ex.ToString());
}
}
}
}Server:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;namespace Test
{
class Program
{
static void Main(string[] args)
{
Hello m_hello = new Hello(); m_hello.start(); System.Console.WriteLine("press return to exit");
System.Console.ReadLine(); m_hello.stop();
Console.WriteLine("server stop");
}
}
}Client
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using Test;namespace Test
{
class Program
{
static void Main(string[] args)
{
ChannelServices.RegisterChannel(new TcpClientChannel()); Hello obj = (Hello)Activator.GetObject(typeofHello), "tcp://localhost:20006/Hi"); for (int i = 0; i < 10; i++)
Console.WriteLine(obj.Greeting("howard")); Console.ReadLine(); }
}
}
解决方案 »
- C#修改NTFS目录文件的权限的问题
- 用Java调用C#的WebService接口,各位大大帮帮忙
- HttpWebRequest 表单提交总是超时,什么原因呀
- 请问2005数据库怎么导到2000,我试着直接导入,但是出错拉,
- C#读取excel,行数问题
- 如何做大工程的界面单元测试?
- 请问怎么在C#的应用程序里面调用一个url地址
- 求教:哪里有ARJ的压缩和解压缩的C#算法啊。
- 写的一个windows服务,发布的时候老是应用程序验证未成功。日志提示标识不匹配。
- 高手快来,关于自定义的用户控件的问题!!
- textbox问题
- 如何让框架里面的aspx文件接收session值,Redirect指的是主框架
开启了客户端,重新启动服务器, 一切正常。
你是不是启动了两个server?