




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验六 基于协议的网络编程 实验六 基于协议的网络编程 实验目的 1 理解网络协议编程的基本原理 2 学会基于协议编程的设计思路和技术 3 掌握基于协议编程的具体应用 实验要求 1 建立一个的基于协议编程的 JAVA 工程 2 建立协议编程的 JAVA 类 并能正确运行且能实现协议通信 3 对 JAVA 类的功能进行拓广 使基于协议编程用于某一具体的应用 实验内容 1 建立工程和 JAVA 类 类程序的原代码为 import java io import public class ClientMainTest public static void main String args throws IOException Socket kkSocket null PrintWriter out null BufferedReader in null try kkSocket new Socket ss 5555 out new PrintWriter kkSocket getOutputStream true in new BufferedReader new InputStreamReader kkSocket getInputStream catch UnknownHostException e System err println Don t know about host ss System exit 1 catch IOException e System err println Couldn t get I O for the connection System exit 1 BufferedReader stdIn new BufferedReader new InputStreamReader System in String fromServer String fromUser while fromServer in readLine null System out println Server fromServer if fromServer equals Bye break fromUser stdIn readLine if fromUser null System out println Client fromUser out println fromUser out close in close stdIn close kkSocket close import java io import public class MultiServerTest public static void main String args throws IOException ServerSocket serverSocket null boolean listening true try serverSocket new ServerSocket 5555 catch IOException e System err println Could not listen on port 5555 System exit 1 while listening new MultiServerThreadTest serverSocket accept start serverSocket close import java io import public class MultiServerThreadTest extends Thread private Socket socket null public MultiServerThreadTest Socket socket super MultiServerThreadTest this socket socket public void run try PrintWriter out new PrintWriter socket getOutputStream true BufferedReader in new BufferedReader new InputStreamReader socket getInputStream String inputLine outputLine ServerProtocolTest kkp new ServerProtocolTest outputLine kkp processInput null out println outputLine while inputLine in readLine null outputLine kkp processInput inputLine out println outputLine if outputLine equals Bye break out close in close socket close catch IOException e e printStackTrace public class ServerProtocolTest private static final int WAITING 0 private static final int SENTKNOCKKNOCK 1 private static final int SENTCLUE 2 private static final int ANOTHER 3 private static final int NUMJOKES 5 private int state WAITING private int currentJoke 0 private String clues 1 2 3 4 5 private String answers 6 7 8 9 10 public String processInput String theInput String theOutput null if state WAITING theOutput Holle state SENTKNOCKKNOCK else if state SENTKNOCKKNOCK if theInput equalsIgnoreCase 你好 theOutput clues currentJoke state SENTCLUE else theOutput 8888888 else if state SENTCLUE if theInput equalsIgnoreCase clues currentJoke string98 theOutput answers currentJoke yyyyy state ANOTHER else theOutput rrrrrr hhhhhhhhhh state SENTKNOCKKNOCK else if state ANOTHER if theInput equalsIgnoreCase y theOutput 565656 if currentJoke NUMJOKES 1 currentJoke 0 else currentJoke state SENTKNOCKKNOCK else theOutput Bye state WAITING return theOutput 2 调试使以上工程能
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论