我已經實現了以下代碼來創建SSL服務器套接字。重複使用同一個服務器的問題Diskocket
public void createConnection(){
port =8888;
listen = true;
isSvrRuning = true;
try {
String KEYSTORE = Config.KEYSTORE_FILE;
// String KEYSTORE = "/u04/app/ato/data/keystore/ATradSvrKeyStore";
char[] KEYSTOREPW = "abcd".toCharArray();
char[] KEYPW = "abcd".toCharArray();
com.sun.net.ssl.TrustManagerFactory tmf;
boolean requireClientAuthentication;
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
java.security.KeyStore keystore = java.security.KeyStore.getInstance(
"JKS");
keystore.load(new FileInputStream(KEYSTORE), KEYSTOREPW);
com.sun.net.ssl.KeyManagerFactory kmf = com.sun.net.ssl.KeyManagerFactory.getInstance("SunX509");
kmf.init(keystore, KEYPW);
com.sun.net.ssl.SSLContext sslc = com.sun.net.ssl.SSLContext.getInstance("SSLv3");
tmf = com.sun.net.ssl.TrustManagerFactory.getInstance("sunx509");
tmf.init(keystore);
sslc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
SSLServerSocketFactory ssf = sslc.getServerSocketFactory();
ssocket = (SSLServerSocket) ssf.createServerSocket();
try {
ssocket.setReuseAddress(true);
} catch (SocketException ex) {
loggerSvr.log(Level.SEVERE, "SocketException in setting timeout for serverSocket.");
ex.printStackTrace();
}
ssocket.bind(new InetSocketAddress(port));
ssocket.setNeedClientAuth(true);
while (listen) {
//wait for client to connect//
socket = ssocket.accept();
// here goes the code which need to handle a new connection
}
}catch(){
//there are several catch blocks to catch all checked exceptions
}
}
如果在上述插座有問題或例外,我關閉套接字,並通過調用上述方法重新創建另一臺服務器插槽。但問題是我有一段時間無法使用相同的端口重新創建服務器套接字。當我嘗試去做時會出現以下錯誤。
Server socket allocation error for the specific port
我也在創建socket.can時設置了可重用屬性有人告訴問題是什麼?
謝謝您的評論EJP.As你說的問題不在這個代碼中。最初實現上述代碼的人並沒有暴露出真正的異常。所以我也無法弄清楚最初的實際情況。實際的問題是其他地方。下次我將確保指出實際的錯誤。我還注意到上面代碼中使用的大多數方法都已被棄用。你能告訴我,我對上述代碼有什麼樣的風險。再次感謝您的意見 – nath 2010-12-06 06:13:44