网络编程技术代码-7_第1页
网络编程技术代码-7_第2页
网络编程技术代码-7_第3页
网络编程技术代码-7_第4页
网络编程技术代码-7_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、1) arith. java public interface arith extends java.rmi.remote int add(int a, int b) throws java.rmi.remoteexception;2) arith工mpljavapublic class arithlmpl extends unicastremoteobject implements arith private string objectname;public arithlmpl(string s) throws remoteexception super();objectname = s;p

2、ublic int add(int a , int b )int c = new int10;for (int i = 0; i < 10; i+)ci = ai + bi;return c;public static void main(string args)rmisecuritymanager sm=new rmisecuritymanager();system.setsecuritymanager(sm);tryarithlmpl obj=new arithlmpl(narithservern);/locateregistry.createregistry(3000);namin

3、g. rebind (arithappjava public class arithapp public static void main(string args) int a=l,2,3,4,5,6,7,8,9,10; int b=l,2,3,4,5,6,7,8,9,10;int resunew int10; tryarith obj=(arith)naminglookup(hrmi:/127.0.0.1:1099/arithserverh); result=obj.add(a, b);catch(exception e)systemout.printin(narithappn+e.getm

4、essage();e.printstacktrace ();system, out .print ( "the sum=11); for(int i=0;i<resultlength;i+)system. out .print (result i + ”f,); system.outprintin (); rmi : /127.0.0.1:1099/arithserver n, ob j);systemout.printin(narithserver bound in registryh;catch(exception e)system out .print in ("

5、;arithlmpl. error: n+e .getmessage ();e.printstacktrace ();1) city2java public interface city2 extends remote int getpopulation() throws remoteexception;int gettemperature() throws remoteexception;2 ) cityfautory j avapublic interface cityfactory extends remotecity2impl getcityserver(string cityname

6、) throws remoteexception;3) city2工mpljavapublic class city2impl extends unicastremoteobject implements city2 private string cityname;publie city2impl() throws remoteexceptionsuper ();publie city2impl(string cityname) throws remoteexceptionsuper ();thiscityname=cityname;public int getpopulation() thr

7、ows remoteexceptionif(cityname.equals(ntorontoh) return 10;else if (cityname .equals (,ottawan) ) return 2;elsereturn 0;public int gettemperature() throws remoteexceptionreturn 1;5) city2app.javapublic class city2app public static void main(string args) remote obj=null;city2 ottawa=null;city2 toront

8、o=null;int ottawapopulation=0;int torontopopulation=0;t rynaming.lookup(n rmi:/1270.0.1:109 9/cityfactory h;catch(exception e)e.printstacktrace();tryottawa=new cityfactorylmpl () . getcityserver (l,ottawan); toronto=new cityfactorylmpl() .getcityserver(htoronto n);catch(remoteexception e)eprintstauk

9、trace();tryo11 awap opulation=ott awa.getpopulation(); torontopopulation=torontogetpopulation ();catch(remoteexception e)e.printstacktrace ();system. out. print in (11 the population of ottawais:n+ottawapopulation);system.out.printin("the population of torontois:n +torontopopulat ion);/1) callb

10、ack.javapublic interface callback void tempchanged(string cityname, int temp);2) callcityjavapublic interface callcity extends remoteint getpopulation(string cityname) throws remoteexception;int gettemperature(string cityname) throws remoteexception; void register(callback cb) throws remoteexception

11、;3) callcitytmpljavapublic class callcityimpl extends unicastremoteobject implements callcity vector list=new vector ();private string name;public callcityimpl(string name) throws remoteexceptionsuper ();this name=name;public int getpopulation(string cityname) throws remoteexception if (cityname equ

12、als (ntoronto11) ) return 10;jelse if (cityname .equals (f,ottawa”) return 2;elsereturn 0;public int gettemperature(string cityname) throws remoteexception return 1;public void register(callback cb)listaddelement(cb);public static void main(string args) system.setsecuritymanager(new rmisecuritymanag

13、er(); t rycallcitylmpl ob j=new callcitylmpl (l,callcityimpln);naming. rebind (n rmi : /127 0 0 1:1099/callcityimpln , ob j); system. out. print in (11 cityserver bo und in registry!11);catch(exception e) e.printstacktrace();4) callcityappj avapublic class callcityapp implements callback,serializabl

14、epublic static void main(string args) int pop = 0;try callcity obj = (callcity)naming. lookup (11 rmi: /127.0.0.1:1099/callcityimpln);pop = obj . getpopulation (ntoronto11); catch (exception e) eprintstauktrace();system.out.printin("the population of toronto is:n + pop);public void tempchanged(

15、string cityname, int temp)system out.printin(ndynamic update:n);system.out.printin("cityname:n+cityname);system.out.printin(n temp:n+temp);5) callcity2工mpljavapublic class callcity2lmpl extends unicastremoteobject implements callcityvector list=new vector();private string name;public callcity2i

16、mpl(string name) throws remoteexception super (); thisname=name;public int getpopulation(string cityname) throws remoteexception if(cityname.equals("toronto")return 10;else if (cityname .equals (,ottawan) ) return 2;else return 0;public int gettemperature(string cityname) throws remoteexce

17、ption return 1;public void register(callback cb)listaddelement(cb);public static void main(string args)system setsecuritymanager(new rmisecuritymanager();trycallcitylmpl ob j=new callcitylmpl (l,callcityimpln); naming. rebind (n rmi : /127 0 0 1:1099/callcityimpln , ob j); system.outprintin(”callcit

18、ylmpl. bound in registry!h);catch(exception e)e.printstacktrace();public void settemp(string cityname,int tmp)for(int i=0;i<listsize();i+)callback cb=(callback)listelementat (i);cbtempchanged(cityname, tmp);6) callcity2appjavapublic class callcity2app implements callback!public static void main(s

19、tring args)int pop=0;callback c=(callback) new callcityapp();callcity obj=null;tryobj=(callcity)naming.lookup(n rmi:/127.0.0.1:1099/callcityhmpu1); pop=ob j . getpopulation (htoronto11);catch(exception e)system.out.print "("arithapp exception:n+e.getmessage(); e.printstacktrace();tryobjregister(c);catch(remoteexception e) system .ou匕.println ("error11); e.printstacktrace();system.out.println(nthe population of toronto is:n + pop);public void tempchanged(string cityname, int temp)system.outprintin(ndynamic update:n)fsystem.outprintln

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论