




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、广州大学华软软件学院 软件工程系JAVA网络编程技术(SS0013)课程设计Java Socket网络编程-电子白板即时通信程序设计 课程设计指导书2011 -2012学年 第一学期课程名称:JAVA网络编程技术 课程代码:SS0013计划学时: 68 学分:4课程性质: 必修、考试 面向专业:软件工程课程负责人:邹立杰授课老师:邹立杰班级(小班代码): 姓名: 陈甜甜 学号: 广州大学华软软件学院South China Institute of Software Engineering, GuangZhou University1、 课程设计题目:Java Socket网络编程 -电子白板即
2、时通信程序设计2、 课程设计要求:通过本课程设计深刻理解Java网络编程的基本方法和技巧本课程设计所要实现的电子白板程序主要应用的编程技术:(1) Java SWing GUI 界面设计技术(2) Java Socket 网络编程技术(3) Java ObjectInputStream 和 ObjectOutputStream 类的实例发送和接收对象的功能(4) 实现Serializable接口编写功能类,实现该类对象的网络发送和接收。3、 程序基本功能:(1)服务器端:提供提供白板图形和文字消息的转发转发功能,把当前的登录到服务器上的所有客户端的电子白板内容群发转发给在线的每个客户,以及群发
3、转发即时文字消息。(2)服务器端保存自第一个用户登录后的所有白板内容。(3)程序的客户端通过 Java Socket 网络协议和服务器实现网络通信,接收服务器端发送过来的白板内容消息并且绘制在该客户端的白板客户区内,接收显示文本消息;绘制白板并发送到服务器端,为所有客户端共享。(4)客户端登录时,发送登录消息,服务器端把当前保存的白板内容发送到该客户端,该客户端收到由服务器发送来的白板内容绘制到客户区。(5)电子白板图形有圆形、填充圆形、矩形、填充矩形、圆角矩形、填充圆角矩形、直线和随机曲线等。4、 GUI 设计GUI设计使用SUN公司SWing可视JavaBean组件技术或IBM公司的SWT
4、可视JavaBean组件技术。GUI设计界面客户端:客户端使用SWing组件技术进行GUI界面设计1)参考界面客户端登陆: 客户端主程序界面:服务器端:服务器端为一个控制台应用程序,主要功能是传递客户端消息。5、主要代码:(1)Serializable接口应用:在服务器端通过ObjectInputSream 和ObjectOutputStream以及Socket类传送dog类的对象到客户端,在客户端接收由服务器端传送过来的dog对象,并输出器属性。时序图:类图:1)服务器端代码import java.io.*;import .*;interface myDog extends
5、java.io.Serializable void setAge(int a);int getAge();void setName(String n);String getName();void run();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
6、name;public void run() System.out.println(I can run fast!);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
7、 = s.accept();OutputStream out = ss.getOutputStream();BufferedOutputStream b = new BufferedOutputStream(out);ObjectOutputStream oo = new ObjectOutputStream(b);oo.writeObject(d);oo.flush();2)客户端代码import java.io.*;import .*;public class OClient public static void main(String args) throws IOExc
8、eption,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.
9、getName();d.run();编译运行:服务器端:客户端:运行结果可以看出,从服务器发送的客户端的dog对象,属性被取出、输出。结果正确。(2)简易网络画板电子白板程序1) 功能:在服务器端实现简易画板,在客户端同步服务器端的画板2) 运行截图:服务器户、客户端界面: 3)系统类图:4)实现:a. 首先封装一个ranomLine类,作为用鼠标划线的轨迹的记录器,并封装实现自动划线的方法:其中有一个向量Vector域,用来记录鼠标划线所包含的点的对象,paint(Graphics g)方法是实现遍历Vector中的点,并将这些点画出。import java.io.*;import java
10、.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);public void paint(Graphics
11、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;randomLine类时序图:b
12、.服务器端:import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import .*;public class myPanel extends JPanelprivate randomLine ranLine=new randomLine(Color.red);private ServerSocket s;private Socket ss;private ObjectOutputStream output;private OutputStream os ;private
13、BufferedOutputStream b ;private Graphics g;public int x0,y0,x1,y1;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)class mouseAction exte
14、nds MouseMotionAdapterpublic 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);class mousemovpress extends MouseAdapterpublic void mousePressed(MouseEvent e)x0=e.getX();y0=e.getY();public void m
15、ouseReleased(MouseEvent e)tryos = ss.getOutputStream();b = new BufferedOutputStream(os);output = new ObjectOutputStream(b);output.writeObject(ranLine);output.flush();catch(IOException ex)class myThread extends Threadpublic void run()trys=new ServerSocket(1234);ss=s.accept();catch(IOException ex)clas
16、s myFrame extends JFramemyPanel 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););public static void main(String ss)new myFrame();c.客户端impo
17、rt 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;public myClientPa
18、nel(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);public void paint(Graphics g) this.ranLine.paint(g);class myThread extends Thread public void run() while (
19、true) try is = ss.getInputStream();b = new BufferedInputStream(is);input = new ObjectInputStream(b);ranLine = (randomLine)input.readObject();repaint(); catch (Exception ex) /f.setTitle(error-errpr);class myClientFrame extends JFrame myClientPanel p = new myClientPanel(this);public myClientFrame() su
20、per(白板客户端);setLayout(null);setBounds(100, 100, 400, 300);add(p);setVisible(true);this.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););public static void main(String ss) new myClientFrame();4)思考问题:为什么2两个客户端不同(见上图),另一客户端的每段线段是连在一起的?提出方案实现客户端和服务器端图形一致。(3)电子
21、白板即时通信程序设计1)系统UML建模:用例图:时序图:2)网络消息协议类电子白板即时通信程序进行网络通信要建立在一套完整的通信协议之上,其中有TCP/IP等网络协议,另外还要为电子白板即时通信程序制定一些自定义的通信协议,这些协议要对各种需要在网络通信中传送的各种消息进行协议规定。本课程设计程序需要传送很多的网络消息。3)系统类图:除了添加随机曲线图形消息类的所有基本图形类消息类都实现Imessage接口Imessage接口类图:代码:import java.io.*;public interface IMessage extends Serializablea.LoninMessage类类
22、图:代码:import java.util.*;public class LoginMessage implements IMessage PaintedObjects paintedOjbs;int id;Vector randLineVector;public LoginMessage(PaintedObjects list, Vector randLines, int id) this.paintedOjbs = list;this.randLineVector = randLines;this.id = id;public PaintedObjects getList() return
23、 paintedOjbs;public Vector getRlines() return randLineVector;public int getId() return id;b. 白板客户端添加图形消息类:ClientAddObjMsg类图:代码:public class ClientAddObjMsg implements IMessage Object obj; public ClientAddObjMsg (Object obj) this.obj = obj; public Object getObj () return obj; c. 客户端移动图形消息类:ClientRepl
24、aceObjMsg类图:代码:import java.io.Serializable;public class ClientReplaceObjMsg implements IMessage Object id; Object object; public ClientReplaceObjMsg(Object object,Object id) this.object = object; this.id = id; public Object getElement () return object; public Object getId() return id; d. 客户端画随机曲线消息类
25、:ClientAddRanLineMsg类图:代码:import java.io.*;public class ClientAddRanLineMsg implements Serializable RanLineObj element; public ClientAddRanLineMsg(RanLineObj line) this.element = line; public RanLineObj getLine() return element; e. 客户端退出消息类:类图:代码:public class ClientQuitMsg implements IMessageprivate
26、 String name ;public String getName() return name;public void setName(String name) = name;f. 服务器端移除图形消息类:类图:代码:public class ServerReplacedMessage implements IMessage Object oldId; Object id; Object object; public ServerReplacedMessage(Object oldID, Object id, Object element) this.oldId = o
27、ldID; this.id = id; this.object = element; public Object getOldID () return oldId; public Object getID () return id; public Object getObject () return object; g. 服务器端添加图形消息类:ServerAddObjMessage类图:代码:public class ServerAddObjMessage implements IMessage Object id; Object object; public ServerAddObjMes
28、sage(Object id, Object element) this.id = id; this.object = element; public Object getID () return id; public Object getElement () return object; 4)图形类:除随机曲线类之外的所有图形类都实现Element接口Element接口类图:代码:import java.awt.*;public interface Element extends java.io.Serializable public void setPara(int x,int y,int
29、 z,int r,Color c); public void paint(Graphics g); public Rectangle getBounds(); public int getType(); public Color getColor();a.随机曲线类:RanLineObj类图:代码:import java.awt.*;import java.io.*;import java.util.*;public class RanLineObj implements Serializable Vector points = new Vector(); Color color = null
30、; int x0 , y0 , x1 , y1; public static int type = 8 ; public RanLineObj(Color c) color = c; public void setPoints(int x, int y) Point point = new Point(x,y); points.add(point); public void paint(Graphics g) g.setColor (color); Enumeration allpoints = points.elements(); Point start = (Point)allpoints
31、.nextElement(); x0 = (int)start.getX(); y0 = (int)start.getY(); while(allpoints.hasMoreElements() Point end = (Point)allpoints.nextElement(); x1 = (int)end.getX(); y1 = (int)end.getY(); g.drawLine(x0,y0,x1,y1); x0 = x1; y0 = y1; b.圆角矩形类:类图:代码:import java.awt.*;public class RoundRecObj implements Ele
32、ment public int x1 , y1 , width , height; public int xold,yold,wold,hold; public Rectangle bounds = null ; public Color color; private static int type = 3; public RoundRecObj(); public void setPara(int x, int y , int w ,int h, Color c) x1 = x; y1 = y; width = w; height = h; color = c; bounds = new R
33、ectangle(x,y,w,h); public int getType() return type; public Rectangle getBounds() return bounds; public void paint(Graphics g) g.setColor (color); g.drawRoundRect (x1 , y1 , width , height,20,20); public Color getColor() return color; 等还有直线类、矩形类、填充矩形类、填充圆角矩形类、椭圆类、填充椭圆类等。3)其他类a.绘制的图形容器类类图:代码:package
34、project1;import java.io.*;import java.util.*;public class PaintedObjects implements Serializable, Cloneable protected Vector paintedObjVector = new Vector();protected Vector objIds = new Vector();public synchronized Object add(Object element) Object id = new Integer(System.identityHashCode(element);
35、addElementWithID(id, element);return id;public synchronized void addElementWithID(Object id, Object element) objIds.addElement(id);paintedObjVector.addElement(element);public synchronized Object replaceObject(Object oldID, Object element) int idx = objIds.indexOf(oldID);if (idx = 0) objIds.removeEle
36、mentAt(idx);paintedObjVector.removeElementAt(idx);return add(element); else return null;public synchronized boolean replaceOjbWithID(Object oldID, Object id,Object element) int index = objIds.indexOf(oldID);if (index = 0) objIds.removeElementAt(index);paintedObjVector.removeElementAt(index);objIds.a
37、ddElement(id);paintedObjVector.addElement(element);return true; else return false;public synchronized Object getObjectId(Object element) int idx = paintedObjVector.indexOf(element);return (idx = 0) ? objIds.elementAt(idx) : null;public synchronized Enumeration elements() return (Vector) paintedObjVe
38、ctor.clone().elements();public synchronized Object clone() try return super.clone(); catch (CloneNotSupportedException ex) return null;public synchronized int size() return paintedObjVector.size();b.白板服务器类类图:代码:import java.io.*;import .*;import java.util.*;public class WhiteBoardServer exten
39、ds Thread public static int port = 1234;static PaintedObjects paintedObjects = new PaintedObjects();static Vector rlineVector = new Vector();static Vector streamVector = new Vector();Socket socket;public WhiteBoardServer(Socket socket) System.out.println(Accepted from + socket.getInetAddress() + .);
40、this.socket = socket;public void run() try ObjectOutputStream outputStream = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream();outputStream.flush();ObjectInputStream inputStream = new ObjectInputStream(new BufferedInputStream(socket.getInputStream();handle(inputStream, outputS
41、tream); catch (Exception ex) ex.printStackTrace(); finally System.out.println(从 + socket.getInetAddress() + 断开);try socket.close(); catch (IOException ignored) void handle(ObjectInputStream inputStream, ObjectOutputStream outputStream)throws IOException, ClassNotFoundException try synchronized (stre
42、amVector) /建立客户端通信流streamVector.addElement(outputStream);outputStream.writeObject(new LoginMessage(PaintedObjects) paintedObjects.clone(),(Vector) rlineVector.clone(), streamVector.indexOf(outputStream);System.out.println(发送LoginMessage消息);outputStream.flush();handleMessage(inputStream); finally str
43、eamVector.removeElement(outputStream); /处理各种消息void handleMessage(ObjectInputStream inputStream) throws IOException,ClassNotFoundException while (true) Object message = inputStream.readObject();if (message instanceof IMessage) /处理所有对基本图形的操作的消息message = (IMessage) message;if (message instanceof Client
44、AddObjMsg) /如果接收到客户端添加基本图形消息Object obj = (ClientAddObjMsg) message).getObj();synchronized (streamVector) /将添加对象加入到白板已有对象的类中Object id = paintedObjects.add(obj);/通知所有客户添加图形对象notifyAll(new ServerAddObjMessage(id, obj); else if (message instanceof ClientReplaceObjMsg) /如果接收到客户端移动图形的消息Object oldID = (Cli
45、entReplaceObjMsg) message).getId();if (oldID != null) /将原来位置的对象提取出Object element = (ClientReplaceObjMsg) message).getElement();synchronized (paintedObjects) /将原来位置的图形对象加入到需要被删除的列表中Object id = paintedObjects.replaceObject(oldID,element);if (id != null) /通知所有客户端更新本地白板notifyAll(new ServerReplacedMessag
46、e(oldID, id,element); else if (message instanceof ClientQuitMsg) /如果接收到用户退出的消息System.out.println(收到退出消息.);return; else System.out.println(Unknown message: + message); else if (message instanceof ClientAddRanLineMsg) /如果接收到用户添加随机曲线的消息 /将点对信息加入的需要添加的图形对象列表rlineVector.addElement(ClientAddRanLineMsg) me
47、ssage).getLine(); /通知所有客户端更新本地白板NotifyRanLine(ClientAddRanLineMsg) message);static void addOutputStream(ObjectOutputStream out) streamVector.addElement(out);/通知所有客户端添加随机曲线static void rmvOutputStream(ObjectOutputStream out) streamVector.removeElement(out);static void NotifyRanLine(ClientAddRanLineMsg
48、 message) System.out.println(Broadcast addRandomLineMsg to + streamVector.size() + recipients.);for (int i = 0; i streamVector.size(); +i) ObjectOutputStream out = (ObjectOutputStream) streamVector.elementAt(i);try out.writeObject(message);out.flush(); catch (Exception ex) ex.printStackTrace();System.err.println(发送ClientAddRanLineMsg消息失败); /通知所有客户端对基本图形进行更新static v
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 人工智能数据挖掘与分析技术应用协议
- 哇她这个人400字(15篇)
- 二零二五年度房地产项目场地平整与基础施工合作协议
- 2025版数字化办公设备租赁与维护服务合同
- 2025至2030家用排气扇行业市场发展分析及前景趋势与投资管理报告
- 2025版汽车租赁公司承包权租赁与经营管理合同
- 2025年度路灯安装工程安全与质量管理合同范本
- 二零二五年度煤炭装卸搬运与销售服务合同
- 2025年车辆租赁及托管服务合同范本
- 二零二五年厂房转租与物业管理合作协议
- 2024新人教七年级上册英语单词表衡水体字帖
- 0-3岁婴幼儿亲子关系与互动智慧树知到期末考试答案章节答案2024年杭州师范大学
- QBT 2024-2012 凹版塑料薄膜复合油墨
- 普通动物学(全套课件1069P)
- 2024合同作废说明范文
- DZ∕T 0289-2015 区域生态地球化学评价规范(正式版)
- 景点联票销售策略与实证研究
- 国外教学方法研究现状
- SYT 6293-2021 勘探试油工作规范-PDF解密
- 钢结构房屋拆除施工方案
- 皮肤科病人的药物不良反应护理与预防
评论
0/150
提交评论