提示:由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。下面是我的全部代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;using System.Runtime.Serialization.Formatters;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Serialization;using DelegateObject;namespace Server
{
    public partial class Form1: Form
    {
        public Form1( )
        {
            InitializeComponent( );
        }        private void Form1_Load( object sender, EventArgs e )
        {            RemotingConfiguration.Configure( @"F:\DotnetTest\RemotingDemo\Server\Server\Server.config");            this.textBox1.Text += "Server Started!";
        }
    }
}Server.config<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <service>
        <wellknown 
           mode="Singleton" 
           type="DelegateObject.ServerObj, DelegateObject" 
           objectUri="Obj.soap"
            />
      </service>
      <channels>
        <channel ref="tcp"  port="9000">
          <serverProviders>
            <formatter ref="soap" typeFilterLevel="Full" />
            <formatter ref="binary" typeFilterLevel="Full" />
          </serverProviders>
        </channel>
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.Runtime.Serialization.Formatters;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using System.Runtime.Remoting.Channels.Http;
    using System.Runtime.Serialization;using DelegateObject;namespace Receiver
    {
        public partial class Form1: Form
        {
            private ServerObj obj = null;        public Form1( )
            {
                InitializeComponent( );
            }        private void Form1_Load( object sender, EventArgs e )
            {
                RemotingConfiguration.Configure( @"F:\DotnetTest\RemotingDemo\Server\Receiver\Receiver.config" );            obj = ( ServerObj )Activator.GetObject( typeof( ServerObj ), "tcp://localhost:9000/Obj.soap" );
                ReceiverObj rObj = new ReceiverObj(  );
                rObj.ReceiverEvent += new DelegateObjHandle( Refresh_Mohed );
                obj.Sending += new DelegateObjHandle( rObj.OnReceiverEvent );
            }        private void Refresh_Mohed( string value )
            {
                this.textBox1.Text += value;
            }
        }
    }Receiver.config<configuration>
      <system.runtime.remoting>
        <application>
          <client>
            <wellknown 
               type="DelegateObject.ServerObj, DelegateObject"
               url="tcp://localhost:9000/Obj.soap" />
          </client>
          <channels>
            <channel 
               ref="tcp" 
               port="0">
              <serverProviders>
                <provider ref="wsdl" />
                <formatter ref="soap" typeFilterLevel="Full" />
                <formatter ref="binary" typeFilterLevel="Full" />
              </serverProviders>
            </channel>
          </channels>
        </application>
      </system.runtime.remoting>
    </configuration>
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;using System.Collections;
    using System.Runtime.Serialization.Formatters;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using System.Runtime.Remoting.Channels.Http;
    using System.Runtime.Serialization;using DelegateObject;namespace Client
    {
        public partial class Form1: Form
        {
            private ServerObj obj;
            public Form1( )
            {
                InitializeComponent( );
            }        private void Form1_Load( object sender, EventArgs e )
            {
                BinaryServerFormatterSinkProvider server = new BinaryServerFormatterSinkProvider( );
                BinaryClientFormatterSinkProvider client = new BinaryClientFormatterSinkProvider( );
                server.TypeFilterLevel = TypeFilterLevel.Full;            IDictionary props = new Hashtable( );
                props[ "port" ] = 0;            TcpChannel tcp = new TcpChannel( props, client, server );
                ChannelServices.RegisterChannel( tcp );            obj = ( ServerObj )Activator.GetObject( typeof( ServerObj ), "tcp://localhost:9000/Obj.soap" );
            }        private void button1_Click( object sender, EventArgs e )
            {
                obj.OnSending( this.textBox1.Text );
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;namespace DelegateObject
    {
        public delegate void DelegateObjHandle( string value );    public class ServerObj:MarshalByRefObject
        {
            public override object InitializeLifetimeService( )
            {
                return null;
            }        public void OnSending( string value )
            {
                if ( Sending != null )
                {
                    Sending( value );
                }
            }
            public event DelegateObjHandle Sending;
           
        }
        public class ReceiverObj: MarshalByRefObject
        {
            public  ReceiverObj( )
            {
               // obj.Sending += new DelegateObjHandle( OnReceiverEvent );
            }        public override object InitializeLifetimeService( )
            {
                return null;
            }        public void OnReceiverEvent( string value )
            {
                if ( ReceiverEvent != null )
                {
                    ReceiverEvent( value );
                }
            }
            public event DelegateObjHandle ReceiverEvent;    }    public interface IServerObj
        {
            void OnSending( string value );
            event DelegateObjHandle Sending;
        }
      
    }