[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.remoting

Getting an object reference from the Server ON THE SERVER???

Chester West

7/19/2004 8:56:00 PM

I'm starting to think this is not possible....

I've got a Singleton remote object created/registered. I can access the
object, modify data, etc., to my hearts content, just as long as I access it
from a remote machine.....THIS MAKES REMOTING relatively useless for me.

The object is registered as follows:
int port=System.Convert.ToInt32(this.txtPort.Text.Trim());

this.serverchannel=new TcpServerChannel ( port ) ;

ChannelServices.RegisterChannel ( this.serverchannel ) ;

RemotingConfiguration.RegisterWellKnownServiceType ( typeof ( User ) ,
"User", WellKnownObjectMode.Singleton ) ;

The object is declared as follows:

[Serializable]

public class User:MarshalByRefObject

{

string myMessage="This is a message from the server";

public User()

{}

public void SetMessage(string Msg)

{

this.myMessage=Msg;

}

public string GetMessage()

{

return this.myMessage;

}}


I need to be able to access the same remote object from BOTH the client AND
the Server, so that I can set values to be seen by the client machines.


My question:
(1) Can this be done
(2) How can you do this
(3) Examples, if possible, please.

Thank you



7 Answers

Sunny

7/19/2004 9:23:00 PM

0

Hi Chester,

In article <#TyJuKdbEHA.556@tk2msftngp13.phx.gbl>, CWEST@Video-
mation.com says...
> My question:
> (1) Can this be done

Yes, it can.

> (2) How can you do this
> (3) Examples, if possible, please.


Do not register the object with RegisterWellKnownServiceType. Instead,
create a local object at the server, and then use
RemotingServices.Marshal to expose it.

That way, you will have access to the object by the local reference, and
clients will have it using remoting.

Be careful, your object as implemented is not thread safe. Every
remoting call is executed on a different thread, so you have to
implement some locks.

Jon Skeet has very good article about threads and thread safety:

http://www.yoda.arachsys.com/csharp/multithre...

Cheers
Sunny

Chester West

7/19/2004 9:58:00 PM

0

Sunny -

I attempted to do this by performing the following:

string url =
"tcp://"+this.txtServerIP.Text.Trim()+":"+this.txtPort.Text.Trim()+"/User" ;

if (this.objSingleton==null)

{

this.objSingleton=new User();

System.Runtime.Remoting.RemotingServices.Marshal(this.objSingleton,url);

this.objSingleton.SetMessage(this.MsgToSet.Text.Trim());

this.btnSetMessage.Enabled=true;

}

else

{

this.objSingleton.SetMessage(this.MsgToSet.Text.Trim());

}



The objSingleton is a object of type "User" declared as follows:
public ClientAndServer.User objSingleton=null;


I am registering it on the client as follows:

string url =
"tcp://"+this.txtServerIP.Text.Trim()+":"+this.txtPort.Text.Trim()+"/User" ;

RemotingConfiguration.RegisterWellKnownClientType ( typeof ( User ), url ) ;

TcpClientChannel channel = new TcpClientChannel( ) ;

ChannelServices.RegisterChannel ( channel ) ;



and then attempting to access it I get an System.Net.Sockets.SocketException
exception (No connection could be made because the target machine actively
refused if) error. Here's how I'm accessing it:

User u = new User( ) ;

MessageBox.Show(u.GetMessage());

What am I missing????

Thanks

Chester


"Sunny" <sunny@newsgroups.nospam> wrote in message
news:%23KmSYadbEHA.4092@TK2MSFTNGP11.phx.gbl...
> Hi Chester,
>
> In article <#TyJuKdbEHA.556@tk2msftngp13.phx.gbl>, CWEST@Video-
> mation.com says...
> > My question:
> > (1) Can this be done
>
> Yes, it can.
>
> > (2) How can you do this
> > (3) Examples, if possible, please.
>
>
> Do not register the object with RegisterWellKnownServiceType. Instead,
> create a local object at the server, and then use
> RemotingServices.Marshal to expose it.
>
> That way, you will have access to the object by the local reference, and
> clients will have it using remoting.
>
> Be careful, your object as implemented is not thread safe. Every
> remoting call is executed on a different thread, so you have to
> implement some locks.
>
> Jon Skeet has very good article about threads and thread safety:
>
> http://www.yoda.arachsys.com/csharp/multithre...
>
> Cheers
> Sunny


Sunny

7/19/2004 10:09:00 PM

0

Hi,

at the server, the url should be only "User", nit the full server path.
Just like what you put in RegisterWellKnown...
Also, it is better to use the overloaded Marshal method, which also
defines the type.

Change your server code to:

In article <umLkWtdbEHA.3728@TK2MSFTNGP10.phx.gbl>, CWEST@Video-
mation.com says...
>
> if (this.objSingleton==null)
>
> {
>
> this.objSingleton=new User();
>
> System.Runtime.Remoting.RemotingServices.Marshal(this.objSingleton,url);

System.Runtime.Remoting.RemotingServices.Marshal(this.objSingleton,
"User", typeof(User));

Cheers
Sunny

Chester West

7/19/2004 10:23:00 PM

0

Sunny -

It still doesn't work....

Do I need to create a port/channel by doing the following
int port=System.Convert.ToInt32(this.txtPort.Text.Trim());

this.serverchannel=new TcpServerChannel ( port ) ;

ChannelServices.RegisterChannel ( this.serverchannel ) ;

this.MyUserObj=new User();

System.Runtime.Remoting.RemotingServices.Marshal(this.MyUserObj,"User",typeo
f(User));

this.MyUserObj.SetMessage(this.MsgToSet.Text.Trim());

Thanks



"Sunny" <sunny@newsgroups.nospam> wrote in message
news:%23mBo9zdbEHA.4092@TK2MSFTNGP11.phx.gbl...
> Hi,
>
> at the server, the url should be only "User", nit the full server path.
> Just like what you put in RegisterWellKnown...
> Also, it is better to use the overloaded Marshal method, which also
> defines the type.
>
> Change your server code to:
>
> In article <umLkWtdbEHA.3728@TK2MSFTNGP10.phx.gbl>, CWEST@Video-
> mation.com says...
> >
> > if (this.objSingleton==null)
> >
> > {
> >
> > this.objSingleton=new User();
> >
> > System.Runtime.Remoting.RemotingServices.Marshal(this.objSingleton,url);
>
> System.Runtime.Remoting.RemotingServices.Marshal(this.objSingleton,
> "User", typeof(User));
>
> Cheers
> Sunny


Chester West

7/19/2004 11:04:00 PM

0

Sunny -

THANKS FOR YOUR HELP!!!

I got the thing finally to work like it should...

Chester
"Chester West" <CWEST@Video-mation.com> wrote in message
news:%230qWi7dbEHA.716@TK2MSFTNGP11.phx.gbl...
> Sunny -
>
> It still doesn't work....
>
> Do I need to create a port/channel by doing the following
> int port=System.Convert.ToInt32(this.txtPort.Text.Trim());
>
> this.serverchannel=new TcpServerChannel ( port ) ;
>
> ChannelServices.RegisterChannel ( this.serverchannel ) ;
>
> this.MyUserObj=new User();
>
>
System.Runtime.Remoting.RemotingServices.Marshal(this.MyUserObj,"User",typeo
> f(User));
>
> this.MyUserObj.SetMessage(this.MsgToSet.Text.Trim());
>
> Thanks
>
>
>
> "Sunny" <sunny@newsgroups.nospam> wrote in message
> news:%23mBo9zdbEHA.4092@TK2MSFTNGP11.phx.gbl...
> > Hi,
> >
> > at the server, the url should be only "User", nit the full server path.
> > Just like what you put in RegisterWellKnown...
> > Also, it is better to use the overloaded Marshal method, which also
> > defines the type.
> >
> > Change your server code to:
> >
> > In article <umLkWtdbEHA.3728@TK2MSFTNGP10.phx.gbl>, CWEST@Video-
> > mation.com says...
> > >
> > > if (this.objSingleton==null)
> > >
> > > {
> > >
> > > this.objSingleton=new User();
> > >
> > >
System.Runtime.Remoting.RemotingServices.Marshal(this.objSingleton,url);
> >
> > System.Runtime.Remoting.RemotingServices.Marshal(this.objSingleton,
> > "User", typeof(User));
> >
> > Cheers
> > Sunny
>
>


Roaringriton

8/15/2009 12:21:00 PM

0

Victal a ?crit :
> Diberville wrote:
>> Agent Double a ?crit :
>>> Rouge vif a ?crit :
>>>> "
>>>> La population d'Hiroshima en a ?t? tr?s satisfaite et continue de
>>>> l'?tre. C'?tait vraiment une bonne id?e, surtout quand on n'?tait
>>>> pas ? Hiroshima.
>>>>
>>>
>>> Ce qui me fait penser ? une question que je me pose souvent.
>>>
>>> On dit toujours qu'apr?s une bombe nucl?aire, le terrain devient
>>> inhabitable pendant des centaines d'ann?es. Comment se fait-il alors
>>> qu'Hiroshima existe encore ?
>> Tout simplement parce que l'essentiel de la radioactivit? ?mise par
>> une bombe A dispara?t en quelques heures, en quelques jours au maximum
>
> exact
>
> les isotopes lib?r?s par une explosion atomique ont tous des demi vie
> relativement courtes exemple iode etc.

Mais qu'il est con !
Et l'Iode elle se transforme en quoi banane ?





> et pour ceux qui en ont des longues la pluie se charche rapidement de les
> emporter
Cesium et Stontium, pris par l'organisme pour du Calcium ...



> remarquez que ca ne justifie pas leur pr?sence tout de m?me

Victal

8/17/2009 8:53:00 AM

0

roaringriton wrote:
> Victal a ?crit :
>> Diberville wrote:
>>> Agent Double a ?crit :
>>>> Rouge vif a ?crit :
>>>>> "
>>>>> La population d'Hiroshima en a ?t? tr?s satisfaite et continue de
>>>>> l'?tre. C'?tait vraiment une bonne id?e, surtout quand on n'?tait
>>>>> pas ? Hiroshima.
>>>>>
>>>>
>>>> Ce qui me fait penser ? une question que je me pose souvent.
>>>>
>>>> On dit toujours qu'apr?s une bombe nucl?aire, le terrain devient
>>>> inhabitable pendant des centaines d'ann?es. Comment se fait-il
>>>> alors qu'Hiroshima existe encore ?
>>> Tout simplement parce que l'essentiel de la radioactivit? ?mise par
>>> une bombe A dispara?t en quelques heures, en quelques jours au
>>> maximum
>>
>> exact
>>
>> les isotopes lib?r?s par une explosion atomique ont tous des demi vie
>> relativement courtes exemple iode etc.
>
> Mais qu'il est con !
> Et l'Iode elle se transforme en quoi banane ?

pour votre information plus un ?l?ment lourd perd de sa masse et plus il
devient stable

L'iode n'?chape pas ? la r?gle


>
>
>
>
>
>> et pour ceux qui en ont des longues la pluie se charche rapidement
>> de les emporter
> Cesium et Stontium, pris par l'organisme pour du Calcium .


oui c'est pour cette raison qu'il est fortement recommand? ( si quelqu'un
est entrer en contatc avec une source de radio activit?? de prendre des
doses massives de calcium et d'iode pour minimiser lesa risques de la
fixation des isotopes en question dans l'organisme ..



--
? Il n'existe pas de nuit assez profonde pour arr?ter un rayon de
lumi?re et pourtant le vide ne s'?claire pas; la lumi?re ne prend
corps que dans l'oil qui la re?oit ?

Extrait de sous-Bois d'A.G.