?? simpleserver.java
字號:
package com.ssl;
import java.io.*;
import java.net.*;
import java.security.KeyStore;
import javax.net.*;
import javax.net.ssl.*;
import javax.security.cert.X509Certificate;
public class SimpleServer {
public static void main(String args[]) {
int port = Integer.parseInt(args[0]);
String passphrase = args[1];
ServerSocket s;
Socket c;
System.out.println("USAGE: java SimpleServer port passphrase");
try {
System.out.println("creating socket...");
ServerSocketFactory ssf =
SimpleServer.getServerSocketFactory(passphrase);
s = ssf.createServerSocket(port);
System.out.println("waiting for connection...");
c = s.accept();
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
c.getOutputStream())));
BufferedReader in =
new BufferedReader(
new InputStreamReader(c.getInputStream()));
// echo messages from the client
System.out.println("start echo ...");
String line;
// out.println("hello\nhow do you do.");
while ((line = in.readLine()) != null) {
System.out.println("Msg from client is: " + line);
out.println(line);
out.flush();
}
System.out.println("done...");
} catch (IOException e) {
System.out.println("SimpleServer died: " + e.getMessage());
e.printStackTrace();
}
}
private static ServerSocketFactory getServerSocketFactory(String passwd) {
SSLServerSocketFactory ssf = null;
try {
// set up key manager to do server authentication
SSLContext ctx;
KeyManagerFactory kmf;
KeyStore ks;
char[] passphrase = passwd.toCharArray();
ctx = SSLContext.getInstance("TLS");
kmf = KeyManagerFactory.getInstance("SunX509");
//kmf = KeyManagerFactory.getInstance("RSA");
System.out.println(kmf.getAlgorithm());
ks = KeyStore.getInstance("JKS");
// ks.load(new FileInputStream("wahabPrivateStore"), passphrase);
ks.load(new FileInputStream("askey.keystore"), passphrase);
kmf.init(ks, passphrase);
ctx.init(kmf.getKeyManagers(), null, null);
ssf = ctx.getServerSocketFactory();
return ssf;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -