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

公用类

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);
            }
        }
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏

使用handler"不落地"返回图片信息

11810
来自专栏C# 编程

C#泛型类的简单创建与使用

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

21700
来自专栏跟着阿笨一起玩NET

ASP.NET常用导出Excel方法汇总

本文转载:http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html

79520
来自专栏草根专栏

.NET Core 网络数据采集 -- 使用AngleSharp做html解析

48600
来自专栏跟着阿笨一起玩NET

winform 多线程中ShowDialog()步骤无效的解决办法

24910
来自专栏.Net移动开发

.Net语言 APP开发平台——Smobiler学习日志:用Gridview控件设计较复杂的表单

新创建MobileForm项,并命名为MessageShow3,并拖入一个Label控件,如图1

19640
来自专栏Kotlin入门系列

C#控制台基础 将byte类型以十六进制字符串的形式输出

46250
来自专栏C#

将文本文件的内容存储在DataSet中的方法总结

    项目中比较多的会对文件进行操作,例如文件的上传下载,文件的压缩和解压等IO操作。在.NET项目中较多的会使用DataSet,DataTable进行...

20780
来自专栏.net core新时代

List,DataTable实现行转列的通用方案

  最近在做报表统计方面的需求,涉及到行转列报表。根据以往经验使用SQL可以比较容易完成,这次决定挑战一下直接通过代码方式完成行转列。期间遇到几个问题和用到的新...

24870
来自专栏跟着阿笨一起玩NET

C#巧用Excel模版变成把Table打印出来

转载:http://blog.csdn.net/gwblue/article/details/38865525

27320

扫码关注云+社区

领取腾讯云代金券