首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >原 WCF学习之旅----基础篇之NET

原 WCF学习之旅----基础篇之NET

作者头像
魂祭心
发布2018-05-17 15:58:21
5760
发布2018-05-17 15:58:21
举报
文章被收录于专栏:魂祭心魂祭心

公用类

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

namespace ServerObject
{
    public interface SayServer
    {
        string SayToServer(string word);
    }

    public class ServerObject:MarshalByRefObject,SayServer
    {
        public static Action<string> SayToServerEvent;
        public string SayToServer(string word)
        {
            if (SayToServerEvent!= null)
            {
                SayToServerEvent(word);
            }
            return "服务器已经收到消息:"+word ;
        }


    }
}

服务端:

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.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Channels.Http;

namespace RemoteingServerTest
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            ServerObject.ServerObject.SayToServerEvent += new Action<string>(MessageCount);
            while (true)
            {
                Console.WriteLine("***************************************");
                Console.WriteLine("****0: 服务端SingleCall激活************");
                Console.WriteLine("****1: 服务端SingleTon 激活(不可用)****");
                Console.WriteLine("****2: 服务端取消          ************");
                Console.WriteLine("****3: 退出程序            ************");
                Console.WriteLine("***************************************");
                string flag = Console.ReadLine();
                switch (flag)
                {
                    case "0":
                        startRemotingServerWithSingleCall();
                        break;
                    //case "1":
                    //    startRemotingServerWithSingleton();
                    //    break;
                    case "2":
                        cancelRemotingServer();
                        break;
                    case "3":
                        Environment.Exit(0);
                        break;
                    default:
                        Console.WriteLine("输入有误,请重新输入");
                        break;

                }
            }
        }
        public static int num = 0;
        static void MessageCount(string word)
        {
             num++;
             Console.Beep();
             Console.WriteLine ("客户端第{0}发来消息: {1}",num,word);
        }
        private static  void startRemotingServerWithSingleCall()
        {
            TcpServerChannel chanel = new TcpServerChannel(8088);
            bool ensureSecurity = false;
            ChannelServices.RegisterChannel(chanel, ensureSecurity);

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServerObject.ServerObject)
               , "Hi", WellKnownObjectMode.SingleCall);
            System.Console.WriteLine("SingleCall正在运行。。。。。。。。");
            Console.ReadLine();
        }
        //private static  void startRemotingServerWithSingleton()
        //{
        //    TcpServerChannel chanel = new TcpServerChannel(8088);
        //    bool ensureSecurity = false;
        //    ChannelServices.RegisterChannel(chanel, ensureSecurity);
        //    RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServerObject.ServerObject)
        //   , "Hi", WellKnownObjectMode.Singleton);
        //    System.Console.WriteLine("Singleton正在运行。。。。。。。。");
        //    Console.ReadLine();
        //}
        private static  void cancelRemotingServer()
        {
            IChannel[] channels = ChannelServices.RegisteredChannels;

            foreach(IChannel ecachChannel in channels)
            {
                TcpChannel tcpChannel = (TcpChannel)ecachChannel;
                tcpChannel.StopListening(null);
                ChannelServices.UnregisterChannel(tcpChannel);
            }
        }
    }
}

客户端:

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.Runtime.Remoting.Channels.Http;
using ServerObject;
namespace RemotingClientTest
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpRemotingClient();
        }
        static void TcpRemotingClient()
        {
            TcpChannel channel = new TcpChannel();
            ChannelServices.RegisterChannel(channel, false);
            ServerObject.ServerObject obj = (ServerObject.ServerObject)Activator.GetObject(typeof(ServerObject.ServerObject),
                "tcp://192.168.40.1:8088/Hi");  //"tcp://ip:port/url"      

            if (obj == null)
            {
                Console.WriteLine("cound ot locate server");
                return;
            }
            while (true)
            {
                Console.WriteLine("*******************************");
                Console.WriteLine("*******输入发送的消息**********");
                Console.WriteLine("*******************************");
                Console.WriteLine("输入一行:");
                string word = Console.ReadLine();
                string backWord = obj.SayToServer(word);
                 Console.WriteLine(backWord);
            }
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档