C# Visual Studio TCP channel 已注册

标签 c# .net-remoting

我正在使用 TCP channel 连接创建具有 2 个不同控制台应用程序的聊天应用程序,但是当我运行客户端控制台或服务器控制台时,我收到“TCP 已注册”

我有 4 个代码项目。

  1. 界面
  2. 远程对象
  3. 客户端
  4. 服务器

接口(interface)代码如下:

using System;

public interface IRemoteObject
{
    void GetData(string myString);        

}

这是 RemoteObject 代码:

using System;

public class RemoteObject : MarshalByRefObject,IRemoteObject
{
    //Server Method
    public RemoteObject()
    {
        Console.WriteLine("Server text");                 
    }

    //Client Method
    public void GetData(string myString)
    {
        Console.WriteLine(myString);
    }
}

这是客户端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Threading;

namespace ClientConsole
{
    public class Client
    {
        public static void Main()
        {

            ThreadStart client1 = new ThreadStart(ClientSide);
            Thread client2 = new Thread(client1);
            ThreadStart server1 = new ThreadStart(ServerSide);
            Thread server2 = new Thread(server1);
            client2.Start();
            server2.Start();           

        }
        public static void ClientSide()
        {
            Console.WriteLine("ClientSide....");
            TcpChannel ch2 = new TcpChannel();
            ChannelServices.RegisterChannel(ch2, true);

            IRemoteObject objRemoteRef = (IRemoteObject)Activator.GetObject(typeof(IRemoteObject), "tcp://127.0.0.1:2233/M");

            while (true)
            {
                string x = Console.ReadLine();
                objRemoteRef.GetData(x);

            }

        }

        public static void ServerSide()
        {
            Console.WriteLine("ServerSide....");
            TcpChannel ch2 = new TcpChannel(2233);
            ChannelServices.RegisterChannel(ch2, true);

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "M", WellKnownObjectMode.Singleton);

        }

    }

}

这是服务器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Threading;


namespace ServerConsole
{
    public class Server
    {
        public static void Main()
        {
            ThreadStart server1 = new ThreadStart(ServerSide);
            Thread server2 = new Thread(server1);
            ThreadStart client1 = new ThreadStart(ClientSide);
            Thread client2 = new Thread(client1);
            server2.Start();
            client2.Start();


        }

        public static void ServerSide()
        {
            Console.WriteLine("ServerSide....");
            TcpChannel ch2 = new TcpChannel();
            ChannelServices.RegisterChannel(ch2, true);

            IRemoteObject objRemoteRef = (IRemoteObject)Activator.GetObject(typeof(IRemoteObject), "tcp://127.0.0.1:2333/M");

            while (true)
            {
                string x = Console.ReadLine();
                objRemoteRef.GetData(x);

            }
        }

        public static void ClientSide()
        {
            Console.WriteLine("ClientSide....");
            TcpChannel ch1 = new TcpChannel(2233);
            ChannelServices.RegisterChannel(ch1, true);

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "M", WellKnownObjectMode.Singleton);

            Console.ReadLine();



        }
    }
}

请帮忙,我是新来的,我不确定我写的帖子是否正确,请理解。

谢谢

最佳答案

试试这个:

客户端

public class Client
{
    public static void Main(string[] args)
    {

        ThreadStart client1 = new ThreadStart(ClientSide);
        Thread client2 = new Thread(client1);
        ThreadStart server1 = new ThreadStart(ServerSide);
        Thread server2 = new Thread(server1);
        client2.Start();
        server2.Start();
    }
    public static void ClientSide()
    {
        Console.WriteLine("ClientSide....");
        TcpChannel ch2 = (TcpChannel)Helper.GetChannel(2333, true);
        ChannelServices.RegisterChannel(ch2, false);
        IRemoteObject objRemoteRef = (IRemoteObject)Activator.GetObject(
            typeof(IRemoteObject), "tcp://127.0.0.1:2233/M");
        while (true)
        {
            string x = Console.ReadLine();
            objRemoteRef.GetData(x);
        }
    }
    public static void ServerSide()
    {
        Console.WriteLine("ServerSide....");
        TcpChannel ch2 = (TcpChannel)Helper.GetChannel(0, true);
        ChannelServices.RegisterChannel(ch2, false);
        RemotingConfiguration.RegisterWellKnownServiceType(
            typeof(RemoteObject), "M", WellKnownObjectMode.Singleton);
    }
}

服务器:

public class Server
{
    public static void Main()
    {
        ThreadStart server1 = new ThreadStart(ServerSide);
        Thread server2 = new Thread(server1);
        ThreadStart client1 = new ThreadStart(ClientSide);
        Thread client2 = new Thread(client1);
        server2.Start();
        client2.Start();
    }
    public static void ServerSide()
    {
        Console.WriteLine("ServerSide....");
        TcpChannel ch2 = (TcpChannel)Helper.GetChannel(2233, true);
        ChannelServices.RegisterChannel(ch2, false);
        IRemoteObject objRemoteRef = (IRemoteObject)Activator.GetObject(
            typeof(IRemoteObject), "tcp://127.0.0.1:2333/M");
        while (true)
        {
            string x = Console.ReadLine();
            objRemoteRef.GetData(x);
        }
    }
    public static void ClientSide()
    {
        Console.WriteLine("ClientSide....");
        TcpChannel ch1 = (TcpChannel)Helper.GetChannel(0, true);
        ChannelServices.RegisterChannel(ch1, false);
        RemotingConfiguration.RegisterWellKnownServiceType(
            typeof(RemoteObject), "M", WellKnownObjectMode.Singleton);
        Console.ReadLine();
    }
}

Helper

public class Helper
{
    public static IChannel GetChannel(int tcpPort, bool isSecure)
    {
        BinaryServerFormatterSinkProvider serverProv =
            new BinaryServerFormatterSinkProvider();
        serverProv.TypeFilterLevel = TypeFilterLevel.Full;
        IDictionary propBag = new Hashtable();
        propBag["port"] = tcpPort;
        propBag["typeFilterLevel"] = TypeFilterLevel.Full;
        propBag["name"] = Guid.NewGuid().ToString();
        if (isSecure)
        {
            propBag["secure"] = isSecure;
            propBag["impersonate"] = false; 
        }
        return new TcpChannel(
            propBag, null, serverProv);
    }
}

关于C# Visual Studio TCP channel 已注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15089026/

相关文章:

wcf - .NET 远程处理和 WCF

c# - 当一个对象从 MarshalByRefObject 派生并且也被标记为 [Serializable] 时会发生什么?

c# - 如何优化大尺寸for循环

python - 从虚拟线程中的调用执行主线程中的 Python 函数

c# - 统一: Record video from device camera

c# - 我如何使用 Moq 或 NInject 模拟内核模拟接口(interface)

powershell - Get-Service 失败,但 Invoke-Command {Get-Service} 成功

c# - PublicKeyToken=null 是否忽略程序集绑定(bind)?

c# - 在 WPF 中使用 EdgeMode.Aliased 选项时会出现额外的线条