实验六 基于协议的网络编程
实验目的:
1、理解网络协议编程的基本原理;
2、学会基于协议编程的设计思路和技术;
3、掌握基于协议编程的具体应用。
实验要求:
1、建立一个的基于协议编程的JAVA工程;
2、建立协议编程的JAVA类,并能正确运行且能实现协议通信;
3、对JAVA类的功能进行拓广,使基于协议编程用于某一具体的应用。
实验内容:
1、建立工程和JAVA类,类程序的原代码为:
import java.io.*;
import java.net.*;
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 java.net.*;
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 java.net.*;
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、调试使以上工程能正常运行,使程序能正常通信。
3、对协议编程的程序代码进行阅读和理解,要求做到每条语句都能明白其具体含义,每个类都了解其功能。
4、对以上程序进行适当的修改,要求协议通信功能保持严谨,设计一个实用的协议。
5、撰写实验报告
实验报告包含:实验报告首页、实验步骤(每步的内容,有程序的要求有源码和运行结果及结果描述)、实验心得。