Java Socket實(shí)現(xiàn)猜數(shù)字小游戲
本文實(shí)例為大家分享了Java Socket實(shí)現(xiàn)猜數(shù)字游戲的具體代碼,供大家參考,具體內(nèi)容如下
運(yùn)行截圖
Server

Client

完整代碼
Server
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
import java.util.Scanner;
public class ServerDemo {
private int flag;
public void server() throws IOException {
//創(chuàng)建一個(gè)服務(wù)器
System.out.println("服務(wù)端啟動(dòng),等待客戶端連接。。。");
PrintWriter out = null;
Scanner getClient = null;
ServerSocket server = null;
Socket client = null;
this.setFlag();
try {
server = new ServerSocket(6666);
//創(chuàng)建一個(gè)接收連接客戶端的對(duì)象
client = server.accept();
System.out.println(client.getInetAddress() + " 已成功連接到此臺(tái)服務(wù)器上。");
//字符輸出流
out = new PrintWriter(client.getOutputStream()); //向客戶端發(fā)送數(shù)據(jù)
out.println("歡迎來到猜數(shù)字小游戲(1-100)?。?!");
out.flush();//將緩沖流中的數(shù)據(jù)全部輸出
getClient = new Scanner(client.getInputStream()); //接收客戶端發(fā)送的數(shù)據(jù)
//阻塞等待客戶端發(fā)送消息過來
while (getClient.hasNextLine()) {
String returnMsg = null;
String tmp = getClient.nextLine();
if ("e".equals(tmp)) {
break;
} else if ("c".equals(tmp)) {
returnMsg = "歡迎來到新一輪的猜數(shù)字小游戲(1-100)?。?!";
this.setFlag();
} else {
int clientInput = Integer.parseInt(tmp);
if (clientInput > flag) {
returnMsg = "你輸入的數(shù)字大了?。?!";
} else if (clientInput < flag) {
returnMsg = "你輸入的數(shù)字小了?。?!";
} else {
returnMsg = "恭喜你,猜中了?。?!繼續(xù)(輸入c),退出(輸入e)";
}
}
out.println(returnMsg); //向客戶端發(fā)送數(shù)據(jù)
out.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
out.close();
getClient.close();
server.close();
}
}
// 生成隨機(jī)數(shù)字
public void setFlag() {
Random r = new Random();
flag = r.nextInt(100);
System.out.println("猜數(shù)字小游戲答案:flag = " + flag);
}
public static void main(String[] args) throws IOException {
new ServerDemo().server();
}
}
Client
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class ClientDemo {
public void client() throws IOException {
System.out.println("正在向服務(wù)器請(qǐng)求連接。。。");
Socket client = null;
Scanner getKey = null;
Scanner getServer = null;
PrintWriter out = null;
try {
client = new Socket("127.0.0.1", 6666);
getServer = new Scanner(client.getInputStream());
System.out.println(getServer.nextLine());
out = new PrintWriter(client.getOutputStream());
System.out.print("請(qǐng)輸入數(shù)字:");
//先讀取鍵盤錄入方可向服務(wù)端發(fā)送消息
getKey = new Scanner(System.in);
while (getKey.hasNextLine()) {
//寫到服務(wù)端的的控制臺(tái)
out.println(getKey.nextLine());
out.flush();
try {
System.out.println("提示消息:" + getServer.nextLine());
System.out.print("請(qǐng)輸入數(shù)字:");
} catch (Exception e) {
System.out.print("游戲結(jié)束?。?!");
break;
}
//阻塞等待接收服務(wù)端的消息
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
getKey.close();
getServer.close();
out.close();
client.close();
}
}
public static void main(String[] args) throws IOException {
new ClientDemo().client();
}
}
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)多線程下載和斷點(diǎn)續(xù)傳
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)多線程下載和斷點(diǎn)續(xù)傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
IDEA創(chuàng)建Servlet并配置web.xml的實(shí)現(xiàn)
這篇文章主要介紹了IDEA創(chuàng)建Servlet并配置web.xml的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Java?stream?sorted使用?Comparator?進(jìn)行多字段排序的方法
這篇文章主要介紹了Java?stream?sorted使用?Comparator?進(jìn)行多字段排序,主要講解使用Java?Stream流排序器Comparator對(duì)List集合進(jìn)行多字段排序的方法,包括復(fù)雜實(shí)體對(duì)象多字段升降序排序方法,需要的朋友可以參考下2023-03-03
SpringSecurity默認(rèn)登錄頁的使用示例教程
Spring 是非常流行和成功的 Java 應(yīng)用開發(fā)框架,Spring Security 正是 Spring 家族中的成員,Spring Security 基于 Spring 框架,提供了一套 Web 應(yīng)用安全性的完整解決方案,本文給大家介紹SpringSecurity的默認(rèn)登錄頁的使用教程,感興趣的朋友一起看看吧2023-12-12

