| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 948 人关注过本帖
标题:一个小问题
只看楼主 加入收藏
wangminjian
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-10-31
收藏
 问题点数:0 回复次数:2 
一个小问题
请高手看一下:
import *;   
import *;   
import java.util.*;
public class StockQuoteServer {
   //服务器监听端口
   private static final int SERVER_PORT = 1701;
   // 最大连接个数
   private static final int MAX_CLIENTS = 50;
   // 股票数据的匹配格式:
   // <stock-id> <stock information>
   private static final File STOCK_QUOTES_FILE =
      new File("stockquotes.txt");
   private ServerSocket listenSocket = null;
   private Hashtable stockInfo;
   private Date stockInfoTime;
   private long stockFileMod;
   private boolean keepRunning = true;

   public static void main(String[] args) {
      StockQuoteServer server = new StockQuoteServer();
      server.serveQuotes();
   }
   public StockQuoteServer() {
      
      if (!loadQuotes()) System.exit(1);
      try {
         //创建一个套接字
         listenSocket =
            new ServerSocket(SERVER_PORT,MAX_CLIENTS);
      } catch(IOException excpt) {
         System.err.println("Unable to listen on port " +
                              SERVER_PORT + ": " + excpt);
         System.exit(1);
      }
   }
   //读取股票的数据文件
   protected boolean loadQuotes() {
      String fileLine;
      StringTokenizer tokenize;
      String id;
      StringBuffer value;
   
      try {
         BufferedReader stockInput = new BufferedReader(
            new FileReader(STOCK_QUOTES_FILE));
         stockInfo = new Hashtable();
         //按行循环读取
         while ((fileLine = stockInput.readLine()) != null) {
           tokenize = new StringTokenizer(fileLine);
           try {
             id = tokenize.nextToken();
             id = id.toUpperCase();
             value = new StringBuffer();
             while(tokenize.hasMoreTokens()) {
               value.append(tokenize.nextToken());
               if (tokenize.hasMoreTokens()) {
                 value.append(" ");
               }
             }
            //创建一个哈希表入口
             stockInfo.put(id,value.toString());
           } catch(NullPointerException excpt) {
             System.err.println("Error creating stock data " +
                                "entry: " + excpt);
           } catch(NoSuchElementException excpt) {
             System.err.println("Invalid stock data record " +
                                "in file: " + excpt);
           }
         }      
         stockInput.close();
         //存放最后的日期时间
         stockFileMod = STOCK_QUOTES_FILE.lastModified();
      } catch(FileNotFoundException excpt) {
         System.err.println("Unable to find file: " + excpt);
         return false;
      } catch(IOException excpt) {
         System.err.println("Failed I/O: " + excpt);
         return false;
      }
      stockInfoTime = new Date();
      return true;
   }

   //等候客户端的连接
   public void serveQuotes() {
      Socket clientSocket = null;
   
      try {
         while(keepRunning) {
            //接受客户端请求
            clientSocket = listenSocket.accept();
            //如果数据文件改变,则重新读取
            if (stockFileMod !=
               STOCK_QUOTES_FILE.lastModified()) {
               loadQuotes();
            }
            //创建一个新的句柄
            StockQuoteHandler newHandler = new
               StockQuoteHandler(clientSocket,stockInfo,
                                 stockInfoTime);
            Thread newHandlerThread = new Thread(newHandler);
            newHandlerThread.start();
         }
         listenSocket.close();
      } catch(IOException excpt) {
         System.err.println("Failed I/O: "+ excpt);
      }
   }
   //停止服务器
   protected void stop() {
      if (keepRunning) {
         keepRunning = false;
      }
   }
}
//管理一个连接到指定的客户端
class StockQuoteHandler implements Runnable {
   private static final boolean AUTOFLUSH = true;
   private Socket mySocket = null;
   private PrintWriter clientSend = null;
   private BufferedReader clientReceive = null;
   private Hashtable stockInfo;
   private Date stockInfoTime;

   public StockQuoteHandler(Socket newSocket,
                            Hashtable info, Date time) {
      mySocket = newSocket;
      stockInfo = info;
      stockInfoTime = time;
   }
   //连接的线程
   public void run() {
      String nextLine;
      StringTokenizer tokens;
      String command;
      String quoteID;
      String quoteResponse;

      try {
         clientSend =
            new PrintWriter(mySocket.getOutputStream(),
                            AUTOFLUSH);
         clientReceive =
            new BufferedReader(new InputStreamReader(
                                  mySocket.getInputStream()));
         clientSend.println("+HELLO "+ stockInfoTime);
         //读取一行,回复客户端
         while((nextLine = clientReceive.readLine())
               != null) {
            tokens = new StringTokenizer(nextLine);
            try {
              command = tokens.nextToken();
              // 退出命令
              if (command.equalsIgnoreCase("QUIT")) break;
              // 股票命令
              else if (command.equalsIgnoreCase("STOCK:")) {
                 quoteID = tokens.nextToken();
                 quoteResponse = getQuote(quoteID);
                 clientSend.println(quoteResponse);
              }
              // 错误命令
              else {
                 clientSend.println("-ERR UNKNOWN COMMAND");
              }
           } catch(NoSuchElementException excpt) {
             clientSend.println("-ERR MALFORMED COMMAND");
           }
         }
         clientSend.println("+BYE");
      } catch(IOException excpt) {
         System.err.println("Failed I/O: " + excpt);
      // 最后,关闭数据和套接字
      } finally {
         try {
            if (clientSend != null) clientSend.close();
            if (clientReceive != null) clientReceive.close();
            if (mySocket != null) mySocket.close();
         } catch(IOException excpt) {
            System.err.println("Failed I/O: " + excpt);
         }
      }
   }

   //得到股票代码是否合法
   protected String getQuote(String quoteID) {
      String info;

      // 确保股票代号大写
      quoteID = quoteID.toUpperCase();
      // 从哈希表中取得股票代号
      info = (String)stockInfo.get(quoteID);
      // 如果哈希表中存在该股票代号,则合法
      if (info != null) {
        return "+" + quoteID + " " + info;
      }
      else {
        // 否则,为非法的股票代号
        return "-ERR UNKNOWN STOCK ID";
      }
   }
}
运行时出现以下错误:
Unable to listen on port 1701: Address already in use: JVM_Bind
搜索更多相关主题的帖子: 大连 private 股票 服务器 import 
2008-04-09 22:38
Eastsun
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:802
专家分:0
注 册:2006-12-14
收藏
得分:0 
把1701这个数改成其它的,比如6556之类

My BlogClick Me
2008-04-10 20:38
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
是不是提示端口已经被用了..换别的试试呢

学习需要安静。。海盗要重新来过。。
2008-04-10 20:48
快速回复:一个小问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026751 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved