您好,登錄后才能下訂單哦!
使用Java Socket實現一個猜數字小游戲?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
運行截圖
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 { //創建一個服務器 System.out.println("服務端啟動,等待客戶端連接。。。"); PrintWriter out = null; Scanner getClient = null; ServerSocket server = null; Socket client = null; this.setFlag(); try { server = new ServerSocket(6666); //創建一個接收連接客戶端的對象 client = server.accept(); System.out.println(client.getInetAddress() + " 已成功連接到此臺服務器上。"); //字符輸出流 out = new PrintWriter(client.getOutputStream()); //向客戶端發送數據 out.println("歡迎來到猜數字小游戲(1-100)!!!"); out.flush();//將緩沖流中的數據全部輸出 getClient = new Scanner(client.getInputStream()); //接收客戶端發送的數據 //阻塞等待客戶端發送消息過來 while (getClient.hasNextLine()) { String returnMsg = null; String tmp = getClient.nextLine(); if ("e".equals(tmp)) { break; } else if ("c".equals(tmp)) { returnMsg = "歡迎來到新一輪的猜數字小游戲(1-100)!!!"; this.setFlag(); } else { int clientInput = Integer.parseInt(tmp); if (clientInput > flag) { returnMsg = "你輸入的數字大了!!!"; } else if (clientInput < flag) { returnMsg = "你輸入的數字小了!!!"; } else { returnMsg = "恭喜你,猜中了!!!繼續(輸入c),退出(輸入e)"; } } out.println(returnMsg); //向客戶端發送數據 out.flush(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { out.close(); getClient.close(); server.close(); } } // 生成隨機數字 public void setFlag() { Random r = new Random(); flag = r.nextInt(100); System.out.println("猜數字小游戲答案: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("正在向服務器請求連接。。。"); 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("請輸入數字:"); //先讀取鍵盤錄入方可向服務端發送消息 getKey = new Scanner(System.in); while (getKey.hasNextLine()) { //寫到服務端的的控制臺 out.println(getKey.nextLine()); out.flush(); try { System.out.println("提示消息:" + getServer.nextLine()); System.out.print("請輸入數字:"); } catch (Exception e) { System.out.print("游戲結束!!!"); break; } //阻塞等待接收服務端的消息 } } 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(); } }
看完上述內容,你們掌握使用Java Socket實現一個猜數字小游戲的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。