ft,高手有的是,只是没看到而已,所谓术业有专攻吗,呵呵。我搞过一个JSSE的FTP Server所以知道一些,你的问题很明白,就是那个安全加密证书没配对。我用的是jdk1.4beta2,把你那个改成了两个文件。你试一下。 //ServeOneJabber.java import java.io.*; import java.net.*; import java.security.KeyStore; import javax.net.*; import javax.net.ssl.*; import javax.security.cert.X509Certificate;class ServeOneJabber extends Thread { private SSLSocket socket; private BufferedReader in; private PrintWriter out;public ServeOneJabber (SSLSocket s) throws IOException{ socket = s; in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //Enable auto_flush; out = new PrintWriter(new BufferedWriter(new OutputStreamWriter( socket.getOutputStream())),true); //if any of the above calls throw an exception,the caller is //responsible for closing the socket.Otherwise the thread will close it; start();//calls run() }public void run(){ // System.out.println("runing" + id); try{ while(true){ String str = in.readLine(); if(str.equals("END")) break; System.out.println("Echoing:"+str); out.println(str); out.flush(); } System.out.println("closing..."); }catch (IOException e){ }finally{ try{ socket.close(); }catch(IOException e){} } } } //MultiJabberServer.java import java.io.*; import java.net.*; import java.security.KeyStore; import javax.net.*; import javax.net.ssl.*; import javax.security.cert.X509Certificate;public class MultiJabberServer{ static final int PORT = 8083; public static void main(String [] args) throws IOException{
SSLServerSocketFactory sslSrvFact = (SSLServerSocketFactory)MultiJabberServer.getServerSocketFactory("TLS"); SSLServerSocket s=(SSLServerSocket)sslSrvFact.createServerSocket(PORT); System.out.println("Server Started"); try{ while(true){ //Blocks until a connection occurs: SSLSocket socket = (SSLSocket)s.accept(); try{ new ServeOneJabber(socket); }catch(IOException e){ //if it fails,close the socket,otherwise the thread will close it. socket.close(); } } }finally{ s.close(); } } private static ServerSocketFactory getServerSocketFactory(String type) { if (type.equals("TLS")) { SSLServerSocketFactory ssf = null; try { // set up key manager to do server authentication SSLContext ctx; KeyManagerFactory kmf; KeyStore ks; char[] passphrase = "passphrase".toCharArray(); ctx = SSLContext.getInstance("TLS"); kmf = KeyManagerFactory.getInstance("SunX509"); ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream("testkeys"), passphrase); kmf.init(ks, passphrase); ctx.init(kmf.getKeyManagers(), null, null); ssf = ctx.getServerSocketFactory(); return ssf; } catch (Exception e) { e.printStackTrace(); } } else { return ServerSocketFactory.getDefault(); } return null; } } 你那个主要的问题,就是没给那个Certificate,你也不用造了,在Jdk1.4/docs/guide\security\jsse\samples\sockets\server下面把testkey放在你编译的路径下面,就可以了,我已经编译过了,完全没有问题。不行再讨论吧。另外,你的代码有问题,public ServeOneJabber (Socket s) throws IOException{这句话的Socket应改为SSLSocket呵呵,好了,下次不要再说没有高手了。
to Yang_Sun(Tsinghua_Worm): 非常感谢您!俺又“改造”了一个客户端,就是Thinking in java中的对应的客户端的例子。 只是将其中的socket改成SSLSocket,具体改动有四行,见下面打*得行。 但是好像和上面大虾您改动过的server端连接不上,不知是何原因? 是不是我用的SSLSocketFactory.getDefault()和server端不匹配? 另外server端程序中的TLS是什么意思? 分数俺已经发给您了,您再回答俺的问题也没什么可送的了。 不过俺上面留了一个mail,如果您需要什么软件或其它什么地,就给俺发mail,俺一定鼎立相助! import java.net.*; import java.io.*; *import javax.net.ssl.*;class JabberClientThread extends Thread{ * private SSLSocket socket; private BufferedReader in; private PrintWriter out; private static int counter = 0; private int id = counter++; private static int threadcount = 0;
public static int threadCount(){ return threadcount; }
public JabberClientThread(InetAddress addr){ System.out.println("Making client " + id); threadcount++; try{ * SSLSocketFactory sslFact = (SSLSocketFactory)SSLSocketFactory.getDefault(); * socket = (SSLSocket)sslFact.createSocket(addr, MultiJabberServer.PORT); //Socket(addr, MultiJabberServer.PORT); }catch(IOException e){ //if the creation of the socket fails,nothing needs to be cleaned up } try{ in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //Enable auto_flush out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))); start(); }catch(IOException e){ //the socket should be closed on any failure other than the socket constructor try{ socket.close(); }catch(IOException ex){} } // otherwise the socket will be closed by the run() method of the thread }
public void run(){ try{ for(int i = 0; i < 25; i++){ out.println("Client " + id + ": " + i); String str = in.readLine(); System.out.println(str); } out.println("END"); }catch(IOException e){ }finally{ //always close it: try{ socket.close(); }catch(IOException e){} threadcount--;//ending this thread } } }public class MultiJabberClient{ static final int MAX_THREADS = 40; public static void main(String [] args) throws IOException,InterruptedException { InetAddress addr = InetAddress.getByName(null); while(true){ if(JabberClientThread.threadCount() < MAX_THREADS) new JabberClientThread(addr); Thread.currentThread().sleep(100); } } }再次感谢您!
原来的例子没问题,只是俺将它搞成了一个sslsocket就不行了.
其实这个问题不难,只要做过数据的安全传输之类的项目,这个应该是very easy!
不知是我得分数给的少(俺就这么多分全给了)?
还是我的问题问的傻?
还是我的问题回答起来好麻烦?
还是java版上真正的高手太少?
给我个答案先!
//ServeOneJabber.java
import java.io.*;
import java.net.*;
import java.security.KeyStore;
import javax.net.*;
import javax.net.ssl.*;
import javax.security.cert.X509Certificate;class ServeOneJabber extends Thread {
private SSLSocket socket;
private BufferedReader in;
private PrintWriter out;public ServeOneJabber (SSLSocket s) throws IOException{
socket = s;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//Enable auto_flush;
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())),true);
//if any of the above calls throw an exception,the caller is
//responsible for closing the socket.Otherwise the thread will close it;
start();//calls run()
}public void run(){
// System.out.println("runing" + id);
try{
while(true){
String str = in.readLine();
if(str.equals("END")) break;
System.out.println("Echoing:"+str);
out.println(str);
out.flush();
}
System.out.println("closing...");
}catch (IOException e){
}finally{
try{
socket.close();
}catch(IOException e){}
}
}
}
//MultiJabberServer.java
import java.io.*;
import java.net.*;
import java.security.KeyStore;
import javax.net.*;
import javax.net.ssl.*;
import javax.security.cert.X509Certificate;public class MultiJabberServer{
static final int PORT = 8083;
public static void main(String [] args) throws IOException{
SSLServerSocketFactory sslSrvFact = (SSLServerSocketFactory)MultiJabberServer.getServerSocketFactory("TLS");
SSLServerSocket s=(SSLServerSocket)sslSrvFact.createServerSocket(PORT);
System.out.println("Server Started");
try{
while(true){
//Blocks until a connection occurs:
SSLSocket socket = (SSLSocket)s.accept();
try{
new ServeOneJabber(socket);
}catch(IOException e){
//if it fails,close the socket,otherwise the thread will close it.
socket.close();
}
}
}finally{
s.close();
}
} private static ServerSocketFactory getServerSocketFactory(String type) {
if (type.equals("TLS")) {
SSLServerSocketFactory ssf = null;
try {
// set up key manager to do server authentication
SSLContext ctx;
KeyManagerFactory kmf;
KeyStore ks;
char[] passphrase = "passphrase".toCharArray(); ctx = SSLContext.getInstance("TLS");
kmf = KeyManagerFactory.getInstance("SunX509");
ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream("testkeys"), passphrase);
kmf.init(ks, passphrase);
ctx.init(kmf.getKeyManagers(), null, null); ssf = ctx.getServerSocketFactory();
return ssf;
} catch (Exception e) {
e.printStackTrace();
}
} else {
return ServerSocketFactory.getDefault();
}
return null;
}
}
你那个主要的问题,就是没给那个Certificate,你也不用造了,在Jdk1.4/docs/guide\security\jsse\samples\sockets\server下面把testkey放在你编译的路径下面,就可以了,我已经编译过了,完全没有问题。不行再讨论吧。另外,你的代码有问题,public ServeOneJabber (Socket s) throws IOException{这句话的Socket应改为SSLSocket呵呵,好了,下次不要再说没有高手了。
非常感谢您!俺又“改造”了一个客户端,就是Thinking in java中的对应的客户端的例子。
只是将其中的socket改成SSLSocket,具体改动有四行,见下面打*得行。
但是好像和上面大虾您改动过的server端连接不上,不知是何原因?
是不是我用的SSLSocketFactory.getDefault()和server端不匹配?
另外server端程序中的TLS是什么意思?
分数俺已经发给您了,您再回答俺的问题也没什么可送的了。
不过俺上面留了一个mail,如果您需要什么软件或其它什么地,就给俺发mail,俺一定鼎立相助!
import java.net.*;
import java.io.*;
*import javax.net.ssl.*;class JabberClientThread extends Thread{
* private SSLSocket socket;
private BufferedReader in;
private PrintWriter out;
private static int counter = 0;
private int id = counter++;
private static int threadcount = 0;
public static int threadCount(){
return threadcount;
}
public JabberClientThread(InetAddress addr){
System.out.println("Making client " + id);
threadcount++;
try{
* SSLSocketFactory sslFact = (SSLSocketFactory)SSLSocketFactory.getDefault();
* socket = (SSLSocket)sslFact.createSocket(addr, MultiJabberServer.PORT);
//Socket(addr, MultiJabberServer.PORT);
}catch(IOException e){
//if the creation of the socket fails,nothing needs to be cleaned up
}
try{
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//Enable auto_flush
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
start();
}catch(IOException e){
//the socket should be closed on any failure other than the socket constructor
try{
socket.close();
}catch(IOException ex){}
}
// otherwise the socket will be closed by the run() method of the thread
}
public void run(){
try{
for(int i = 0; i < 25; i++){
out.println("Client " + id + ": " + i);
String str = in.readLine();
System.out.println(str);
}
out.println("END");
}catch(IOException e){
}finally{
//always close it:
try{
socket.close();
}catch(IOException e){}
threadcount--;//ending this thread
}
}
}public class MultiJabberClient{
static final int MAX_THREADS = 40;
public static void main(String [] args) throws IOException,InterruptedException {
InetAddress addr = InetAddress.getByName(null);
while(true){
if(JabberClientThread.threadCount() < MAX_THREADS)
new JabberClientThread(addr);
Thread.currentThread().sleep(100);
}
}
}再次感谢您!