TCPClient/ServerChannel
-
Hallo,
ich versuche gerade zwischen 2 Programmen Daten zu verschicken und bin da auf TcpServer/ClientChannel gestoßen.
Der Code ssieht so aus:Remotable.h
#include <iostream> using namespace System; using namespace System::Runtime::Remoting; using namespace std; public ref class Remotable: public MarshalByRefObject { private: int callCount; public: Remotable() : callCount( 0 ) {} int GetCount() { callCount++; cout << "Callcount: " << callCount << endl; return (callCount); } };
Server:
channel = gcnew TcpServerChannel(8085); ChannelServices::RegisterChannel(channel, false); RemotingConfiguration::RegisterWellKnownServiceType( Remotable::typeid, "Remotable.rem", WellKnownObjectMode::Singleton); //MarshalByRefObject:: //RemotingConfiguration::RegisterWellKnownServiceType(WellKnownObjectMode::Singleton ); cout << "Channel Name: " << To_string( channel->ChannelName) << endl; ChannelDataStore^ data = dynamic_cast<ChannelDataStore^>(channel->ChannelData); System::Collections::IEnumerator^ myEnum = data->ChannelUris->GetEnumerator(); while ( myEnum->MoveNext() ) { String^ uri = safe_cast<String^>(myEnum->Current); cout << To_string(uri)<<endl; } // Wait for method calls. cout << "Listening" << endl;
Client:
TcpClientChannel^ clientChannel = gcnew TcpClientChannel; ChannelServices::RegisterChannel( clientChannel ); // Show the name and priority of the channel. LB->Items->Add( "Channel Name: "+ clientChannel->ChannelName ); LB->Items->Add( "Channel Priority: "+clientChannel->ChannelPriority.ToString() ); // Obtain a proxy for a remote object. RemotingConfiguration::RegisterWellKnownClientType( Remotable::typeid, "tcp://localhost:8085/Remotable.rem" ); // Call a method on the object. try{ Remotable ^ remoteObject = gcnew Remotable; LB->Items->Add( remoteObject->GetCount() ); //remoteObject = (Remotable^)Activator::GetObject(Remotable::typeid,"tcp://localhost:8085/Remotable.rem" ); //remoteObject->GetCount(); } catch(Exception^ ex){ MessageBox::Show(ex->Message); }
Klappt auch alles soweit. Wenn ich beim Clienten ne Remotable Instanz erstelle, wird callcount auf beiden Applikationen inkrementiert ausgegeben.
Aber wie greife ich vom Server auf diese Instanz von Remotable zu?
Einfach ne Variable von dem Typ zu erstllen klappt nicht.veio
-
Probiers mal gleich wie der Client. (Sozusagen ein Client im Server).
-
Klappt nicht. Gibt eine RemotingException, die sich irgendwie nicht abfangen lässt.
Habe folgenden Code hinter das cout <<"listening"; eingefügt:try{ TcpClientChannel^ clientChannel = gcnew TcpClientChannel; // Show the name and priority of the channel. cout << "Channel Name: "<< To_string(clientChannel->ChannelName)<< endl; cout << "Channel Priority: "<<clientChannel->ChannelPriority <<endl; } catch(Exception^ ex){ MessageBox::Show(ex->Message); } // Obtain a proxy for a remote object. RemotingConfiguration::RegisterWellKnownClientType( Remotable::typeid, "tcp://localhost:8085/Remotable.rem" ); // Call a method on the object. try{ Remotable ^ remoteObject = gcnew Remotable; cout << remoteObject->GetCount()<< endl; } catch(RemotingException^ ex){ MessageBox::Show(ex->Message); }