⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stockquoteclient.java

📁 java开发的网络方面的原代码例子
💻 JAVA
字号:
//StockQuoteClient.java
import java.io.*;    
import java.net.*;   

public class StockQuoteClient {
   //服务器监听端口
   private static final int SERVER_PORT = 1701;
   //是否自动刷新 
   private static final boolean AUTOFLUSH = true;
   private String serverName;
   private Socket quoteSocket = null;
   private BufferedReader quoteReceive = null;
   private PrintWriter quoteSend = null;
   //请求ID的数组
   private String[] stockIDs;  
   //返回数据的数组
   private String[] stockInfo;   
   //数据的日期时间
   private String currentAsOf = null;  

   public static void main(String[] args) {
      if (args.length < 2) {
         System.out.println(
            "Usage: StockQuoteClient <server> <stock ids>");
         System.exit(1);
      }
      StockQuoteClient client = new StockQuoteClient(args);
      client.printQuotes(System.out);
      System.exit(0);
   }

   public StockQuoteClient(String[] args) {
      String serverInfo;

      // 服务器名字是第一个参数
      serverName = args[0];      
      stockIDs = new String[args.length-1];
      stockInfo = new String[args.length-1];
      
      for (int index = 1; index < args.length; index++) {
         stockIDs[index-1] = args[index];
      }
      // 和服务器通信并且返回" HELLO" 
      serverInfo = contactServer();

      if (serverInfo != null) {
         currentAsOf = serverInfo.substring(
                        serverInfo.indexOf(" ")+1);
      }
      //获取股票信息
      getQuotes();
      //关闭连接
      quitServer();
   }
   
   protected String contactServer() {
      String serverWelcome = null;

      try {
         //向服务器打开一个套接字
         quoteSocket = new Socket(serverName,SERVER_PORT);
         //得到输入输出的数据
         quoteReceive = new BufferedReader(
                          new InputStreamReader(
                             quoteSocket.getInputStream()));
         quoteSend = new PrintWriter(
                           quoteSocket.getOutputStream(),
                           AUTOFLUSH);
         // 读取 "HELLO "
         serverWelcome = quoteReceive.readLine();
      } catch (UnknownHostException excpt) {
         System.err.println("Unknown host " + serverName + 
                              ": " + excpt);
      } catch (IOException excpt) {
         System.err.println("Failed I/O to " + serverName +
                              ": " + excpt);
      }
      //返回"HELLO"
      return serverWelcome;   
   }
   //得到股票信息
   protected void getQuotes() {
      String response;  
      if (connectOK()) {
         try {
            for (int index = 0; index < stockIDs.length; 
                  index++) {
               quoteSend.println("STOCK: "+stockIDs[index]);
               response = quoteReceive.readLine();
               stockInfo[index] = response.substring(
                                    response.indexOf(" ")+1);
            }
         } catch (IOException excpt) {
            System.err.println("Failed I/O to " + serverName
                                 + ": " + excpt);
         }
      }
   }
   //退出服务器   
   protected String quitServer() {
      String serverBye = null;   

      try {
         if (connectOK()) {
            quoteSend.println("QUIT");
            serverBye = quoteReceive.readLine();
         }
         if (quoteSend != null) quoteSend.close();
         if (quoteReceive != null) quoteReceive.close();
         if (quoteSocket != null) quoteSocket.close();
      } catch (IOException excpt) {
         System.err.println("Failed I/O to server " + 
                              serverName + ": " + excpt);
      }
      return serverBye; 
   }
   //输出股票信息
   public void printQuotes(PrintStream sendOutput) {
      if (currentAsOf != null) {
         sendOutput.print("INFORMATION ON REQUESTED QUOTES" 
            + "\n\tCurrent As Of: " + currentAsOf + "\n\n");
         for (int index = 0; index < stockIDs.length; 
               index++) {
            sendOutput.print(stockIDs[index] + ":");
            if (stockInfo[index] != null)
               sendOutput.println(" " + stockInfo[index]);
            else sendOutput.println();
         }
      }
   }
  
  protected boolean connectOK() {
      return (quoteSend != null && quoteReceive != null && 
               quoteSocket != null);
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -