熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java高級技術 >> 正文

Java多線程Socket操作猜數游戲樣例

2022-06-13   來源: Java高級技術 

   服務器端程序接受連接並處理客戶端的請求

 ServerAppjava
    package test;
    import javaio*;
    import *;
    /** *//**
     * 服務器端程序
     *
     * @author luxuan修正趙學慶
     *
     */
    public class ServerApp {
      static final int PORT = ;
      private ServerSocket serverSocket;
      private Socket socket;
      private BufferedReader netIn;
      private PrintWriter netOut;
      public ServerApp() throws IOException {
        serverSocket = new ServerSocket(PORT);
        Systemoutprintln(server start);
        while (true) {
          // 等待連接
          socket = serverSocketaccept();
          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(socketgetInputStream()));
          netOut = new PrintWriter(socketgetOutputStream());
        }
        public void run() {
          Systemoutprintln(client + socketgetInetAddress() + is connect);
          randomNumber = (int) (Mathrandom() * );
          Systemoutprintln(random number is: + randomNumber);
          try {
            clientGuessNumber = IntegerparseInt(netInreadLine());
            Systemoutprintln(client guess number is: + clientGuessNumber);
            for (int i = ; i > ; i) {
              if (clientGuessNumber == randomNumber) {
                netOutprintln(OK恭喜猜對了);
                ClientAppfinished = true;
              } else if (clientGuessNumber < randomNumber) {
                netOutprintln(您猜的數小了您還有 + (i ) + 次機會);
                ClientAppfinished = false;
              } else if (clientGuessNumber > randomNumber) {
                netOutprintln(您猜的數大了您還有 + (i ) + 次機會);
                ClientAppfinished = false;
              }
              netOutflush();
              if (!ClientAppfinished) {
                clientGuessNumber = IntegerparseInt(netInreadLine());
              } else {
                break;
              }
            }
            if (!ClientAppfinished) {
              netOutprintln(OK您沒有機會了游戲結束);
            }
            ClientAppfinished = true;
          } catch (IOException e) {
          } finally {
            try {
              netOutclose();
              netInclose();
              socketclose();
            } catch (IOException ee) {
            }
          }
        }
      }
      public static void main(String[] args) throws IOException {
        new ServerApp();
      }
    }
    客戶端程序發送每次猜的數字

   package test;
    import javaio*;
    import *;
    public class ClientApp {
      private Socket socket;
      private BufferedReader netIn;
      private PrintWriter netOut;
      private BufferedReader keyboardIn;
      static Boolean finished = false;
      public ClientApp() throws IOException {
        Systemoutprintln(請輸入服務器地址連接本地服務器請輸入localhost);
        keyboardIn = new BufferedReader(new InputStreamReader(Systemin));
        try {
          if (keyboardInreadLine()equalsIgnoreCase(localhost)) {
            socket = new Socket(InetAddressgetLocalHost() ServerAppPORT);
          } else {
            socket = new Socket(InetAddressgetByName(keyboardInreadLine()) ServerAppPORT);
          }
          netIn = new BufferedReader(new InputStreamReader(socketgetInputStream()));
          netOut = new PrintWriter(socketgetOutputStream());
        } catch (UnknownHostException e) {
          Systemerrprintln(連接不到服務器);
          Systemexit();
        }
        Systemoutprintln(連接成功);
        while (!finished) {
          Systemoutprintln(請輸入之間的數字);
          netOutprintln(keyboardInreadLine());
          netOutflush();
          String str = netInreadLine();
          if (str == null) {
            finished = true;
            break;
          }
          Systemoutprintln(str);
          if (strstartsWith(OK)) {
            finished = true;
            break;
          }
        }
        netInclose();
        netOutclose();
        keyboardInclose();
        socketclose();
      }
      public static void main(String[] args) throws IOException {
        new ClientApp();
      }
    }       
    運行結果

  


From:http://tw.wingwit.com/Article/program/Java/gj/201311/27387.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.