麻烦建立一个C sharp的分布式计算器系统

时间:2018-04-30 09:34:24

标签: c# distributed-computing

有人可以帮我吗?此代码旨在从客户端系统获取2个数字,并使用Clientserver代码添加这两个数字并将数字返回给客户端,但实际上,输入数字时,根本就没有输出

客户代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CalcService;

namespace CalcClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Type requiredType = typeof(ICalculator);
            ICalculator proxyRemoteObject = 
                (ICalculator)Activator.GetObject(requiredType, "tcp://10.10.10.10:500/Serverone");

            Console.WriteLine("Number 1: ");
            int number1 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Number 2: ");
            int number2 = Convert.ToInt32(Console.ReadLine());

            int answer = proxyRemoteObject.AddNumbers(number1, number2);
            Console.WriteLine("Answer: " + answer);

            Console.ReadLine();
        }
    }
}

服务代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CalcService
{
    public interface ICalculator
    {
        int AddNumbers(int number1, int number2);
    }
}

服务器代码

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

namespace CalcServer
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpChannel channel = new TcpChannel(500);
            ChannelServices.RegisterChannel(channel,false);

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyCalculator), "Serverone", WellKnownObjectMode.SingleCall);

            Console.WriteLine("Server is running");
            Console.ReadLine();
        }
    }

    public class MyCalculator: MarshalByRefObject, ICalculator
    {
        public int AddNumbers(int num1, int num2)
        {
            return num1 + num2;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

将服务器更改为127.0.0.1解决了问题!很抱歉浪费任何时间。谢谢用户Henk!