![實(shí)驗(yàn)4:基于UDP的Sockets編程(解答)_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-4/17/65b9afbd-fda1-44f8-8b4f-2624adf33eb1/65b9afbd-fda1-44f8-8b4f-2624adf33eb11.gif)
![實(shí)驗(yàn)4:基于UDP的Sockets編程(解答)_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-4/17/65b9afbd-fda1-44f8-8b4f-2624adf33eb1/65b9afbd-fda1-44f8-8b4f-2624adf33eb12.gif)
![實(shí)驗(yàn)4:基于UDP的Sockets編程(解答)_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-4/17/65b9afbd-fda1-44f8-8b4f-2624adf33eb1/65b9afbd-fda1-44f8-8b4f-2624adf33eb13.gif)
![實(shí)驗(yàn)4:基于UDP的Sockets編程(解答)_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-4/17/65b9afbd-fda1-44f8-8b4f-2624adf33eb1/65b9afbd-fda1-44f8-8b4f-2624adf33eb14.gif)
![實(shí)驗(yàn)4:基于UDP的Sockets編程(解答)_第5頁](http://file2.renrendoc.com/fileroot_temp3/2021-4/17/65b9afbd-fda1-44f8-8b4f-2624adf33eb1/65b9afbd-fda1-44f8-8b4f-2624adf33eb15.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、實(shí)驗(yàn)四 基于UDP的Sockets編程一、實(shí)驗(yàn)?zāi)康? 熟悉UDP編程的基本過程2. 理解UDP協(xié)議相關(guān)的基本概念3. 掌握UDP協(xié)議與線程的結(jié)合方式二、實(shí)驗(yàn)內(nèi)容1. 利用UDP協(xié)議實(shí)現(xiàn)一個(gè)簡單的白板程序,要求如下:用戶在客戶端界面上單擊鼠標(biāo)左鍵后程序在鼠標(biāo)單擊位置畫20*20的圓形,單擊右鍵后程序在鼠標(biāo)單擊位置畫20*20的圓。服務(wù)器界面上將同時(shí)顯示與客戶端一致的畫面。2. 采用UDP編寫一個(gè)網(wǎng)絡(luò)程序,該程序的客戶端每隔1秒向服務(wù)端發(fā)送一個(gè)溫度值,服務(wù)端能夠接收該溫度值并顯示一條動(dòng)態(tài)溫度曲線。其界面如下:3. 如果要求每增加一個(gè)客戶端則增加一條動(dòng)態(tài)曲線,并且顯示一條所有客戶端的平均溫度曲線,該
2、程序應(yīng)該如何改變?三、實(shí)驗(yàn)步驟1. 針對第1題,定義數(shù)據(jù)傳遞的格式為:對象類型(圓、正方形等等)X坐標(biāo)Y坐標(biāo)1字節(jié)2字節(jié)2字節(jié)2.實(shí)現(xiàn)客戶端繪制程序,其代碼如下:package lab .1 ab5;import java.applet.*;import java.awt.*;import java.awt.eve nt.*;import java.io.*;import java .n et.*;import java.util.*;public class BoardClie nt exte nds Applet impleme nts MouseListe ner private Gra
3、phics 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.addWin
4、dowListener(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, gr
5、aphHeight); 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
6、-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 Sh
7、ape(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 m
8、ouseClicked(MouseEvent e) /Event listener implementation goes here.3在該程序中添加發(fā)送網(wǎng)絡(luò)數(shù)據(jù)的代碼。代碼中所用到的 Shape 類請參照代碼自行補(bǔ)充。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);sendData(shape);else if (e.g
9、etButton()=e.BUTTON3)shape=new Shape(Shape.SQUARE,e.getPoint().x,e.getPoint().y); shapeList.add(shape);sendData(shape); repaint();public class Shape public final static int CIRCLE=1; public final static int SQUARE=2; public int x; public int y;public int type;public Shape(int type,int x,int y) this.
10、x=x; this.y=y; this.type=type; 4參照客戶端程序?qū)崿F(xiàn)服務(wù)器端程序。package lab.lab5;import java.applet.*;import java.awt.*;import java.awt.event.*;import java.io.*;import .*;import java.util.*;public class BoardServer extends Applet private Graphics gBuf; private Image imgBuf;private ArrayList shapeList=new Ar
11、rayList(); public static void main(String args) / TODO Auto-generated method stub Frame app = new Frame( 白板服務(wù)器端 ); app.setSize(400, 400); app.setLocationByPlatform(true);BoardServer applet = new BoardServer();/ applet.ad app.add(applet); app.addWindowListener(new WindowAdapter() public void windowCl
12、osing(WindowEvent event) event.getWindow().dispose();System.exit(0););app.show();applet.start();try DatagramPacket packet = new DatagramPacket(new byte256, 256);DatagramSocket ds = new DatagramSocket(2007);Shape shape;while (true) ds.receive(packet);byte data = packet.getData();DataInputStream di =
13、new DataInputStream( new ByteArrayInputStream(data);shape=new Shape(di.readByte(),di.readInt(),di.readInt(); applet.shapeList.add(shape);/System.out.println(data:+shape.type); applet.repaint(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace();public void init() public void
14、 paint(Graphics g) /final double PI=3.1415926;/int x0,y0,x1,y1,x2,y2,x,y,clock,t;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()
15、;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);5實(shí)現(xiàn)溫度曲線客戶端程序。其代碼如下:packa
16、ge 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);By
17、teArrayOutputStream 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
18、.printStackTrace(); catch (IOException e) e.printStackTrace();6實(shí)現(xiàn)溫度曲線服務(wù)器端程序。下面是它的代碼: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;stat
19、ic 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 Wind
20、owAdapter() 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();try DatagramPacket packet = new DatagramPacket(new byte256, 256);DatagramSocket ds = new DatagramSocket(55000
21、);while (true) ds.receive(packet); /System.out.println(hello.); byte data = packet.getData(); /System.out.println(bytes:+packet.getLength(); /for (int i=0;idata.length;i+)/System.out.print(datai); DataInputStream di = new DataInputStream( new ByteArrayInputStream(data);int t = di.readInt();/ Socket
22、s=ss.accept();/ temper0for (int i = 1; i MAXLENGTH; i+) temperi - 1 = temperi; /System.out.println(temperi); temperMAXLENGTH-1 = t; applet.repaint(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); public TemperatureServer() public void init() public void paint(Graphics
23、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) / valueSca
24、leX;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 + rightMargi
25、n) / 2, topMargin / 2);/ 繪制坐標(biāo)系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.
26、drawLine(leftMargin, graphHeight - bottomMargin, leftMargin+ scaleX * countX, graphHeight - bottomMargin);/ 繪制坐標(biāo)系說明/ gBuf.setFont(new Font( 黑體, Font.BOLD, 16); gBuf.drawString( 溫度( C) , leftMargin / 2, topMargin / 2); gBuf.drawString( 時(shí)間( t) , graphWidth - rightMargin / 2, graphHeight- bottomMargin
27、/ 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
28、 = countY; i+) gBuf.drawLine(leftMargin,(graphHeight - bottomMargin) - i * scaleY , leftMargin + 5, (graphHeight - bottomMargin) - i * scaleY);gBuf.drawString( + (i * valueScaleY + minY), leftMargin / 2,(graphHeight - bottomMargin) - i * scaleY);/ 繪制動(dòng)態(tài)折線for (int i = 1; i MAXLENGTH; i+) gBuf.setColor
29、(Color.RED);gBuf.drawL in e(leftMarg in + i * scaleX, (graphHeight - bottomMarg in)-(temperi - 1 - minY) * scaleY / 5, leftMargin + (i + 1)* scaleX, (graphHeight - bottomMargin) - (temperi - minY)* scaleY / 5);gBuf.setColor(Color.BLACK);gBuf.drawString(temperi-1+,leftMargin+ i * scaleX, (graphHeight
30、 -bottomMargi n)-(temperi - 1 - minY) * scaleY / 5-5);g.drawlmage(imgBuf, 0, 0, this);public void update(Graphics g) pai nt(g);7.修改溫度曲線客戶端與服務(wù)器端程序,實(shí)現(xiàn)第3題。其數(shù)據(jù)格式可采用以下形式??蛻舳司幪?hào)溫度值1字節(jié)1字節(jié)四、調(diào)試分析在溫度曲線程序的調(diào)試過程中,由于服務(wù)器程序的端口與客戶端的端口不一致,導(dǎo)致 服務(wù)器程序無法響應(yīng),改正端口號(hào)后程序工作正常。五、實(shí)驗(yàn)結(jié)果通過上機(jī),編寫了兩個(gè)工程,得到完成了題目所要求的實(shí)驗(yàn)結(jié)果。第3題的修改后的代碼為:服務(wù)端:pac
31、kage lab .l ab5;import java.applet.*;import java.awt.*;import java.awt.eve nt.*;import java.io.*;import java .n et.*;public class TemperatureServer exte nds Applet static String str = |;static final int MAXLENGTH=24;static in t temper = new in t3MAXLENGTH; private Graphics gBuf;private Image imgBuf;
32、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().
33、dispose(); System.exit(0); ); app.show(); applet.start(); for (int i = 0; i MAXLENGTH; i+) temper0i = 0; temper1i = 0; temper2i = 0; applet.repaint();try DatagramPacket packet = new DatagramPacket(new byte256, 256); DatagramSocket ds = new DatagramSocket(55000);while (true) ds.receive(packet); byte
34、data = packet.getData(); DataInputStream di = new DataInputStream( new ByteArrayInputStream(data);int id=di.readInt();int t = di.readInt();System.out.println(id=+id);for (int i = 1; i MAXLENGTH; i+) temperidi - 1 = temperidi; temperidMAXLENGTH-1 = t; temper2MAXLENGTH-1=(t+temper1MAXLENGTH-1)/2; appl
35、et.repaint(); catch (IOException e) e.printStackTrace();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 = MAX
36、LENGTH;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.
37、getGraphics();gBuf.clearRect(0, 0, graphWidth, graphHeight);gBuf.drawString( 溫度曲線圖 , (graphWidth - (topMargin + rightMargin) / 2, topMargin / 2);/ 繪制坐標(biāo)系int scaleY = (graphHeight - topMargin - bottomMargin) / countY;int scaleX = (graphWidth - leftMargin - rightMargin) / countX;gBuf.setColor(Color.BLA
38、CK);gBuf.drawLine(leftMargin, graphHeight - bottomMargin - scaleY * countY , leftMargin, graphHeight - bottomMargin);gBuf.drawLine(leftMargin, graphHeight - bottomMargin, leftMargin+ scaleX * countX, graphHeight - bottomMargin);/ 繪制坐標(biāo)系說明/ gBuf.setFont(new Font( 黑體, Font.BOLD, 16); gBuf.drawString( 溫
39、度( C) , leftMargin / 2, topMargin / 2);gBuf.drawString( 時(shí)間( 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.drawSt
40、ring( + (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 - bottomMargin) - i * scaleY); gBuf.drawString( + (i * valueScaleY + minY), leftMargin / 2,(graphHeight - bottomMargin) - i * scaleY);/ 繪制動(dòng)態(tài)折線 for(int id=0;id3;id+) for (int i = 1; i MAXLENGTH; i+) gBuf.setColor(Color.RED); gBu
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度國內(nèi)體育器材采購及租賃服務(wù)合同
- 2025年度辦公樓室內(nèi)外一體化裝修工程合同
- 農(nóng)田果園轉(zhuǎn)租合同范例
- 農(nóng)場注入資金合同范本
- 農(nóng)田修路流轉(zhuǎn)合同范例
- 出國勞務(wù)押金合同范本
- 建筑工程管理中供應(yīng)鏈管理的關(guān)鍵問題探討
- 供苗草坪合同范本
- 委托平面設(shè)計(jì)合同范本
- 五金加工合同范本
- 2025屆高考數(shù)學(xué)一輪專題重組卷第一部分專題十四立體幾何綜合文含解析
- 福建省泉州市南安市2024-2025學(xué)年九年級上學(xué)期期末考試語文試題(無答案)
- 2025年中國電子煙行業(yè)發(fā)展前景與投資戰(zhàn)略規(guī)劃分析報(bào)告
- 醫(yī)療器材申請物價(jià)流程
- 人教PEP版2025年春季小學(xué)英語三年級下冊教學(xué)計(jì)劃
- 2024年世界職業(yè)院校技能大賽高職組“市政管線(道)數(shù)字化施工組”賽項(xiàng)考試題庫
- 華為研發(fā)部門績效考核制度及方案
- CSC資助出國博士聯(lián)合培養(yǎng)研修計(jì)劃英文-research-plan
- 2025年蛇年年度營銷日歷營銷建議【2025營銷日歷】
- 攝影入門課程-攝影基礎(chǔ)與技巧全面解析
- 司法考試2024年知識(shí)點(diǎn)背誦版-民法
評論
0/150
提交評論