|
-
March 6th, 2006, 02:27 AM
#1
call another class?
Hello,
This code makes a TCP server in RemoteServer. When it gets data on port 333 it instantiate HelloRemoteServer. The question is:
how can I know the instance of RemoteServer in class HelloRemoteServer and call MyFunction()?
namespace RemoteTestServer
{
public interface iInterface
{
string HelloMethod(string name);
}
class RemoteServerTest
{
[STAThread]
static void Main(string[] args)
{
RemoteServer rs = new RemoteServer();
}
}
class RemoteServer
{
public RemoteServer
{
TcpChannel chan = new TcpChannel(333);
ChannelServices.RegisterChannel(chan);
RemotingConfiguration.RegisterWellKnownServiceType(
Type.GetType("RemoteTestServer.HelloRemoteServer"),
"Hello", WellKnownObjectMode.SingleCall);
System.Console.WriteLine("Hit to exit...");
System.Console.ReadLine();
}
public void MyFunction()
{
//how ca I call this from HelloRemoteServer
}
}
class HelloRemoteServer : MarshalByRefObject, iInterface
{
public HelloRemoteServer()
{
Console.WriteLine("HelloServer activated");
}
public string HelloMethod(string name)
{
Console.WriteLine("Hello.HelloMethod : {0}", name);
return "Hi there " + name;
}
}
}
Thanks in advanced!
Ps: the remote of tcp:
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
iInterface obj = (iInterface)Activator.GetObject(
typeof(RemoteTestServer.iInterface),
"tcp://localhost:333/Hello");
if (obj != null)
{
obj.SayHellow("it's me");
}
-
March 6th, 2006, 04:31 PM
#2
Re: call another class?
You need to place the interface in a seperate dll, and reference it from both the server and the client.
-
March 7th, 2006, 08:23 AM
#3
Re: call another class?
Hi,
Yes, indeed, iInterface is defined in a DLL and is referenced by those two classes. But the question is how ca I call RemoteServer - MyFunction() from HelloRemoteServer?
Thanks
-
March 7th, 2006, 04:42 PM
#4
Re: call another class?
I don't really understand what you are trying to do. If you have the RemoteServer class as Singleton then you can call it from any other class:
Code:
class RemoteServerTest
{
[STAThread]
static void Main(string[] args)
{
RemoteServer.Instance.InitRemoting();
Console.ReadLine();
}
}
class RemoteServer
{
private RemoteServer()
{
}
private static RemoteServer _instance = null;
public static RemoteServer Instance
{
get
{
if (_instance == null)
_instance = new RemoteServer();
return _instance;
}
}
public void InitRemoting()
{
TcpChannel chan = new TcpChannel(333);
ChannelServices.RegisterChannel(chan);
RemotingConfiguration.RegisterWellKnownServiceType(
Type.GetType("RemoteTestServer.HelloRemoteServer"),
"Hello", WellKnownObjectMode.SingleCall);
}
public void MyFunction()
{
//Do something
}
}
Then you can call RemoteServer.Instance.MyFunction from your HelloRemoteServer class.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|