本文主要是介绍Java多线程Socket操作猜数游戏样例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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();
}
}
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();
}
}
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 运行效果
这篇关于Java多线程Socket操作猜数游戏样例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!