分布式系統(tǒng)實驗報告_第1頁
分布式系統(tǒng)實驗報告_第2頁
分布式系統(tǒng)實驗報告_第3頁
分布式系統(tǒng)實驗報告_第4頁
分布式系統(tǒng)實驗報告_第5頁
已閱讀5頁,還剩37頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

分布式系統(tǒng)試驗報告-標(biāo)準(zhǔn)化文件公布號:〔9456-EUATWK-MWUB-WUNN-INNUL-DDQTY-KII10中南大學(xué)分布式系統(tǒng)試驗報告名目ctrl點擊名目條可直達,wps下有效〕\l“_TOC_250025“試驗一數(shù)據(jù)包socket應(yīng)用 4\l“_TOC_250024“一、試驗?zāi)康?4\l“_TOC_250023“二、預(yù)習(xí)與試驗要求 4\l“_TOC_250022“三、試驗環(huán)境 4\l“_TOC_250021“四、試驗原理 5\l“_TOC_250020“五、試驗內(nèi)容 6\l“_TOC_250019“六、試驗報告 7\l“_TOC_250018“七、思考題 9\l“_TOC_250017“試驗二流式socket應(yīng)用 9\l“_TOC_250016“一、試驗?zāi)康?9\l“_TOC_250015“二、預(yù)習(xí)與試驗要求 9\l“_TOC_250014“三、試驗環(huán)境 9\l“_TOC_250013“四、試驗原理 10\l“_TOC_250012“五、試驗內(nèi)容 10\l“_TOC_250011“六、試驗報告 10\l“_TOC_250010“七、思考題 13\l“_TOC_250009“試驗三客戶/效勞器應(yīng)用開發(fā) 14\l“_TOC_250008“一、試驗?zāi)康?14\l“_TOC_250007“二、預(yù)習(xí)與試驗要求 14\l“_TOC_250006“三、試驗環(huán)境 14\l“_TOC_250005“四、試驗原理 14\l“_TOC_250004“五、試驗內(nèi)容 15\l“_TOC_250003“六、試驗報告 15試驗九虛擬機的使用與Linux系統(tǒng)的安裝 34\l“_TOC_250002“一、試驗?zāi)康?34\l“_TOC_250001“二、試驗內(nèi)容和步驟 34\l“_TOC_250000“三、試驗結(jié)果 36socket應(yīng)用一、試驗?zāi)康睦斫鈹?shù)據(jù)包socket的應(yīng)用實現(xiàn)數(shù)據(jù)包socket通信了解Java并行編程的根本方法二、預(yù)習(xí)與試驗要求預(yù)習(xí)試驗指導(dǎo)書及教材的有關(guān)內(nèi)容,了解數(shù)據(jù)包socket的通信原理;生疏一種javaIDE和程序開發(fā)過程;了解以下JavaAPI:Thread、Runnable;盡可能獨立思考并完成試驗。三、試驗環(huán)境獨立計算機或計算機網(wǎng)絡(luò);WindowsJdkJCreatororothers四、試驗原理分布式計算的核心是進程通信。如Winsock、.*。SocketAPIIPC制。盡管應(yīng)用人socketAPI1,高層設(shè)socketAPIsocketAPI;2,對于以響應(yīng)時間要求較高或運行于有限資源平臺上的應(yīng)用來說,socketAPI可能是最適合的。InternetUDPTCPUDP〔是否存在面對連接的數(shù)據(jù)包socket〕socketUDPIPC。JavasocketAPI針對socket的datagramSocket類針對數(shù)據(jù)包交換的datagramPacket類APIdatagramSocketsocektUDP程必需創(chuàng)立datagramsocket。在接收者進程中,datagramPacket上,該端DatagramPacket,并datagramSocketreceiveDatagramPacket為參數(shù)定義。并行編程〔以Java為例1〕Java程同時運類。這個子類Threadrun法創(chuàng)立一個線Runnable〔run〕。一個類的實例可以被安排并作為參數(shù)傳遞給創(chuàng)立的線程,并啟動線程。例如:創(chuàng)立一個類是Thread的子類:classSomeThreadextendsThread{SomeThread{}publicvoidrun{...}}SomeThreadp=newSomeThread;p.start;RunnableclassSomeRunimplementsRunnable{SomeRun{}publicvoidrun{...}}SomeRunp=newSomeRun(143);newThread(p).start;RunnableThread實例,并通過自身作為目標(biāo)線程。在大多數(shù)狀況下,假設(shè)你只打算重寫的run〔〕方法Runnable非程序員有意修改或增加類的根本行為。五、試驗內(nèi)容構(gòu)建客戶端程序〔1〕datagramSocket對象實例〔2〕DatagramPacket對象實例,并包含接收者主機地址、接收端口號等信息〔3〕datagramSocket對象實例的send方法,將DatagramPacket對數(shù)發(fā)送。構(gòu)建效勞器端程序構(gòu)建datagramSocket對象實例,指定接收的端口號。構(gòu)建DatagramPacket對象實例,用于重組接收到的消息。datagramSocketreceive將DatagramPacket六、試驗報告客戶端:importjava.io.IOException;import.DatagramPacket;import.DatagramSocket;import.InetAddress;importjava.util.Scanner;publicclassClient{publicstaticvoidmain(String[]args)throwsIOException{//DatagramSocket,使用隨機端口DatagramSocketsocketnewDatagramSocket;//初始化發(fā)送用的DatagramSocket,0組DatagramPacketoutPacket=newDatagramPacket(newbyte[0],0,InetAddress.getByName(““),30000);//byte[]inBuffnewbyte[4096];DatagramPacketinPacket=newDatagramPacket(inBuff,inBuff.length);//Scannerscan=newScanner(System.in);//while(scan.hasNextLine){

//byte[]buffscan.nextLine.getBytes;outPacket.setData(buff);//socket.send(outPacket);//讀取SocketinPacketsocket.receive(inPacket);System.out.println(newString(inBuff,0,inPacket.getLength));}}}效勞端:importjava.io.IOException;import.DatagramPacket;.DatagramSocket;publicclassServices{publicstaticvoidmain(String[]args)throwsIOException{//定義接收網(wǎng)絡(luò)數(shù)據(jù)的字節(jié)數(shù)組byte[]inBuffnewbyte[4096];//DatagramSocketDatagramSocketsocket=newDatagramSocket(30000);DatagramPacketDatagramPacketinPacket=newDatagramPacket(inBuff,inBuff.length);Stringmessage=“消息已經(jīng)收到!“;while(true){socket.receive(inPacket);//將接收到的內(nèi)容轉(zhuǎn)換成字符串后輸出System.out.println(newString(inBuff,0inPacket.getLength));//byte[]sendData=message.getBytes;//DatagramPacket//SocketAddressSocketAddressDatagramPacket}

//DatagramPacketDatagramPacketoutPacket=newDatagramPacket(sendData,sendData.length,inPacket.getSocketAddress);//發(fā)送數(shù)據(jù)socket.send(outPacket);}}試驗過程中的問題和解決途徑;、DatagramSocketJDKapi效勞端:1:2:七、思考題如何避開數(shù)據(jù)包喪失而造成的無限等待問題?承受者收包時查看序號即可。如何實現(xiàn)全雙工的數(shù)據(jù)包通信?答:利用端口套接字之間的通信功能。socket應(yīng)用一、試驗?zāi)康睦斫饬魇絪ocket的原理實現(xiàn)流式socket通信二、預(yù)習(xí)與試驗要求預(yù)習(xí)試驗指導(dǎo)書及教材的有關(guān)內(nèi)容,了解流式socket的通信原理;生疏java環(huán)境和程序開發(fā)過程;盡可能獨立思考并完成試驗。三、試驗環(huán)境獨立計算機;WindowsJdk四、試驗原理SocketAPIIPC該層編寫代socketAPI:1,高層設(shè)施是構(gòu)建于socketAPI之上的,即他們socketAPI;2,對于以響應(yīng)時間要求較高或運行于有限資源平臺上的應(yīng)用來說,socketAPI可能是最適合的。InternetUDPTCPUDPTCPIPCsocket。JavasocketAPIsocket〔1〕式用于連socket〔2〕式用于數(shù)據(jù)交換的數(shù)據(jù)socket。五、試驗內(nèi)容MystreamSocketjavaSocketsendMessage和receiveMessage方法構(gòu)建客戶端程序MyStreamsocket號調(diào)用該socket的receiveMessage方法讀取從效勞器端獲得的消息構(gòu)建效勞器端程序socketsocket偵聽客戶端的連接懇求創(chuàng)立一個MyStreamsocket的實例對象MyStreamsocketsendMessage。六、試驗報告應(yīng)用程序的構(gòu)造圖,說明程序之間的關(guān)系;程序的偽代碼。import.*;importjava.io.*;publicclassMystreamSocketextendsSocket{privateSocketsocket;privateBufferedReaderinput;privatePrintWriteroutput;//客戶端的構(gòu)造方法MystreamSocket(InetAddressacceptorHost,intacceptorPort)throwsSocketException,IOException{socket=newSocket(acceptorHost,acceptorPort);setStreams;}//效勞端的構(gòu)造方法MystreamSocket(Socketsocket) throwsIOException{this.socket=socket;setStreams;}設(shè)置輸出輸入流privatevoidsetStreamsthrowsIOException{InputStreaminStream=socket.getInputStream;input=newBufferedReader(newInputStreamReader(inStream));OutputStreamoutStream=socket.getOutputStream;output=newPrintWriter(newOutputStreamWriter(outStream));}//發(fā)送消息publicvoidsendMessage(Stringmessage)throwsIOException{output.println(message);output.flush;}//接收消息publicStringreceiveMessagethrowsIOException{Stringmessage=input.readLine;returnmessage;}//關(guān)閉全部翻開的東西publicvoidcloseAll{try{if(input!=null)input.close;if(output!=null)output.close;if(socket!=null)socket.close;}catch(IOExceptione){//TODOcatche.printStackTrace;}}}Client.java:importjava.io.IOException;import.InetAddress;publicclassClient{publicstaticvoidmain(Stringargs[]){try{InetAddresshostname=InetAddress.getByName(“l(fā)ocalhost“);MystreamSocketmss=newMystreamSocket(hostname,12345);我是客戶端,我懇求連接!“);System.out.println(mss.receiveMessage);mss.close;}catch(IOExceptione){//TODOcatche.printStackTrace;}}}效勞端Services.java:importjava.io.IOException;import.ServerSocket;import.Socket;publicclassServices{publicstaticvoidmain(Stringargs[]){try{ServerSocketservices=newServerSocket(12345);Socketsocket;while(true){socket=services.accept;//多線程處理連接懇求newServicesThread(socket).start;}}catch(IOExceptione){//TODOcatche.printStackTrace;}}}importjava.io.IOException;import.Socket;publicclassServicesThreadextendsThread{Socketsocket;ServicesThread(Socketsocket){this.socket=socket;}publicvoidrun{try{MystreamSocketmss=newMystreamSocket(socket);System.out.println(mss.receiveMessage);mss.sendMessage(“消息已收到!“);mss.closeAll;}catch(IOExceptione){//TODOcatche.printStackTrace;}}}七、思考題如何實現(xiàn)全雙工的流式socket通信?后利用其供給的功能進展通信。如何實現(xiàn)安全socketAPI?答:留意在通信過程中的各種特別狀況的捕獲與處理。1答:在效勞端使用多線程。/一、試驗?zāi)康尿炞Cdaytime和echo程序,實現(xiàn)包socket支撐的C/S模式IPC機制實現(xiàn)流式socket支撐的C/S模式IPC機制二、預(yù)習(xí)與試驗要求daytimeecho效勞內(nèi)容;復(fù)習(xí)包socket和流式socket的實現(xiàn)原理;試驗前認(rèn)真聽講,聽從安排。盡可能獨立思考并完成試驗。三、試驗環(huán)境獨立計算機;WindowsJdk四、試驗原理C/S指如daytime、telnet、ftpWWWC/S如何通過會話實現(xiàn)多個用戶的并發(fā)問題如何定義客戶和效勞器在效勞會話期間必需遵守的協(xié)議效勞定位問題進程間通信和大事同步問題:語法、語義和響應(yīng)數(shù)據(jù)表示問題在解決了這些問題的根底上,C/S范型必需遵從3層構(gòu)造的軟件體系構(gòu)造:表示層,供給與客戶端進展交互的界面應(yīng)用規(guī)律層,定義效勞器和客戶端要處理的主要事務(wù)的業(yè)務(wù)規(guī)律IPC里的receivesend方法等。五、試驗內(nèi)容構(gòu)建用數(shù)據(jù)包socket實現(xiàn)的daytime客戶端程序構(gòu)建表示層程序DaytimeClient1.java構(gòu)建應(yīng)用規(guī)律層程序DaytimeHelper1.java構(gòu)建效勞層程序MyClientDatagramSocket.java構(gòu)建用數(shù)據(jù)包socket實現(xiàn)的daytime效勞器端程序構(gòu)建表示層和應(yīng)用規(guī)律層程序DaytimeServer1.java構(gòu)建效勞層程序MyServerDatagramSocket.javaMyServerDatagramSocket.javaDatagramMessage.java〔它封裝了客戶端的消息和地址〕構(gòu)建用流式socket實現(xiàn)的daytime應(yīng)用程序包構(gòu)建用數(shù)據(jù)包socket實現(xiàn)的echo應(yīng)用程序包構(gòu)建用流式socket實現(xiàn)的echo應(yīng)用程序包六、試驗報告socketdaytime關(guān)系;客戶端:效勞端:代碼:DaytimeClient1.javaimportjava.io.*;publicclassDaytimeClient1{publicstaticvoidmain(String[]args){InputStreamReaderis=newInputStreamReader(System.in);BufferedReaderbr=newBufferedReader(is);try{client.\n“+

System.out.println(“theDaytime“Whatisthenameoftheserverhost“);StringhostName=br.readLine;if(hostName.length==0)hostName=“l(fā)ocalhost“; //使用默認(rèn)主機名System.out.println(“Whatistheportnumberoftheserverhost“);口

StringportNum=br.readLine;if(portNum.length==0)portNum=“1300“; //設(shè)置默認(rèn)端System.out.println(“Hereisthetimestampreceivedfromtheserver“.getTimestamp(hostName,portNum));}catch(Exceptionex){ex.printStackTrace;

+DaytimeClientHelper1}}}DaytimeClientHelper1.javaimport.*;publicclassDaytimeClientHelper1{publicstaticStringgetTimestamp(StringhostName,StringportNum){Stringtimestamp=““;try{InetAddressserverHost=InetAddress.getByName(hostName);intserverPort=Integer.parseInt(portNum);//socket發(fā)送和接收數(shù)據(jù)MyDatagramSocketmySocketnewMyDatagramSocket;““);戳mySocket.close;}

mySocket.sendMessage(serverHost,serverPort,timestampmySocket.receiveMessage;//catch(Exceptionex){System.out.println(“Thereisaproblem:“+ex);}returntimestamp;}}MyClientDatagramSocket.javaimport.*;importjava.io.*;publicclassMyClientDatagramSocketextendsDatagramSocket{staticfinalintMAX_LEN=100;MyClientDatagramSocketthrowsSocketException{super;}MyClientDatagramSocket(intportNo)throwsSocketException{super(portNo);}publicvoidsendMessage(InetAddressreceiverHost,intreceiverPort,Stringmessage)throwsIOException{byte[]sendBuffer=message.getBytes;DatagramPacketdatagram=newDatagramPacket(sendBuffer,sendBuffer.length,receiverHost,receiverPort);this.send(datagram);}publicStringreceiveMessagethrowsIOException{byte[]receiveBuffer=newbyte[MAX_LEN];DatagramPacketdatagram=newDatagramPacket(receiveBuffer,MAX_LEN);this.receive(datagram);Stringmessage=newString(receiveBuffer);returnmessage;}}效勞端:DaytimeServer1.javaimportjava.io.*;importjava.util.Date;publicclassDaytimeServer1{publicstaticvoidmain(String[]args){intserverPort=13;if(args.length==1)serverPort=Integer.parseInt(args[0]);try{MyServerDatagramSocketmySocket=newMyServerDatagramSocket(serverPort);System.out.println(“Daytimeserverready.“);while(true){DatagramMessagerequest=mySocket.receiveMessageAndSender;System.out.println(“Requestreceived“);Datetimestamp=newDate;System.out.println(“timestampsent:“+timestamp.toString);timestamp.toString);}

mySocket.sendMessage(request.getAddress,request.getPort,}catch(Exceptionex){System.out.println(“Thereisaproblem:“+ex);}}}MyServerDatagramSocket.javaimport.*;importjava.io.*;publicclassMyServerDatagramSocketextendsDatagramSocket{staticfinalintMAX_LEN=100;MyServerDatagramSocket(intportNo)throwsSocketException{super(portNo);}publicvoidsendMessage(InetAddressreceiverHost,intreceiverPort,Stringmessage)throwsIOException{byte[]sendBuffer=message.getBytes;DatagramPacketdatagram=newDatagramPacket(sendBuffer,sendBuffer.length,receiverHost,receiverPort);this.send(datagram);}publicStringreceiveMessagethrowsIOException{byte[]receiveBuffer=newbyte[MAX_LEN];DatagramPacketdatagram=newDatagramPacket(receiveBuffer,MAX_LEN);this.receive(datagram);Stringmessage=newString(receiveBuffer);returnmessage;}publicDatagramMessagereceiveMessageAndSenderthrowsIOException{byte[]receiveBuffer=newbyte[MAX_LEN];DatagramPacketdatagram=newDatagramPacket(receiveBuffer,MAX_LEN);this.receive(datagram);DatagramMessagereturnVal=newDatagramMessage;returnVal.putVal(newString(receiveBuffer),datagram.getAddress,));}}

returnreturnVal;

datagram.getPort(DatagramMessage.javaimport.*;publicclassDatagramMessage{privateStringmessage;privateInetAddresssenderAddress;privateintsenderPort;publicvoidputVal(Stringmessage,InetAddressaddr,intport){this.message=message;this.senderAddress=addr;this.senderPort=port;}publicStringgetMessage{returnthis.message;}publicInetAddressgetAddress{returnthis.senderAddress;}publicintgetPort{returnthis.senderPort;}}socketdaytime系;客戶端:效勞端:共有的:MyStreamSocket.javaimport.*;importjava.io.*;publicclassMyStreamSocketextendsSocket{privateSocketsocket;privateBufferedReaderinput;privatePrintWriteroutput;MyStreamSocket(InetAddressacceptorHost,intacceptorPort)throwsSocketException,IOException{socket=newSocket(acceptorHost,acceptorPort);setStreams;}MyStreamSocket(Socketsocket) throwsIOException{this.socket=socket;setStreams;}privatevoidsetStreamsthrowsIOException{InputStreaminStream=socket.getInputStream;input=newBufferedReader(newInputStreamReader(inStream));OutputStreamoutStream=socket.getOutputStream;output=newPrintWriter(newOutputStreamWriter(outStream));}publicvoidsendMessage(Stringmessage)throwsIOException{output.println(message);output.flush;}publicStringreceiveMessagethrowsIOException{Stringmessage=input.readLine;returnmessage;}}客戶端:DaytimeClient2.javaimportjava.io.*;publicclassDaytimeClient2{publicstaticvoidmain(String[]args){InputStreamReaderis=newInputStreamReader(System.in);BufferedReaderbr=newBufferedReader(is);try{client.\n“+

System.out.println(“theDaytime“Whatisthenameoftheserverhost“);StringhostName=br.readLine;if(hostName.length==0)hostName=“l(fā)ocalhost“;System.out.println(“Whatistheportnumberoftheserverhost“);fromtheserver“portNum));}

StringportNum=br.readLine;if(portNum.length==0)portNum=“13“;System.out.println(“Hereisthetimestampreceived+DaytimeClientHelper2.getTimestamp(hostName,catch(Exceptionex){ex.printStackTrace;}}}DaytimeClientHelper2.javaimport.*;publicclassDaytimeClientHelper2{publicstaticStringgetTimestamp(StringhostName,StringportNum)throwsException{Stringtimestamp=““;InetAddressserverHost=InetAddress.getByName(hostName);intserverPort=Integer.parseInt(portNum);System.out.println(“Connectionrequestmade“);MyStreamSocketmySocket=newMyStreamSocket(serverHost,serverPort);timestamp=mySocket.receiveMessage;mySocket.close;returntimestamp;}}效勞端:DaytimeServer2.javaimportjava.io.*;import.*;importjava.util.Date;publicclassDaytimeServer2{publicstaticvoidmain(String[]args){intserverPort=13;if(args.length==1)serverPort=Integer.parseInt(args[0]);try{ServerSocketmyConnectionSocket=newServerSocket(serverPort);System.out.println(“Daytimeserverready.“);while(true){System.out.println(“Waitingforaconnection.“);MyStreamSocketmyDataSocket=newMyStreamSocket(myConnectionSocket.accept);System.out.println(“Aclienthasmadeconnection.“);Datetimestamp=newDate;System.out.println(“timestampsent:“+timestamp.toString);myDataSocket.sendMessage(timestamp.toString);myDataSocket.close;}}catch(Exceptionex){ex.printStackTrace;}}}socketecho系;客戶端:EchoClient1.javaimportjava.io.*;publicclassEchoClient1{staticfinalStringendMessage=“.“;publicstaticvoidmain(String[]args){InputStreamReaderis=newInputStreamReader(System.in);BufferedReaderbr=newBufferedReader(is);try{System.out.println(“theEchoclient.\n“+“Whatisthenameoftheserverhost“);StringhostName=br.readLine;if(hostName.length==0)hostName=“l(fā)ocalhost“;System.out.println(“Whatistheportnumberoftheserverhost“);

StringportNum=br.readLine;if(portNum.length==0)portNum=“7777“;EchoClientHelper1helper=newEchoClientHelper1(hostName,portNum);booleandone=false;Stringmessage,echo;while(!done){System.out.println(“Enteralinetoreceiveanechobackfromtheserver,“+“orasingleperoidtoquit.“);message=br.readLine;if((message.trim).equals(endMessage)){done=true;helper.done;}else{echo=helper.getEcho(message);System.out.println(echo);}}}catch(Exceptionex){ex.printStackTrace;}}}EchoClientHelper1.javaimport.*;importjava.io.*;publicclassEchoClientHelper1{privateMyClientDatagramSocketmySocket;privateInetAddressserverHost;privateintserverPort;EchoClientHelper1(StringhostName,StringportNum)throwsSocketException,UnknownHostException{this.serverHost=InetAddress.getByName(hostName);this.serverPort=Integer.parseInt(portNum);this.mySocket=newMyClientDatagramSocket;}publicStringgetEcho(Stringmessage)throwsSocketException,IOException{Stringecho=““;mySocket.sendMessage(serverHost,serverPort,message);echo=mySocket.receiveMessage;returnecho;}publicvoiddonethrowsSocketException{mySocket.close;}}MyClientDatagramSocket.javaimport.*;importjava.io.*;publicclassMyClientDatagramSocketextendsDatagramSocket{staticfinalintMAX_LEN=100;MyClientDatagramSocketthrowsSocketException{super;}MyClientDatagramSocket(intportNo)throwsSocketException{super(portNo);}publicvoidsendMessage(InetAddressreceiverHost,intreceiverPort,Stringmessage)throwsIOException{byte[]sendBuffer=message.getBytes;DatagramPacketdatagram=newDatagramPacket(sendBuffer,sendBuffer.length,receiverHost,receiverPort);this.send(datagram);}publicStringreceiveMessagethrowsIOException{byte[]receiveBuffer=newbyte[MAX_LEN];DatagramPacketdatagram=newDatagramPacket(receiveBuffer,MAX_LEN);this.receive(datagram);Stringmessage=newString(receiveBuffer);returnmessage;}}效勞端:EchoServer1.javaimportjava.io.*;publicclassEchoServer1{publicstaticvoidmain(String[]args){intserverPort=1117;if(args.length==1)serverPort=Integer.parseInt(args[0]);try{MyServerDatagramSocketmySocket=newMyServerDatagramSocket(serverPort);System.out.println(“Echoserverready.“);while(true){DatagramMessagerequest=mySocket.receiveMessageAndSender;System.out.println(“Requestreceived“);Stringmessage=request.getMessage;System.out.println(“messagereceived:“+message);}}

mySocket.sendMessage(request.getAddress,request.getPort,message);catch(Exceptionex){ex.printStackTrace;}}}MyServerDatagramSocket.javaimport.*;importjava.io.*;publicclassMyServerDatagramSocketextendsDatagramSocket{staticfinalintMAX_LEN=100;MyServerDatagramSocket(intportNo)throwsSocketException{super(portNo);}publicvoidsendMessage(InetAddressreceiverHost,intreceiverPort,Stringmessage)throwsIOException{byte[]sendBuffer=message.getBytes;DatagramPacketdatagram=newDatagramPacket(sendBuffer,sendBuffer.length,receiverHost,receiverPort);this.send(datagram);}publicStringreceiveMessagethrowsIOException{byte[]receiveBuffer=newbyte[MAX_LEN];DatagramPacketdatagram=newDatagramPacket(receiveBuffer,MAX_LEN);this.receive(datagram);Stringmessage=newString(receiveBuffer);returnmessage;}publicDatagramMessagereceiveMessageAndSenderthrowsIOException{byte[]receiveBuffer=newbyte[MAX_LEN];DatagramPacketdatagram=newDatagramPacket(receiveBuffer,MAX_LEN);this.receive(datagram);DatagramMessagereturnVal=newDatagramMessage;returnVal.putVal(newString(receiveBuffer),datagram.getAddress,));}}

returnreturnVal;

datagram.getPort(DatagramMessage.javaimport.*;publicclassDatagramMessage{privateStringmessage;privateInetAddresssenderAddress;privateintsenderPort;publicvoidputVal(Stringmessage,InetAddressaddr,intport){this.message=message;this.senderAddress=addr;this.senderPort=port;}publicStringgetMessage{returnthis.message;}publicInetAddressgetAddress{returnthis.senderAddress;}publicintgetPort{returnthis.senderPort;}}用流式socket實現(xiàn)的echo應(yīng)用程序包的構(gòu)架,列明各程序之間的關(guān)系。共有的:MyStreamSocket.javaimport.*;importjava.io.*;publicclassMyStreamSocketextendsSocket{privateSocketsocket;privateBufferedReaderinput;privatePrintWriteroutput;MyStreamSocket(InetAddressacceptorHost,intacceptorPort)throwsSocketException,IOException{socket=newSocket(acceptorHost,acceptorPort);setStreams;}MyStreamSocket(Socketsocket) throwsIOException{this.socket=socket;setStreams;}privatevoidsetStreamsthrowsIOException{InputStreaminStream=socket.getInputStream;input=newBufferedReader(newInputStreamReader(inStream));OutputStreamoutStream=socket.getOutputStream;output=newPrintWriter(newOutputStreamWriter(outStream));}publicvoidsendMessage(Stringmessage)throwsIOException{output.println(message);output.flush;}publicStringreceiveMessagethrowsIOException{Stringmessage=input.readLine;returnmessage;}}客戶端:EchoClient2.javaimportjava.io.*;publicclassEchoClient2{staticf?inalStringendMessage=“.“;publicstaticvoidmain(String[]args){InputStreamReaderis=newInputStreamReader(System.in);BufferedReaderbr=newBufferedReader(is);try{+serverhost“);

System.out.println(“theEchoclient.\n““Whatisthenameoftheserverhost“);StringhostName=br.readLine;if(hostName.length==0)hostName=“l(fā)ocalhost“;System.out.println(“WhatistheportnumberoftheStringportNum=br.readLine;if(portNum.length==0)portNum=“7“;EchoClientHelper2helper=newEchoClientHelper2(hostName,portNum);booleandone=false;Stringmessage,echo;while(!done){System.out.println(“Enteralinetoreceiveanecho“toquit.“);

+“fromtheserver,orasingleperiodmessage=br.readLine;if((message.trim).equals(endMessage)){done=true;helper.done;}else{echo=helper.getEcho(message);System.out.println(echo);}}}catch(Exceptionex){ex.printStackTrace;}}}EchoClientH

溫馨提示

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

最新文檔

評論

0/150

提交評論