代碼只兼容JAVA 7及以上版本。
服務器端代碼:
package com.stevex.app.bio;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.net.ServerSocketFactory;
public class XiaoNa {
public static void main(String[] args){
XiaoNa xiaona = new XiaoNa();
ServerSocket serverSocket = null;
Socket socket = null;
try {
//可以設置客戶連接請求隊列的長度,比如5,隊列長度超過5後拒絕連接請求
//serverSocket = ServerSocketFactory.getDefault().createServerSocket(8383, 5);
serverSocket = ServerSocketFactory.getDefault().createServerSocket(8383);
while(true){
try{
//監聽直到接受連接後返回一個新Socket對象
socket = serverSocket.accept();//阻塞
//new一個線程處理連接請求
new Thread(xiaona.new Worker(socket)).start();;
}
catch (Throwable e) { //防止發生異常搞死服務器
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
if(socket != null){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String getAnswer(String question){
String answer = null;
switch(question){
case "who":
answer = "我是小娜";
break;
case "what":
answer = "我是來幫你解悶的";
break;
case "where":
answer = "我來自外太空";
break;
default:
answer = "請輸入 who, 或者what, 或者where";
}
return answer;
}
private class Worker implements Runnable{
private Socket socket;
public Worker(Socket socket){
this.socket = socket;
}
@Override
public void run() {
BufferedReader reader = null;
PrintWriter writer = null;
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream());
String question = reader.readLine();//沒有內容會阻塞
while(!question.equals("OVER")){
String answer = getAnswer(question);
writer.println(answer);
question = reader.readLine();
}
writer.println("OVER");//OVER作為操作完成暗號
writer.flush();
if(writer != null){
writer.close();
}
if(reader != null){
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客戶端代碼:
package com.stevex.app.bio;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
Client c = new Client();
//種20個線程發起Socket客戶端連接請求
for(int i=0; i<20; i++){
new Thread(c.new Worker()).start();
}
}
private class Worker implements Runnable{
@Override
public void run() {
Socket socket = null;
BufferedReader reader = null;
PrintWriter writer = null;
try {
//創建一個Socket並連接到指定的目標服務器
socket = new Socket("localhost", 8383);
reader = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream());
writer.println("who");
writer.println("what");
writer.println("where");
writer.println("OVER");//OVER作為操作完成暗號
writer.flush();
String answer = reader.readLine(); //沒有內容會阻塞
while(! answer.equals("OVER")){
System.out.println(Thread.currentThread().getId() + "---Message from server:" + answer);
answer = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
if (reader != null) {
reader.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Java中介者設計模式 http://www.linuxidc.com/Linux/2014-07/104319.htm
Java 設計模式之模板方法開發中應用 http://www.linuxidc.com/Linux/2014-07/104318.htm
設計模式之 Java 中的單例模式(Singleton) http://www.linuxidc.com/Linux/2014-06/103542.htm
Java對象序列化 http://www.linuxidc.com/Linux/2014-10/107584.htm
大話設計模式(帶目錄完整版) PDF+源代碼 http://www.linuxidc.com/Linux/2014-08/105152.htm
Java中的函數傳遞 http://www.linuxidc.com/Linux/2014-11/109056.htm