2009-06-30 4 views
2

.Net Remoting을 사용하고 있으며 서버에서 수정 된 원격 객체에 액세스하려고하면 def : 개체가 생성됩니다. 서버에서 생성 한 MRB 객체 클라이언트에 getString()이 호출되면 "Set On Server"가 인쇄됩니다..Net Remoting (C#)의 문제점

현재 클라이언트에서 null 문자열이 표시되므로 클라이언트에서 MRB 개체가 호출 될 때 MRB 개체가 클라이언트에 보내지지 않습니다. 다른 모든 클래스 등을 유감스럽게 생각하지만 그 유일한 방법은 가능한 한 많이 수정했습니다.

내가 실제로 원하는 것은 "서버에 설정"클라이언트가 실행될 때이 인쇄 된 것입니다.

using System; 
using System.Runtime.Remoting; 
using RemoteType; 
namespace SimpleServer 
{ 
    class SimpleServer 
    { 
     public static MyRemoteObject MRB = null; 
     static void Main(string[] args) 
     { 
      RemotingConfiguration.Configure("RemotingAppServer.exe.config"); 
      MRB = new MyRemoteObject(); 
      MRB.setString("Set on the server"); 
      Console.WriteLine(MRB.getString()); 
      RemotingServices.Marshal((MRB), "MyRemoteObject"); 
      Console.WriteLine("Press return to exit"); 
      Console.ReadLine(); 
     } 
    } 

그리고 .NET 원격 구성의 app.config :

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.runtime.remoting> 
     <application> 
      <channels> 
       <channel ref="tcp" port="8000" /> 
      </channels> 
      <service> 
       <wellknown mode="Singleton" 
       type="RemoteType.MyRemoteObject, RemoteType" 
       objectUri="MyRemoteObject" /> 
      </service> 
     </application> 
    </system.runtime.remoting> 
</configuration> 

마지막으로 클라이언트 :

using System; 
using System.Runtime.Remoting.Lifetime; 
namespace RemoteType 
{ 
    public class MyRemoteObject : System.MarshalByRefObject 
    { 
     private string sharedString; 
     public string getString() 
     { 
      return sharedString; 
     } 
     public void setString(string value) 
     { 
     sharedString = value; 
     } 
     public MyRemoteObject() 
     { 
      Console.WriteLine("MyRemoteObject Constructor Called"); 
     } 

     public override object InitializeLifetimeService() 
     { 
      return null; 
     } 
     public string Hello() 
     { 
      return "Hello, Welcome to .Net Remoting !"; 
     } 
    } 

여기 알고는 서버입니다

using System; 
using System.Runtime.Remoting; 
using RemoteType; 
namespace SimpleClient 
{ 
    class SimpleClient 
    { 
     static void Main(string[] args) 
     { 
      RemotingConfiguration.Configure("RemoteClient.exe.config"); 
      MyRemoteObject robj = new MyRemoteObject(); 
      Console.WriteLine(robj.Hello() + " " + robj.getString()); 
      Console.ReadLine(); 
     } 
    } 
} 

그리고 그 구성 :

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.runtime.remoting> 
     <application name = "SimpleClient"> 
      <client> 
       <wellknown 
       type="RemoteType.MyRemoteObject,RemoteType" 
       url="tcp://localhost:8000/MyRemoteObject"/> 
      </client> 
      <channels> 
       <channel ref="tcp" port="0"/> 
      </channels> 
     </application> 
    </system.runtime.remoting> 
</configuration> 

답변

2

코드를 테스트하고 완벽하게 작동했습니다. 저는 세 개의 프로젝트를 설정했습니다. 하나는 서버, 다른 하나는 클라이언트, 다른 하나는 서버와 클라이언트가 원격 객체로 공유했습니다. 원격 app.config에서 잘 알려진 항목을 제거 할 수 있습니다. 코드로 이미 정렬 한 것입니다.

+0

클라이언트가 실행될 때 클라이언트에 인쇄 된 "서버에 설정"이 나타 났습니까? – daniel

+0

@daniel : 한 번에 서버와 클라이언트를 시작하도록 프로젝트를 설정 했습니까? 그렇다면 클라이언트 코드의 시작 부분에 Thread.Sleep을 추가하는 것이 도움이 될 수 있습니다. –

+0

예, 두 화면의 클라이언트 및 서버에서 "서버에 설정"을 보았습니다. 나는 서버용과 클라이언트 용의 두 가지 솔루션을 가지고있었습니다. 먼저 서버를 시작한 다음 여러 번 클라이언트를 시작했습니다. 모두 똑같은 성공 결과를 얻었습니다. – jmservera

0

응용 프로그램이 종료되는 즉시 소켓을 닫으시겠습니까?

방화벽 사용자 환경에서 TCP 포트 8000을 차단 될 수 있습니다 여러 번 빠르게이 발생할 수 TCP 통신 문제

0

을 테스트하는 경우? 테스트로 http로 전환 할 수 있습니다.