




免费预览已结束,剩余3页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验指导书实验五、UDP绘制温度曲线一、实验目的1 熟悉UDP编程的基本过程2 理解UDP协议相关的基本概念3 掌握UDP协议与线程的结合方式二、实验内容1 利用UDP协议实现一个简单的白板程序,要求如下:用户在客户端界面上单击鼠标左键后程序在鼠标单击位置画20*20的圆形,单击右键后程序在鼠标单击位置画20*20的圆。服务器界面上将同时显示与客户端一致的画面。2 采用UDP编写一个网络程序,该程序的客户端每隔1秒向服务端发送一个温度值,服务端能够接收该温度值并显示一条动态温度曲线。其界面如下:3 如果要求每增加一个客户端则增加一条动态曲线,并且显示一条所有客户端的平均温度曲线,该程序应该如何改变?三、实验步骤1 针对第1题,定义数据传递的格式为:对象类型(圆、正方形等等)X坐标Y坐标1字节2字节2字节2 实现客户端绘制程序,其代码如下:package lab.lab5;import java.applet.*;import java.awt.*;import java.awt.event.*;import java.io.*;import .*;import java.util.*;public class BoardClient extends Applet implements MouseListenerprivate Graphics gBuf;private Image imgBuf;private ArrayList shapeList=new ArrayList();public static void main(String args) Frame app = new Frame(白板客户端);app.setSize(400, 400);app.setLocationByPlatform(true);BoardClient applet = new BoardClient();applet.addMouseListener(applet);app.add(applet);app.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent event) event.getWindow().dispose();System.exit(0););app.show();applet.start();public void init() public void paint(Graphics g) int graphWidth = bounds().width;int graphHeight = bounds().height;imgBuf = createImage(graphWidth, graphHeight);gBuf = imgBuf.getGraphics();gBuf.clearRect(0, 0, graphWidth, graphHeight);Iterator it=shapeList.iterator();Shape shape;while (it.hasNext()shape=(Shape)it.next();if (shape.type=Shape.CIRCLE)gBuf.drawOval(shape.x-10,shape.y-10,20,20);else if (shape.type=Shape.SQUARE)gBuf.drawRect(shape.x-10,shape.y-10,20,20);g.drawImage(imgBuf, 0, 0, this);public void update(Graphics g) paint(g);public void mousePressed(MouseEvent e) Shape shape; if (e.getButton()=e.BUTTON1) shape=new Shape(Shape.CIRCLE,e.getPoint().x,e.getPoint().y); shapeList.add(shape); else if (e.getButton()=e.BUTTON3) shape=new Shape(Shape.SQUARE,e.getPoint().x,e.getPoint().y);shapeList.add(shape); repaint(); /* Empty method definition. */ public void mouseReleased(MouseEvent e) /* Empty method definition. */ public void mouseEntered(MouseEvent e) /* Empty method definition. */ public void mouseExited(MouseEvent e) public void mouseClicked(MouseEvent e) /Event listener implementation goes here. 3 在该程序中添加发送网络数据的代码。代码中所用到的Shape类请参照代码自行补充。 public void sendData(Shape shape) DatagramSocket ds;try ds = new DatagramSocket(); catch (SocketException e1) / TODO Auto-generated catch blocke1.printStackTrace();return;DatagramPacket packet;ByteArrayOutputStream bao = new ByteArrayOutputStream();DataOutputStream dos = new DataOutputStream(bao);try dos.writeByte(shape.type);dos.writeInt(shape.x);dos.writeInt(shape.y);byte b=bao.toByteArray();packet=new DatagramPacket(b,b.length,InetAddress.getByName(localhost),2007);ds.send(packet);dos.close();ds.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); 4 参照客户端程序实现服务器端程序。5 实现温度曲线客户端程序。其代码如下:package network;import java.io.*;import .*;import java.util.*;public class TempleratureClient public static void main(String args) int temper;DatagramSocket ds;DatagramPacket packet;try temper = 10;Random r=new Random();ds = new DatagramSocket();while (true) temper=(Math.abs(r.nextInt()%75-30);ByteArrayOutputStream bao = new ByteArrayOutputStream();DataOutputStream dos = new DataOutputStream(bao);dos.writeInt(temper);byteb=bao.toByteArray();packet=new DatagramPacket(b,b.length,InetAddress.getByName(localhost),55000);ds.send(packet);try Thread.sleep(1000); catch (InterruptedException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();6 实现温度曲线服务器端程序。下面是它的部分代码:package network;import java.applet.*;import java.awt.*;import java.awt.event.*;import java.io.*;import .*;public class TemperatureServer extends Applet static String str = |;static final int MAXLENGTH=24;static int temper = new intMAXLENGTH;private Graphics gBuf;private Image imgBuf;public static void main(String args) Frame app = new Frame(温度曲线显示程序);app.setSize(800, 600);app.setLocationByPlatform(true);TemperatureServer applet = new TemperatureServer();app.add(applet);app.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent event) event.getWindow().dispose();System.exit(0););app.show();applet.start();for (int i = 0; i MAXLENGTH; i+)temperi = 0;applet.repaint();/请补充完整public TemperatureServer() public void init() public void paint(Graphics g) final int leftMargin = 60;final int rightMargin = 100;final int topMargin = 40;final int bottomMargin = 40;final int maxY = 45;final int minY = -30;final int maxX = MAXLENGTH;final int minX = 0;final int valueScaleX = 1;final int valueScaleY = 5;final int countX = (maxX - minX) / valueScaleX;final int countY = (maxY - minY) / valueScaleY;int graphWidth = bounds().width;int graphHeight = bounds().height;imgBuf = createImage(graphWidth, graphHeight);gBuf = imgBuf.getGraphics();gBuf.clearRect(0, 0, graphWidth, graphHeight);gBuf.drawString(温度曲线图, (graphWidth - (topMargin + rightMargin) / 2,topMargin / 2);/ 绘制坐标系int scaleY = (graphHeight - topMargin - bottomMargin) / countY;int scaleX = (graphWidth - leftMargin - rightMargin) / countX;gBuf.setColor(Color.BLACK);gBuf.drawLine(leftMargin, graphHeight - bottomMargin - scaleY * countY,leftMargin, graphHeight - bottomMargin);gBuf.drawLine(leftMargin, graphHeight - bottomMargin, leftMargin+ scaleX * countX, graphHeight - bottomMargin);/ 绘制坐标系说明/ gBuf.setFont(new Font(黑体, Font.BOLD, 16);gBuf.drawString(温度(C), leftMargin / 2, topMargin / 2);gBuf.drawString(时间(t), graphWidth - rightMargin / 2, graphHeight- bottomMargin / 2);/ 绘制刻度for (int i = 0; i = countX; i+) gBuf.drawLine(leftMargin + i * scaleX, graphHeight - bottomMargin,leftMargin + i * scaleX, graphHeight - bottomMargin - 5);gBuf.drawString( + (minX + i * valueScaleX), leftMargin + i* scaleX, graphHeight - bottomMargin + bottomMargin / 2);for (int i = 0; i = countY; i+) gBuf.drawLine(leftMargin,(graphHeight - bottomMargin) - i * scaleY, leftMargin + 5,(graphHeight - bottomMar
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025智能解决方案技术服务合同样本
- 2025市场营销合作的合同范本
- 2025年签订商业店面购房合同应注意要点
- 2025年新能源车辆充电桩投资风险与收益分析报告
- 肺结核病人特点
- 企业间竞业禁止协议补偿金支付标准及监管
- 2025跨境电商服装采购销售合同
- 2025年签订无固定期限合同的条件分析
- 车辆抵押贷款业务客户需求分析与解决方案服务合同
- 2025建筑施工劳务双包协议
- 斜视检查(斜视诊疗课件)
- 和安风电场电气设备定检及预防性试验技术规范
- 农产品食品安全评价技术 课件全套 模块1-8 走进农产品食品安全检测 - 油脂脂肪酸组成和溶剂残留检测
- (正式版)HGT 22820-2024 化工安全仪表系统工程设计规范
- 第二章 临床康复工程学基础
- (高清版)TDT 1075-2023 光伏发电站工程项目用地控制指标
- 《水生生物学桡足类》课件
- 《预算员培训二》课件
- 八年级劳动课下册教案
- 人工动静脉瘘狭窄查房
- 加药装置技术规范书2014.1.16
评论
0/150
提交评论