CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2003
    Location
    Romania
    Posts
    41

    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");

    }

  2. #2
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: call another class?

    You need to place the interface in a seperate dll, and reference it from both the server and the client.

  3. #3
    Join Date
    Sep 2003
    Location
    Romania
    Posts
    41

    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

  4. #4
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    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
  •  





Click Here to Expand Forum to Full Width

Featured