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

📄 stockinfo.java~27~

📁 手机端游戏开发程序
💻 JAVA~27~
字号:
package stock;

import java.io.*;
import java.net.*;
import java.util.*;

//import chess.SocketConn;

//**********************************
//关闭服务器时 要关闭的还有serverSocket
//**********************************
public class StockInfo
    implements Runnable
{
  public static final String URL_LIVE_HEADER =
      "http://cn.finance.yahoo.com/d/quotes.csv?s=";
  public static final String URL_LIVE_FOOTER = "&m=c&f=sl1d1t1c1ohgv&e=.csv";

  public static final String URL_TABLE_HEADER =
      "http://table.finance.yahoo.com/table.csv?a=";
  public static final String URL_TABLE_FOOTER = "&y=0&g=d&ignore=.csv";
  String stock_symbol = null;

  ServerSocket serverSocket;
  Socket clientconn;
  InputStream is;
  OutputStream os;

  String usermessage;
  String stockmessage;
  String message;
  MainFrame mf;
  String loginusername;
  public StockInfo(MainFrame mf)
  {
    try
    {
      this.mf = mf;
      serverSocket = new ServerSocket(new Integer(mf.TextField_port.getText()).
                                      intValue());
      mf.log.AddLine("服务器开始于:" +
                     serverSocket.getInetAddress().
                     getLocalHost() +
                     ":" + serverSocket.getLocalPort());
    }
    catch (Exception e)
    {
      mf.log.AddLine(e.toString());
    }

  }

  public void run()
  {
    mf.log.AddLine("test");
    while (true)
    {
      try
      {
        // clientconn = serverSocket.accept();

        //is = clientconn.getInputStream();
        //os = clientconn.getOutputStream();
        Socket alink = serverSocket.accept();
        mf.log.AddLine("用户连接:" + alink);
        DataInputStream input = new DataInputStream(alink.getInputStream());
        String strRec = input.readUTF();
        //BufferedReader br = new BufferedReader(new InputStreamReader(is));
        //String msg = br.readLine(); //接收到的消息
        mf.recMsg.AddLine(strRec);
        input.close();
        alink.close();
        MessageClass mc = new MessageClass(strRec);
        this.loginusername = mc.first;
        usermessage = mc.third; //600xxx.SS(live)(historical)

        mf.log.AddLine(usermessage);
        stock_symbol = usermessage.substring(0, 9); //9bit  600xxx.SS
        mf.log.AddLine("股票代码" + stock_symbol);
        stockmessage = usermessage.substring(9); //live or historical
        System.out.println(usermessage);
        mf.log.AddLine("要求信息为:" + stockmessage);
        if (stockmessage.equals("live"))
        {
          dealLive(stock_symbol, os); //取股票实时信息并发送至手机端
        }
        else if (stockmessage.equals("historical1") ||
                 stockmessage.equals("historical2")
                 || stockmessage.equals("historical3") ||
                 stockmessage.equals("historical4"))
        {
          dealHistorical(stock_symbol, os); //取股票历史信息并发送至手机端
        }
        stop(); //关闭连接的Socket及输入输出流
      }
      catch (Exception e)
      {
        mf.log.AddLine(e.toString());
      }
    }
  }

  public void dealLive(String symbol, OutputStream os)
  {
    int lastPrice = 0;
    int changePrice = 0;
    int openPrice = 0;
    int highPrice = 0;
    int lowPrice = 0;
    int volume = 0;

    DataOutputStream dos = new DataOutputStream(os);
    try
    {
      String str = symbol;
      URL url = new URL(URL_LIVE_HEADER + symbol + URL_LIVE_FOOTER);
      InputStream is = url.openStream();

      BufferedReader in = new BufferedReader(new InputStreamReader(is));
      String data = in.readLine();
      data = data.replace('"', ',');
      StringTokenizer st = new StringTokenizer(data, ",");

      // Skipping
      st.nextToken(); // Symbol

      lastPrice = (int) (Float.parseFloat(st.nextToken()) * 1000);

      st.nextToken(); // Date
      st.nextToken(); // Time

      changePrice = (int) (Float.parseFloat(st.nextToken()) * 1000);
      openPrice = (int) (Float.parseFloat(st.nextToken()) * 1000);
      highPrice = (int) (Float.parseFloat(st.nextToken()) * 1000);
      lowPrice = (int) (Float.parseFloat(st.nextToken()) * 1000);
      volume = Integer.parseInt(st.nextToken());

    }
    catch (Exception e)
    {
      mf.log.AddLine(e.toString());
    }

    String ack = (1 + "*" + lastPrice) + ("*" + changePrice) + ("*" + openPrice) +
        ("*" + highPrice) + ("*" + lowPrice) + ("*" + volume + "*");
    this.SendOneMsg("stock.#." + loginusername + "." + ack);
    /*
           dos.write( (1 + "*" + lastPrice).getBytes());
           dos.write( ("*" + changePrice).getBytes());
           dos.write( ("*" + openPrice).getBytes());
           dos.write( ("*" + highPrice).getBytes());
           dos.write( ("*" + lowPrice).getBytes());
           dos.write( ("*" + volume + "*" + '\n').getBytes());*/

  }

  public void dealHistorical(String symbol, OutputStream os)
  {
    String dateString = null;
    int year = 0;
    int month = 0;
    int date = 0;
    int highPrice = 0;
    int lowPrice = 0;
    int volume = 0;
    String lastMonthString = "";
    int lastMonth = 0;
    Calendar calendar = Calendar.getInstance();
    int c = calendar.get(Calendar.YEAR);
    int a = calendar.get(Calendar.MONTH);
    int b = calendar.get(Calendar.DAY_OF_MONTH);
    String s = "1" + "*";
    mf.log.AddLine("month:" + (a + 1) + "day:" + b + "year:" + c);
    DataOutputStream dos = new DataOutputStream(os);
    try
    {
      // BufferedReader in = new BufferedReader(new FileReader(
      //    "D:\\Java\\table.txt"));
      BufferedReader in;
      if (stockmessage.equals("historical1"))
        in = new BufferedReader(new FileReader(
            "table.txt"));
      else if (stockmessage.equals("historical2"))
        in = new BufferedReader(new FileReader(
            "table.txt"));
      else if (stockmessage.equals("historical2"))
        in = new BufferedReader(new FileReader(
            "table.txt"));
      else in = new BufferedReader(new FileReader(
            "table.txt"));

      in.readLine(); // Skip the title line.
      String data = null;
      data = in.readLine();
      while (data != null && data.length() > 0)
      {
        StringTokenizer st = new StringTokenizer(data, ",");
        dateString = st.nextToken();
        StringTokenizer dateSt = new StringTokenizer(dateString, "-");
        date = Integer.parseInt(dateSt.nextToken());
        String monthString = dateSt.nextToken();

        if (monthString.equals(lastMonthString))
        {
          month = lastMonth;
        }
        else if (monthString.equals("Jan"))
        {
          month = 1;
        }
        else if (monthString.equals("Feb"))
        {
          month = 2;
        }
        else if (monthString.equals("Mar"))
        {
          month = 3;
        }
        else if (monthString.equals("Apr"))
        {
          month = 4;
        }
        else if (monthString.equals("May"))
        {
          month = 5;
        }
        else if (monthString.equals("Jun"))
        {
          month = 6;
        }
        else if (monthString.equals("Jul"))
        {
          month = 7;
        }
        else if (monthString.equals("Aug"))
        {
          month = 8;
        }
        else if (monthString.equals("Sep"))
        {
          month = 9;
        }
        else if (monthString.equals("Oct"))
        {
          month = 10;
        }
        else if (monthString.equals("Nov"))
        {
          month = 11;
        }
        else if (monthString.equals("Dec"))
        {
          month = 12;
        }
        if (month != lastMonth)
        {
          lastMonth = month;
          lastMonthString = monthString;
        }

        year = Integer.parseInt(dateSt.nextToken());
        st.nextToken(); // Open
        highPrice = (int) (Float.parseFloat(st.nextToken()) * 1000);
        lowPrice = (int) (Float.parseFloat(st.nextToken()) * 1000);
        st.nextToken(); // Close
        volume = Integer.parseInt(st.nextToken());
        s = s + (year + "-" + month + "-" + date + " " +
                 highPrice + " " + lowPrice + " " + volume + "*");
        data = in.readLine();
      } // End while loop.
      mf.log.AddLine(s);
      this.SendOneMsg("stock.#." + loginusername + "." + s);
      //dos.write( (s + '\n').getBytes());
    }
    catch (Exception e)
    {
      System.out.println(e);
    }
  }

  public void stop()
  {
    try
    {
      System.out.println("****************");
      if (is != null)
      {
        is.close();
      }
      if (os != null)
      {
        os.close();
      }
      if (clientconn != null)
      {
        clientconn.close();
      }
    }
    catch (IOException ioe)
    {}
  }

  public void stopserver()
  {
    try
    {
      if (is != null)
      {
        is.close();
      }
      if (os != null)
      {
        os.close();
      }
      if (clientconn != null)
      {
        clientconn.close();
      }
      if (serverSocket != null)
      {
        serverSocket.close();
      }
    }
    catch (IOException ioe)
    {}
  }

  public void SendOneMsg(String s)
  {
    SocketConn sc = new SocketConn(mf.TextField_ip.getText(), 6001);
    sc.send(s);
    mf.ackMsg.AddLine(s); //record the records
    sc.close();

  }
//关闭服务器时 要关闭的还有serverSocket
} // End of class def.

⌨️ 快捷键说明

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