JavaSE02Day04Top1.DMS网络原型——HelloWorld2.DMS网络原型——服务端多线程技术3.DMS网络原型——服务端线程池技术及客户端出错重传技术4.DMS网络原型——服务端缓冲队列1DMS网络原型——HelloWorld1.1问题使用Java的Socket实现客户端和服务器端通信。通信过程如下表所示:客户端与服务器端通信过程1.2方案新建类ServerDemo表示服务器端,新建类ClientDemo表示客户端。首先服务器端创建ServerSocket对象,设置服务的端口。1.publicServerDemo(){2.try{3.ss=newServerSocket(port);4.}catch(Exceptione){5.e.printStackTrace();6.thrownewRuntimeException(,e);7.}8.}服务器端建立Socket等待客户端连接,并建立相应Socket的输入输出流。1.Socketsocket=ss.accept();2.InputStreamis=socket.getInputStream();3.OutputStreamos=socket.getOutputStream();4.PrintWriterwriter=newPrintWriter(os);5.BufferedReaderreader=newBufferedReader(newInputStreamReader(is));客户端建立Socket,创建相应Socket的输入输出流,向服务器发送信息:“你好,服务器!”。1.socket=newSocket(localhost,8888);2.InputStreamis=socket.getInputStream();3.OutputStreamos=socket.getOutputStream();4.PrintWriterwriter=newPrintWriter(os);5.BufferedReaderreader=newBufferedReader(newInputStreamReader(is));6.//17.writer.println(你好,服务器!);8.writer.flush();服务器端接收客户端信息。1.//12.Stringline=reader.readLine();3.System.out.println(line);服务器端发送“你好,客户端!”。1.//22.writer.println(你好,客户端!);3.writer.flush();客户端接收信息。1.//22.Stringline=reader.readLine();3.System.out.println(line);客户端发送“再见,服务器!”。1.//32.writer.println(再见,服务器!);3.writer.flush();服务器端接收客户端信息。1.//32.line=reader.readLine();3.System.out.println(line);服务器发送“再见,客户端!”。1.//42.writer.println(再见,客户端!);3.writer.flush();客户端接收信息。1.//42.line=reader.readLine();3.System.out.println(line);1.3实现系统代码实现如下:ServerDemo类代码如下:1.publicclassServerDemo{2.privateServerSocketss;3.privateintport=8888;4.publicServerDemo(){5.try{6.ss=newServerSocket(port);7.}catch(Exceptione){8.e.printStackTrace();9.thrownewRuntimeException(,e);10.}11.}12.publicvoidstart(){13.try{14.//监听服务器端口,等待客户端的连接15.System.out.println(等待客户端连接);16.Socketsocket=ss.accept();17.InputStreamis=socket.getInputStream();18.OutputStreamos=socket.getOutputStream();19.PrintWriterwriter=newPrintWriter(os);20.BufferedReaderreader=newBufferedReader(21.newInputStreamReader(is));22.//123.Stringline=reader.readLine();24.System.out.println(line);25.//226.writer.println(你好,客户端!);27.writer.flush();28.//329.line=reader.readLine();30.System.out.println(line);31.//432.writer.println(再见,客户端!);33.writer.flush();34.socket.close();35.}catch(Exceptione){36.e.printStackTrace();37.}38.}39.publicstaticvoidmain(String[]args){40.System.out.println(ServerDemo...);41.ServerDemoserverDemo=newServerDemo();42.serverDemo.start();43.}44.}ClientDemo类代码如下:1.publicclassClientDemo{2.privateSocketsocket;3.publicvoidsend(){4.try{5.System.out.println(连接服务器...);6.socket=newSocket(localhost,8888);7.InputStreamis=socket.getInputStream();8.OutputStreamos=socket.getOutputStream();9.PrintWriterwriter=newPrintWriter(os);10.BufferedReaderreader=newBufferedReader(11.newInputStreamReader(is));12.//113.writer.println(你好,服务器!);14.writer.flush();15.//216.Stringline=reader.readLine();17.System.out.println(line);18.//319.writer.println(再见,服务器!);20.writer.flush();21.//422.line=reader.readLine();23.System.out.println(line);24.}catch(Exceptione){25.e.printStackTrace();26.thrownewRuntimeException(,e);27.}28.}29.publicstaticvoidmain(String[]args){30.System.out.println(ClientDemo...);31.ClientDemoclientDemo=newClientDemo();32.clientDemo.send();33.}34.}1.4扩展在DMS网络原型——HelloWorld中,已经完成了客户端和服务器端的通信,现需要继续进行通信,通信过程如下表所示。客户端与服务器端通信过程ServerDemo类代码如下:1.publicclassServerDemo{2.privateServerSocketss;3.privateintport=8888;4.publicServerDemo(){5.try{6.ss=newServerSocket(port);7.}catch(Exceptione){8.e.printStackTrace();9.thrownewRuntimeException(,e);10.}11.}12.publicvoidstart(){13.try{14.//监听服务器端口,等待客户端的连接15.System.out.println(等待客户端连接);16.Socketsocket=ss.accept();17.InputStreamis=socket.getInputStream();18.OutputStreamos=socket.getOutputStream();19.PrintWriterwriter=newPrintWriter(os);20.BufferedReaderreader=newBufferedReader(21.newInputStreamReader(is));22.//123.Stringline=reader.readLine();24.System.out.println(line);25.//226.writer.println(你好,客户端!);27.writer.flush();28.//329.line=reader.readLine();30.System.out.println(line);31.//432.writer.println(再见,客户端!);33.writer.flush();34./**35.*扩展部分36.*/37.StringmyName=reader.readLine();38.System.out.println(myName+来了!);39.writer.println(你好,+myName);40.writer.flush();41.socket.close();42.}catch(Exceptione){43.e.printStackTrace();44.}45.}46.publicstaticvoidmain(String[]args){47.System.out.println(ServerDemo...);48.ServerDemoserverDemo=newServerDemo();49.serverDemo.start();50.}51.}ClientDemo类完整代码如下:1.publicclassClientDemo{2.privateSocketsocket;3.publicvoidsend(){4.try{5.System.out.println(连接服务器...);6.socket=newSocket(localhost,8888);7.InputStreamis=socket.getInputStream();8.OutputStreamos=socket.getOutputStream();9.PrintWriterwriter=newPrintWriter(os);10.BufferedReaderreader=newBufferedReader(11.newInputStreamReader(is));12.//113.writer.println(你好,服务器!);14.writer.flush();15.//216.Stringline=reader.readLine();17.System.out.println(line);18.//319.writer.println(再见,服务器!);20.writer.flush();21.//422.line=reader.readLine();23.System.out.println(line);24./**25.*扩展部分26.*/27.StringmyNam