.Net 远程处理问题 (C#)

发布于 2024-07-26 06:29:51 字数 3309 浏览 7 评论 0原文

我正在使用 .Net Remoting 并尝试访问服务器中修改的远程对象,这是对象定义: 这个想法是,我在服务器上创建的 MRB 对象返回到客户端,并在客户端调用 getString() 时打印出“Set On Server”。

我现在得到的是客户端上的空字符串,因此当在客户端上调用 new 时,MRB 对象没有发送到客户端。 对所有不同的课程等感到抱歉,但这是唯一的方法,我尽可能地修剪。

我真正想要的是运行时在客户端上打印“在服务器上设置”

using System;
using System.Runtime.Remoting.Lifetime;
namespace RemoteType
{
    public class MyRemoteObject : System.MarshalByRefObject
    {
        private string sharedString;
        public string getString()
        {
            return sharedString;
        }
        public void setString(string value)
        {
        sharedString = value;
        }   
        public MyRemoteObject()
        {
            Console.WriteLine("MyRemoteObject Constructor Called");
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }
        public string Hello()
        {
            return "Hello, Welcome to .Net Remoting !";
        }
    }

这里知道服务器:

using System;
using System.Runtime.Remoting;
using RemoteType;
namespace SimpleServer
{
    class SimpleServer
    {
        public static MyRemoteObject MRB = null;
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure("RemotingAppServer.exe.config");
            MRB = new MyRemoteObject();
            MRB.setString("Set on the server");
            Console.WriteLine(MRB.getString());
            RemotingServices.Marshal((MRB), "MyRemoteObject");
            Console.WriteLine("Press return to exit");
            Console.ReadLine();
        }
    }

和 .NET 远程配置 App.Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application>
            <channels>
                <channel ref="tcp" port="8000" />
            </channels>
            <service>
                <wellknown mode="Singleton" 
                type="RemoteType.MyRemoteObject, RemoteType"
                objectUri="MyRemoteObject" />
            </service>
        </application>
    </system.runtime.remoting>
</configuration>

最后是客户端:

using System;
using System.Runtime.Remoting;
using RemoteType;
namespace SimpleClient
{
    class SimpleClient
    {
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure("RemoteClient.exe.config");
            MyRemoteObject robj = new MyRemoteObject();
            Console.WriteLine(robj.Hello() + " " + robj.getString());
            Console.ReadLine();
        }
    }
}

及其配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application name = "SimpleClient">
            <client>
                <wellknown
                type="RemoteType.MyRemoteObject,RemoteType"
                url="tcp://localhost:8000/MyRemoteObject"/>
            </client>
            <channels>
                <channel ref="tcp" port="0"/>
            </channels>
        </application>
    </system.runtime.remoting>
</configuration>

I am using .Net Remoting and trying to access a remote object modified in the Server, here is the object def:
The idea is that the MRB object, which I create on the server, is returned to the client and "Set On Server" is printed out when getString() is called on the client.

What I get right now is a null string on the client, so the MRB object was not send to the client when new was called on the client.
Sorry for all the different classes etc, but its the only way, I trimmed as much as possible.

What I really want is for "Set On The Server" printed on the client when run.

using System;
using System.Runtime.Remoting.Lifetime;
namespace RemoteType
{
    public class MyRemoteObject : System.MarshalByRefObject
    {
        private string sharedString;
        public string getString()
        {
            return sharedString;
        }
        public void setString(string value)
        {
        sharedString = value;
        }   
        public MyRemoteObject()
        {
            Console.WriteLine("MyRemoteObject Constructor Called");
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }
        public string Hello()
        {
            return "Hello, Welcome to .Net Remoting !";
        }
    }

Know here is the server:

using System;
using System.Runtime.Remoting;
using RemoteType;
namespace SimpleServer
{
    class SimpleServer
    {
        public static MyRemoteObject MRB = null;
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure("RemotingAppServer.exe.config");
            MRB = new MyRemoteObject();
            MRB.setString("Set on the server");
            Console.WriteLine(MRB.getString());
            RemotingServices.Marshal((MRB), "MyRemoteObject");
            Console.WriteLine("Press return to exit");
            Console.ReadLine();
        }
    }

And the .NET Remote Config App.Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application>
            <channels>
                <channel ref="tcp" port="8000" />
            </channels>
            <service>
                <wellknown mode="Singleton" 
                type="RemoteType.MyRemoteObject, RemoteType"
                objectUri="MyRemoteObject" />
            </service>
        </application>
    </system.runtime.remoting>
</configuration>

Finally the client:

using System;
using System.Runtime.Remoting;
using RemoteType;
namespace SimpleClient
{
    class SimpleClient
    {
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure("RemoteClient.exe.config");
            MyRemoteObject robj = new MyRemoteObject();
            Console.WriteLine(robj.Hello() + " " + robj.getString());
            Console.ReadLine();
        }
    }
}

And its config too:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application name = "SimpleClient">
            <client>
                <wellknown
                type="RemoteType.MyRemoteObject,RemoteType"
                url="tcp://localhost:8000/MyRemoteObject"/>
            </client>
            <channels>
                <channel ref="tcp" port="0"/>
            </channels>
        </application>
    </system.runtime.remoting>
</configuration>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剩余の解释 2024-08-02 06:29:51

我确实测试了你的代码并且工作得很好。 我确实设置了三个项目,一个与服务器有关,另一个与客户端有关,第三个项目在服务器和客户端之间共享远程对象。
在远程 app.config 中,您可以删除众所周知的条目,因为您已经通过代码对其进行了编组。

I did test your code and worked perfectly. I did set three projects, one with server, another with client and a third one shared between server and client for the remote object.
In the remote app.config you can remove the wellknown entry as you are already marshalling it by code.

青朷 2024-08-02 06:29:51

您确定应用程序结束后您的套接字会立即关闭吗?

如果您快速测试多次,这可能会导致 TCP 通信问题

Are you sure your sockets closes as soon as the application ends?

If you test multiple times rapidly this may cause tcp communication problems

白昼 2024-08-02 06:29:51

您的环境中的防火墙是否会阻止 tcp 端口 8000? 您可以尝试切换到 http 作为测试。

Could a firewall be blocking tcp port 8000 in your environment? You could try switching to http just as a test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文