?? knockknockserver.java
字號:
//下面KKState類實現了一個有趣的對話過程的主要流程:用戶啟動服務程序,服務程序監聽來自網絡的連接請求,
//如果有客戶連接,則建立連接。然后,在客戶終端上顯示"Knock! Knock!",客戶端如果輸入”Who's there?”,則
//回答"Turnip"。客戶輸入"Turnip who?",回答"Turnip the heat, it's cold in here! Want another? (y/n)"。如果客戶
//輸入"y",則進行下一輪新的對話,否則結束。關于對話的內容事先存儲在兩個字符串數組中。
import java.net.*;
import java.io.*;
/**
* @com.register ( clsid=F8188795-F5FC-11D2-A9DF-000021E9BCD1, typelib=F8188794-F5FC-11D2-A9DF-000021E9BCD1 )
*/
class KKState {
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 = { "Turnip", "Little Old Lady", "Atch", "Who", "Who" };
private String[] answers = { "Turnip the heat, it's cold in here!",
"I didn't know you could yodel!",
"Bless you!",
"Is there an owl in here?",
"Is there an echo in here?" };
String processInput(String theInput) {
String theOutput = null;
if (state == WAITING) {
theOutput = "Knock! Knock!";
state = SENTKNOCKKNOCK;
} else if (state == SENTKNOCKKNOCK) {
if (theInput.equalsIgnoreCase("Who's there?")) {
theOutput = clues[currentJoke];
state = SENTCLUE;
} else {
theOutput = "You're supposed to say \"Who's there?\"! Try again. Knock! Knock!";
}
} else if (state == SENTCLUE) {
if (theInput.equalsIgnoreCase(clues[currentJoke] + " who?")) {
theOutput = answers[currentJoke] + " Want another? (y/n)";
state = ANOTHER;
} else {
theOutput = "You're supposed to say \"" + clues[currentJoke] + " who?\"" + "! Try again. Knock! Knock!";
state = SENTKNOCKKNOCK;
}
} else if (state == ANOTHER) {
if (theInput.equalsIgnoreCase("y")) {
theOutput = "Knock! Knock!";
if (currentJoke == (NUMJOKES - 1))
currentJoke = 0;
else
currentJoke++;
state = SENTKNOCKKNOCK;
} else {
theOutput = "Bye.";
state = WAITING;
}
}
return theOutput;
}
}
//服務器程序
/**
* @com.register ( clsid=F8188796-F5FC-11D2-A9DF-000021E9BCD1, typelib=F8188794-F5FC-11D2-A9DF-000021E9BCD1 )
*/
class KnockKnockServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444); //創建服務器端socket
} catch (IOException e) {
System.out.println("Could not listen on port: " + 4444 + ", " + e);
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();//監聽申請連接到該socket的要求并建立連接
} catch (IOException e) {
System.out.println("Accept failed: " + 4444 + ", " + e);
System.exit(1);
}
try {
//通過客戶端socket,獲取輸入數據流對象,通過它可以讀入客戶端輸入的信息
//通過客戶端socket,獲取輸出數據流對象,通過它可以向客戶端輸入的信息
DataInputStream is = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
PrintStream os=new PrintStream(new BufferedOutputStream(clientSocket.getOutputStream(), 1024), false);
//KKState類用于處理輸入的會話信息,然后返回設定好的回答
KKState kks = new KKState();
String inputLine, outputLine;
outputLine = kks.processInput(null);
os.println(outputLine);//第一次運行,輸入參數為null,kks返回:"Knock! Knock!";然后向客戶端寫入。
os.flush();
//如果客戶端沒有輸入,readLine將阻塞;如果客戶端關閉,readLine返回為null。
//在下面循環語句中,以讀一行,再寫一行的方式與客戶交互。
while ((inputLine = is.readLine()) != null) {
System.out.println("cli:"+inputLine);
outputLine = kks.processInput(inputLine);
os.println(outputLine);
os.flush();
if (outputLine.equals("Bye.")) break;
}
//以下完成清除工作。
os.close();
is.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -