Click to See Complete Forum and Search --> : call another class?


albusorin
March 6th, 2006, 01:27 AM
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");

}

jhammer
March 6th, 2006, 03:31 PM
You need to place the interface in a seperate dll, and reference it from both the server and the client.

albusorin
March 7th, 2006, 07:23 AM
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

jhammer
March 7th, 2006, 03:42 PM
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:

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.