公用类
代码语言:javascript复制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 ;
}
}
}
服务端:
代码语言:javascript复制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);
}
}
}
}
客户端:
代码语言:javascript复制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);
}
}
}
}