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

下载本文档

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

文档简介

第9章 Java Socket网络编程案例,Java 网络白板程序设计,2,9.1 Java 网络白板程序设计 需求分析,1. 应用程序架构: Client/Server 2. 通信协议: 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 接口 import java.io.*; import .*; interface myDog extends Serializable void setAge(int a); int getAge(); void setName(String n); String getName(); void run(); ,8,客户端电子白板界面,dog class 实现 myDog 接口,class dog implements myDog private int age; private String name; public void setAge(int a) age=a; public int getAge() return age; public void setName(String n) name = n; public String getName() return name; public void run() System.out.println(“I can run fast!“); ,9,服务器端,public class OServer public static void main(String args) throws IOException dog d = new dog(); d.setAge(10); d.setName(“Tom“); System.out.println(d.getAge(); System.out.println(d.getName(); d.run(); ServerSocket s = new ServerSocket(8888); Socket ss = s.accept(); OutputStream out = ss.getOutputStream(); BufferedOutputStream b = new BufferedOutputStream(out); ObjectOutputStream oo = new ObjectOutputStream(b); oo.writeObject(d); oo.flush(); ,10,客户端接收dog对象,import java.io.*; import .*; public class OClient public static void main(String args) throws IOException,ClassNotFoundException Socket s=new Socket(““,8888); InputStream in=s.getInputStream(); BufferedInputStream input=new BufferedInputStream(in); ObjectInputStream inn=new ObjectInputStream(input); dog d=(dog)inn.readObject(); System.out.println(“client:“+d.getAge(); System.out.println(“client:“+d.getName(); d.run(); ,11,(2)简易白板,12,Server,Client,画图类对象,13,系统类图,画图类,import java.io.*; import java.util.*; import java.awt.*; public class randomLine implements Serializable Vector points=new Vector(); Color color=null; int x0,y0,x1,y1; Point point,start,end; public randomLine(Color c) this.color=c; public void setPoints(int x,int y) point=new Point(x,y); this.points.add(point); ,14,public void paint(Graphics g) g.setColor(color); Enumeration allPoints=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,服务器端,import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import .*; public class myPanel extends JPanel private randomLine ranLine=new randomLine(Color.red); private ServerSocket s; private Socket ss; private ObjectOutputStream output; private OutputStream os ; private BufferedOutputStream b ; private Graphics g; public int x0,y0,x1,y1;,17,public myPanel() setBounds(0,0,400,300); setBackground(Color.white); addMouseMotionListener(new mouseAction(); addMouseListener(new mousemovpress(); this.validate(); new myThread().start(); public void paint(Graphics g) ,18,class mouseAction extends MouseMotionAdapter public void mouseDragged(MouseEvent e) 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,class mousemovpress extends MouseAdapter public void mousePressed(MouseEvent e) x0=e.getX(); y0=e.getY(); public void mouseReleased(MouseEvent e) try os = ss.getOutputStream(); b = new BufferedOutputStream(os); output = new ObjectOutputStream(b); output.writeObject(ranLine); output.flush(); catch(IOException ex) ,20,class myThread extends Thread public void run() try s=new ServerSocket(1234); ss=s.accept(); catch(IOException ex) ,21,class myFrame extends JFrame myPanel p=new myPanel(); public myFrame() super(“白板服务器“); setLayout(null); setBounds(100,100,400,300); add(p); setVisible(true); this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); ,22,public static void main(String ss) new myFrame(); ,23,客户端,import java.awt.*; import java.io.*; import .*; import java.awt.event.*; import javax.swing.*; public class myClientPanel extends JPanel private randomLine ranLine; private Socket ss; private InputStream is; private BufferedInputStream b; private ObjectInputStream input; private JFrame f;,24,public myClientPanel(JFrame f) this.f=f; setBounds(0, 0, 400, 300); setBackground(Color.white); this.validate(); new myThread().start(); try ss = new Socket(““, 1234); catch (IOException ex) f.setTitle(“error“); ,25,public void paint(Graphics g) this.ranLine.paint(g); class myThread extends Thread public void run() while (true) try is = ss.getInputStream(); b = new BufferedInputStream(is); input = new ObjectInputStream(b); ranLine = (randomLine)input.readObject(); repaint(); catch (Exception ex) ,26,class myClientFrame extends JFrame myClientPanel p = new myClientPanel(this); public myClientFram

温馨提示

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

评论

0/150

提交评论