分布計(jì)算 實(shí)驗(yàn)1(共8頁)_第1頁
分布計(jì)算 實(shí)驗(yàn)1(共8頁)_第2頁
分布計(jì)算 實(shí)驗(yàn)1(共8頁)_第3頁
分布計(jì)算 實(shí)驗(yàn)1(共8頁)_第4頁
分布計(jì)算 實(shí)驗(yàn)1(共8頁)_第5頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、福州大學(xué)數(shù)學(xué)(shxu)與計(jì)算機(jī)科學(xué)學(xué)院上機(jī)實(shí)驗(yàn)報(bào)告專業(yè)和班級(jí)信息數(shù)學(xué)02班姓名詹小青成績(jī)課程名稱分布計(jì)算實(shí)驗(yàn)名稱使用數(shù)據(jù)報(bào)和流式Socket實(shí)現(xiàn)消息傳遞實(shí)驗(yàn)?zāi)康暮鸵?.實(shí)驗(yàn)?zāi)康?(1)學(xué)習(xí)使用DatagramSocket和DatagramSocket實(shí)現(xiàn)消息傳遞 (2)通過使用ServerSocket為服務(wù)器端,Socket為客戶端實(shí)現(xiàn)基于C/S范型的數(shù)據(jù)通信。 2.實(shí)驗(yàn)基本要求 (1)使用DatagramSocket和DatagramSocket實(shí)現(xiàn)消息傳遞 。 (2)使用ServerSocket和Socket實(shí)現(xiàn)消息傳遞 。 實(shí)驗(yàn)內(nèi)容和步驟1.實(shí)驗(yàn)內(nèi)容:(1)寫一段出現(xiàn)在某main方法

2、中的java程序片段,用于打開一個(gè)最多接收100個(gè)字節(jié)數(shù)據(jù)的數(shù)據(jù)包socket,設(shè)置超時(shí)周期為50秒。如果發(fā)生超時(shí),須在屏幕上顯示“接收超時(shí)”消息;(2)考慮一個(gè)簡(jiǎn)單應(yīng)用,服務(wù)器端的功能是計(jì)算圓的面積??蛻舳藢A的半徑發(fā)送給服務(wù)器端,服務(wù)器端計(jì)算得出的圓面積將發(fā)送給客戶端,并在客戶端顯示;(3)使用多線程實(shí)現(xiàn)任務(wù)(2)。2.實(shí)驗(yàn)基本步驟:(1)程序:Server端:import .*;import java.util.*;public class serverpublic static void main(String args) throws ExceptionDatagramSocket

3、ds = new DatagramSocket();InetAddress addr = InetAddress.getByName(localhost);byte buff = new byte100;DatagramPacket dp;String s;for (;)Thread.sleep(5000);System.out.println(sending.);s = (new Date().toString();buff = s.getBytes();dp = new DatagramPacket(buff, buff.length, addr, 1313);ds.send(dp);Ci

4、ent端:import java.io.IOException;import .*;public class clientpublic static void main(String args) throws ExceptionDatagramSocket ds = new DatagramSocket(1313);byte buff = new byte100;DatagramPacket dp = new DatagramPacket(buff, buff.length);String s;ds.setSoTimeout(5000);for (;)tryds.receive(dp); ca

5、tch (SocketTimeoutException e)System.out.println(接收超時(shí));break;s = new String(dp.getData();System.out.println(Time received from + dp.getAddress()+ n Time is: + s);實(shí)驗(yàn)結(jié)果截圖:先運(yùn)行客戶端,在運(yùn)行服務(wù)器,直接超時(shí)先運(yùn)行服務(wù)器,在運(yùn)行客戶端程序:客戶端:import .*;import java.io.*;public class Client public static void main(String args) throws Ex

6、ception Socket sc=new Socket(127.0.0.1,4007); BufferedReader in=new BufferedReader(new InputStreamReader(System.in); BufferedReader sin=new BufferedReader(new InputStreamReader(sc.getInputStream(); PrintWriter ps=new PrintWriter(new OutputStreamWriter(sc.getOutputStream(); System.out.println(請(qǐng)輸入圓的半徑

7、:); String s=; while (true) s=in.readLine(); if (s.equals(88) break; ps.println(s); ps.flush(); s=sin.readLine(); System.out.println(圓的面積為: +s+n請(qǐng)輸入圓的半徑:); 服務(wù)器端:import java.io.*;import .*;public class Server public static void main(String args) throws Exception ServerSocket ss=new ServerSocket(4007);

8、 Socket sc=ss.accept(); BufferedReader in=new BufferedReader(new InputStreamReader(System.in); BufferedReader sin=new BufferedReader(new InputStreamReader(sc.getInputStream(); PrintWriter ps=new PrintWriter(new OutputStreamWriter(sc.getOutputStream(); String s=; while (true) s=sin.readLine(); double

9、 r=0; r=Integer.parseInt(s); r=3.14*r*r; /System.out.println(answer: +r); /System.out.println(please input one line:); /s=in.readLine(); if (s.equals(88) break; ps.println(r); ps.flush(); 實(shí)驗(yàn)結(jié)果截圖:程序:Server端:import java.io.*;import .*;public class serverpublic static void main(String args) throws Exce

10、ptionServerSocket ss = null;Socket socket = null;ss = new ServerSocket(8888);int clientnum =for(;)new ServerThread(ss.accept(), clientnum).start();clientnum+;class ServerThread extends ThreadSocket socket = null;int clientnum;public ServerThread(Socket socket, int num)this.socket = socket;clientnum

11、= num + 1;public void run()tryInputStream Is = socket.getInputStream();OutputStream Os = socket.getOutputStream();DataInputStream DIS = new DataInputStream(Is);PrintStream PS = new PrintStream(Os);for(;)String recvStr = DIS.readLine();double r = Double.valueOf(recvStr);double res = r * r * Math.PI;S

12、ystem.out.println(res);PS.println(r= + r + and area= + res);catch (Exception e)Cient端:import java.io.*;import .*;import java.util.Scanner;public class client public static void main(String args) throws Exception Socket socket= new Socket(127.0.0.1, 8888);InputStream Is = socket.getInputStream();Outp

13、utStream Os = socket.getOutputStream();DataInputStream DIS = new DataInputStream(Is);PrintStream PS = new PrintStream(Os);for(;)Scanner in=new Scanner(System.in);double r=in.nextDouble();PS.println(r);System.out.println(DIS.readLine();實(shí)驗(yàn)結(jié)果截圖:研究與探討第一個(gè)任務(wù)中要寫一段程序打開一個(gè)最多接收100個(gè)字節(jié)數(shù)據(jù)的數(shù)據(jù)包socket且超時(shí)周期為50秒,所以byte的最大值要設(shè)為100,為了設(shè)置超時(shí)時(shí)間要調(diào)用setSoTimeout函數(shù)。另外,要使接收端成功接收到發(fā)送端發(fā)送的信息,

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論