一个小问题
请高手看一下: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