《Java网络编程技术》PPT课件.ppt_第1页
《Java网络编程技术》PPT课件.ppt_第2页
《Java网络编程技术》PPT课件.ppt_第3页
《Java网络编程技术》PPT课件.ppt_第4页
《Java网络编程技术》PPT课件.ppt_第5页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

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

文档简介

第9章JavaSocket网络编程案例 Java网络白板程序设计 2 9 1Java网络白板程序设计需求分析 1 应用程序架构 Client Server2 通信协议 TCP协议3 使用对象流ObjectInputStream和ObjectOutputStream传递类的实例对象3 程序界面 3 客户端登陆界面 4 Server 5 参考用例图 1 网络消息协议类 6 参考时序图 9 2使用对象流ObjectInputStream和ObjectOutputStream传递类的实例对象 7 1 使用Serializable接口 在服务器端通过ObjectInputSream和ObjectOutputStream以及Socket类传送dog类的对象到客户端 在客户端接收由服务器端传送过来的dog对象 并输出器属性 1 定义一个接口继承Serializable接口importjava io interfacemyDogextendsSerializable voidsetAge inta intgetAge voidsetName Stringn StringgetName voidrun 8 客户端电子白板界面 dogclass实现myDog接口 classdogimplementsmyDog privateintage privateStringname publicvoidsetAge inta age a publicintgetAge returnage publicvoidsetName Stringn name n publicStringgetName returnname publicvoidrun System out println Icanrunfast 9 服务器端 publicclassOServer publicstaticvoidmain String args throwsIOException dogd newdog d setAge 10 d setName Tom System out println d getAge System out println d getName d run ServerSockets newServerSocket 8888 Socketss s accept OutputStreamout ss getOutputStream BufferedOutputStreamb newBufferedOutputStream out ObjectOutputStreamoo newObjectOutputStream b oo writeObject d oo flush 10 客户端接收dog对象 importjava io publicclassOClient publicstaticvoidmain String args throwsIOException ClassNotFoundException Sockets newSocket 127 0 0 1 8888 InputStreamin s getInputStream BufferedInputStreaminput newBufferedInputStream in ObjectInputStreaminn newObjectInputStream input dogd dog inn readObject System out println client d getAge System out println client d getName d run 11 2 简易白板 12 Server Client 画图类对象 13 系统类图 画图类 importjava io importjava util importjava awt publicclassrandomLineimplementsSerializable Vectorpoints newVector Colorcolor null intx0 y0 x1 y1 Pointpoint start end publicrandomLine Colorc this color c publicvoidsetPoints intx inty point newPoint x y this points add point 14 publicvoidpaint Graphicsg g setColor color EnumerationallPoints points elements start Point allPoints nextElement x0 int start getX y0 int start getY while allPoints hasMoreElements end Point allPoints nextElement x1 int end getX y1 int end getY g drawLine x0 y0 x1 y1 x0 x1 y0 y1 15 16 服务器端 importjava awt importjava awt event importjavax swing importjava io publicclassmyPanelextendsJPanel privaterandomLineranLine newrandomLine Color red privateServerSockets privateSocketss privateObjectOutputStreamoutput privateOutputStreamos privateBufferedOutputStreamb privateGraphicsg publicintx0 y0 x1 y1 17 publicmyPanel setBounds 0 0 400 300 setBackground Color white addMouseMotionListener newmouseAction addMouseListener newmousemovpress this validate newmyThread start publicvoidpaint Graphicsg 18 classmouseActionextendsMouseMotionAdapter publicvoidmouseDragged MouseEvente g getGraphics g setColor Color red x1 e getX y1 e getY g drawLine x0 y0 x1 y1 x0 x1 y0 y1 ranLine setPoints x1 y1 19 classmousemovpressextendsMouseAdapter publicvoidmousePressed MouseEvente x0 e getX y0 e getY publicvoidmouseReleased MouseEvente try os ss getOutputStream b newBufferedOutputStream os output newObjectOutputStream b output writeObject ranLine output flush catch IOExceptionex 20 classmyThreadextendsThread publicvoidrun try s newServerSocket 1234 ss s accept catch IOExceptionex 21 classmyFrameextendsJFrame myPanelp newmyPanel publicmyFrame super 白板服务器 setLayout null setBounds 100 100 400 300 add p setVisible true this addWindowListener newWindowAdapter publicvoidwindowClosing WindowEvente System exit 0 22 publicstaticvoidmain String ss newmyFrame 23 客户端 importjava awt importjava io importjava awt event importjavax swing publicclassmyClientPanelextendsJPanel privaterandomLineranLine privateSocketss privateInputStreamis privateBufferedInputStreamb privateObjectInputStreaminput privateJFramef 24 publicmyClientPanel JFramef this f f setBounds 0 0 400 300 setBackground Color white this validate newmyThread start try ss newSocket 127 0 0 1 1234 catch IOExceptionex f setTitle error 25 publicvoidpaint Graphicsg this ranLine paint g classmyThreadextendsThread publicvoidrun while true try is ss getInputStream b newBufferedInputStream is input newObjectInputStream b ranLine randomLine input readObject repaint catch Exceptionex 26 classmyClientFrameextendsJFrame myClientPanelp newmyClientPanel this publicmyClientFr

温馨提示

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

评论

0/150

提交评论