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

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

2013-11-23 19:57:50  來源: Java高級技術 

    客戶端程序發送每次猜的數字


package test;
  import javaio*;
  import javanet*;
  public class ClientApp {
  private Socket socket;
  private BufferedReader netIn;
  private PrintWriter netOut;
  private BufferedReader keyboardIn;
  xxxx
  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()));
  xxxx
  netOut = new PrintWriter(socketgetOutputStream());
  } catch (UnknownHostException e) {
  Systemerrprintln(連接不到服務器);
  Systemexit();
  }
  Systemoutprintln(連接成功);
  while (!finished) {
  Systemoutprintln(請輸入之間的數字);
  netOutprintln(keyboardInreadLine());
  netOutflush();
  String str = netInreadLine();
  if (str == null) {
  finished = true;
  xxxx
  break;
  }
  Systemoutprintln(str);
  if (strstartsWith(OK)) {
  finished = true;
  break;
  }
  }
  netInclose();
  netOutclose();
  keyboardInclose();
  socketclose();
  }
  public static void main(String[] args) throws IOException {
  new ClientApp(); xxxx
  }
  }

[]  []  


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