1 服務器端程序,接受連接並處理客戶端的請求
ServerApp.Java
package test;
import Java.io.*;
import Java.Net.*;
/** *//**
* 服務器端程序
*
* @author luxuan310,修正趙學慶。
*
*/
public class ServerApp ...{
static final int PORT = 9000;
private ServerSocket serverSocket;
private Socket socket;
private BufferedReader netIn;
private PrintWriter netOut;
public ServerApp() throws IOException ...{
serverSocket = new ServerSocket(PORT);
System.out.println("server start");
while (true) ...{
// 等待連接
socket = serverSocket.accept();
ServerThread st = new ServerThread(socket);
new Thread(st).start();
}
}
class ServerThread implements Runnable ...{
private Socket socket;
private int randomNumber;
private int clIEntGuessNumber;
public ServerThread(Socket s) throws IOException ...{
socket = s;
netIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
netOut = new PrintWriter(socket.getOutputStream());
}
public void run() ...{
System.out.println("clIEnt" + socket.getInetAddress() + " is connect");
randomNumber = (int) (Math.random() * 100);
System.out.println("random number is:" + randomNumber);
try ...{
clIEntGuessNumber = Integer.parseInt(netIn.readLine());
System.out.println("client guess number is:" + clIEntGuessNumber);
for (int i = 3; i > 0; i--) ...{
if (clIEntGuessNumber == randomNumber) ...{
netOut.println("OK,恭喜,猜對了");
ClIEntApp.finished = true;
} else if (clIEntGuessNumber < randomNumber) ...{
netOut.println("您猜的數小了,您還有" + (i - 1) + "次機會");
ClIEntApp.finished = false;
} else if (clIEntGuessNumber > randomNumber) ...{
netOut.println("您猜的數大了,您還有" + (i - 1) + "次機會");
ClIEntApp.finished = false;
}
netOut.flush();
if (!ClIEntApp.finished) ...{
clIEntGuessNumber = Integer.parseInt(netIn.readLine());
} else ...{
break;
}
}
if (!ClIEntApp.finished) ...{
netOut.println("OK,您沒有機會了,游戲結束");
}
ClIEntApp.finished = true;
} catch (IOException e) ...{
} finally ...{
try ...{
netOut.close();
netIn.close();
socket.close();
} catch (IOException ee) ...{
}
}
}
}
public static void main(String[] args) throws IOException ...{
new ServerApp();
}
}
2 客戶端程序,發送每次猜的數字
package test;
import Java.io.*;
import Java.Net.*;
public class ClIEntApp ...{
private Socket socket;
private BufferedReader netIn;
private PrintWriter netOut;
private BufferedReader keyboardIn;
static Boolean finished = false;
public ClIEntApp() throws IOException ...{
System.out.println("請輸入服務器地址,連接本地服務器請輸入localhost");
keyboardIn = new BufferedReader(new InputStreamReader(System.in));
try ...{
if (keyboardIn.readLine().equalsIgnoreCase("localhost")) ...{
socket = new Socket(InetAddress.getLocalHost(), ServerApp.PORT);
} else ...{
socket = new Socket(InetAddress.getByName(keyboardIn.readLine()), ServerApp.PORT);
}
netIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
netOut = new PrintWriter(socket.getOutputStream());
} catch (UnknownHostException e) ...{
System.err.println("連接不到服務器");
System.exit(-1);
}
System.out.println("連接成功");
while (!finished) ...{
System.out.println("請輸入0-100之間的數字");
netOut.println(keyboardIn.readLine());
netOut.flush();
String str = netIn.readLine();
if (str == null) ...{
finished = true;
break;
}
System.out.println(str);
if (str.startsWith("OK")) ...{
finished = true;
break;
}
}
netIn.close();
netOut.close();
keyboardIn.close();
socket.close();
}
public static void main(String[] args) throws IOException ...{
new ClIEntApp();
}
}
3 運行效果