zoukankan      html  css  js  c++  java
  • WCF TCP

    
    // Server.cs
    namespace WCF.Services
    {
        using System;
        using System.ServiceModel;
        using Contracts.Operations;
        // Service class which implements the service contract.
        // Added code to write output to the console window
        public class CalculatorService : ICalculator
        {
            public double Add(double n1, double n2)
            {
                double result = n1 + n2;
                Console.WriteLine("Received Add({0},{1})", n1, n2);
                Console.WriteLine("Return: {0}", result);
                return result;
            }
            public double Subtract(double n1, double n2)
            {
                double result = n1 - n2;
                Console.WriteLine("Received Subtract({0},{1})", n1, n2);
                Console.WriteLine("Return: {0}", result);
                return result;
            }
            public double Multiply(double n1, double n2)
            {
                double result = n1 * n2;
                Console.WriteLine("Received Multiply({0},{1})", n1, n2);
                Console.WriteLine("Return: {0}", result);
                return result;
            }
            public double Divide(double n1, double n2)
            {
                double result = n1 / n2;
                Console.WriteLine("Received Divide({0},{1})", n1, n2);
                Console.WriteLine("Return: {0}", result);
                return result;
            }
            // Host the service within this EXE console application.
            public static void Main()
            {
                // Create a ServiceHost for the CalculatorService type.
                using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService)))
                {
                    // Open the ServiceHost to create listeners and start listening for messages.
                    string address = @"net.tcp://192.168.1.200:9000/servicemodelsamples/service";
                    NetTcpBinding binding = new NetTcpBinding();
                    //binding.Security.Transport.MsmqAuthenticationMode = MsmqAuthenticationMode.None;
                    //binding.Security.Mode = MsmqIntegrationSecurityMode.None;
                    serviceHost.AddServiceEndpoint(typeof(ICalculator), binding, address);
                    serviceHost.Open();
                    // The service can now be accessed.
                    Console.WriteLine("The service is ready.");
                    Console.WriteLine("Press <ENTER> to terminate service.");
                    Console.WriteLine();
                    Console.ReadLine();
                }
            }
        }
    }
    //========================================================================================================
    // Client.cs
    namespace WCF.Clients.Proxys
    {
        // "D:\Microsoft.SDKs\Windows\v7.1\Bin\NETFX 4.0 Tools\SvcUtil.exe" Share.dll
        // "D:\Microsoft.SDKs\Windows\v7.1\Bin\NETFX 4.0 Tools\SvcUtil.exe" *.wsdl *.xsd
            //------------------------------------------------------------------------------
        // <auto-generated>
        //     此代码由工具生成。
        //     运行时版本:4.0.30319.1
        //
        //     对此文件的更改可能会导致不正确的行为,并且如果
        //     重新生成代码,这些更改将会丢失。
        // </auto-generated>
        //------------------------------------------------------------------------------
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
        [System.ServiceModel.ServiceContractAttribute(ConfigurationName="ICalculator")]
        public interface ICalculator
        {
            [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculator/Add", ReplyAction="http://tempuri.org/ICalculator/AddResponse")]
            double Add(double n1, double n2);
            [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculator/Subtract", ReplyAction="http://tempuri.org/ICalculator/SubtractResponse")]
            double Subtract(double n1, double n2);
            [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculator/Multiply", ReplyAction="http://tempuri.org/ICalculator/MultiplyResponse")]
            double Multiply(double n1, double n2);
            [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculator/Divide", ReplyAction="http://tempuri.org/ICalculator/DivideResponse")]
            double Divide(double n1, double n2);
        }
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
        public interface ICalculatorChannel : ICalculator, System.ServiceModel.IClientChannel
        {
        }
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
        public partial class CalculatorClient : System.ServiceModel.ClientBase<ICalculator>, ICalculator
        {
            public CalculatorClient()
            {
            }
            public CalculatorClient(string endpointConfigurationName) : 
                    base(endpointConfigurationName)
            {
            }
            public CalculatorClient(string endpointConfigurationName, string remoteAddress) : 
                    base(endpointConfigurationName, remoteAddress)
            {
            }
            public CalculatorClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
                    base(endpointConfigurationName, remoteAddress)
            {
            }
            public CalculatorClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
                    base(binding, remoteAddress)
            {
            }
            public double Add(double n1, double n2)
            {
                return base.Channel.Add(n1, n2);
            }
            public double Subtract(double n1, double n2)
            {
                return base.Channel.Subtract(n1, n2);
            }
            public double Multiply(double n1, double n2)
            {
                return base.Channel.Multiply(n1, n2);
            }
            public double Divide(double n1, double n2)
            {
                return base.Channel.Divide(n1, n2);
            }
        }
    }
    namespace WCF.Clients.Host
    {
        using System;
        using System.ServiceModel;
        using WCF.Clients.Proxys;
        class Client
        {
            static void Main()
            {
                // Create a client
                string address = @"net.tcp://192.168.1.200:9000/servicemodelsamples/service";
                NetTcpBinding binding = new NetTcpBinding();
                CalculatorClient client = new CalculatorClient(binding, new EndpointAddress(address));
                string input = string.Empty;
                while ("q" != (input = Console.ReadLine().ToLower()))
                {
                    // Call the Add service operation.
                    double value1 = 100.00D;
                    double value2 = 15.99D;
                    double result = client.Add(value1, value2);
                    Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
                    // Call the Subtract service operation.
                    value1 = 145.00D;
                    value2 = 76.54D;
                    result = client.Subtract(value1, value2);
                    Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
                    // Call the Multiply service operation.
                    value1 = 9.00D;
                    value2 = 81.25D;
                    result = client.Multiply(value1, value2);
                    Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
                    // Call the Divide service operation.
                    value1 = 22.00D;
                    value2 = 7.00D;
                    result = client.Divide(value1, value2);
                    Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
                    //Closing the client gracefully closes the connection and cleans up resources
                }
                client.Close();
                Console.WriteLine();
                Console.WriteLine("Press <ENTER> to terminate client.");
                Console.ReadLine();
            }
        }
    }
    //=============================================================================================================
    // Share.cs
    namespace Contracts.Operations
    {
        using System.ServiceModel;
        // Define a service contract.
        [ServiceContract]
        public interface ICalculator
        {
            [OperationContract]
            double Add(double n1, double n2);
            [OperationContract]
            double Subtract(double n1, double n2);
            [OperationContract]
            double Multiply(double n1, double n2);
            [OperationContract]
            double Divide(double n1, double n2);
        }
    }
    //====================================================================================
    
    
  • 相关阅读:
    MVP模式与MVVM模式
    webpack的配置处理
    leetcode 287 Find the Duplicate Number
    leetcode 152 Maximum Product Subarray
    leetcode 76 Minimum Window Substring
    感知器算法初探
    leetcode 179 Largest Number
    leetcode 33 Search in Rotated Sorted Array
    leetcode 334 Increasing Triplet Subsequence
    朴素贝叶斯分类器初探
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/1865467.html
Copyright © 2011-2022 走看看