当前位置: 技术问答>java相关
那位大虾帮帮忙,帮我把下面这个小程序跑起来!
来源: 互联网 发布时间:2015-02-06
本文导语: //程序如下,摘抄自thinking in java //对原程序的改动主要是将原来的socket改成SSLSocket,将原来的ServerSocket //改成SSLServerSocket,其它没变。 import java.io.*; import java.net.*; import javax.net.ssl.*; class ServeOneJabber extends Thread ...
//程序如下,摘抄自thinking in java
//对原程序的改动主要是将原来的socket改成SSLSocket,将原来的ServerSocket
//改成SSLServerSocket,其它没变。
import java.io.*;
import java.net.*;
import javax.net.ssl.*;
class ServeOneJabber extends Thread {
private SSLSocket socket;
private BufferedReader in;
private PrintWriter out;
public ServeOneJabber (Socket 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){}
}
}
}
public class MultiJabberServer{
static final int PORT = 8083;
public static void main(String [] args) throws IOException{
SSLServerSocketFactory sslSrvFact = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
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();
}
}
}
//编译通过,运行出现下列信息
Server Started
Exception in thread "main" javax.net.ssl.SSLException: No available certificate
corresponds to the SSL cipher suites which are enabled.
at com.sun.net.ssl.internal.ssl.SSLServerSocketImpl.a([DashoPro-V1.2-120
198])
at com.sun.net.ssl.internal.ssl.SSLServerSocketImpl.accept([DashoPro-V1.
2-120198])
at MultiJabberServer.main(MultiJabberServer.java:57)
请问大虾,我该如何让它运行起来?谢谢!
另:我只有32分可送,不过欢迎大家和我联系,我这里软件资源还是很丰富的。
我地Email:liday@263.net
//对原程序的改动主要是将原来的socket改成SSLSocket,将原来的ServerSocket
//改成SSLServerSocket,其它没变。
import java.io.*;
import java.net.*;
import javax.net.ssl.*;
class ServeOneJabber extends Thread {
private SSLSocket socket;
private BufferedReader in;
private PrintWriter out;
public ServeOneJabber (Socket 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){}
}
}
}
public class MultiJabberServer{
static final int PORT = 8083;
public static void main(String [] args) throws IOException{
SSLServerSocketFactory sslSrvFact = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
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();
}
}
}
//编译通过,运行出现下列信息
Server Started
Exception in thread "main" javax.net.ssl.SSLException: No available certificate
corresponds to the SSL cipher suites which are enabled.
at com.sun.net.ssl.internal.ssl.SSLServerSocketImpl.a([DashoPro-V1.2-120
198])
at com.sun.net.ssl.internal.ssl.SSLServerSocketImpl.accept([DashoPro-V1.
2-120198])
at MultiJabberServer.main(MultiJabberServer.java:57)
请问大虾,我该如何让它运行起来?谢谢!
另:我只有32分可送,不过欢迎大家和我联系,我这里软件资源还是很丰富的。
我地Email:liday@263.net
|
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/guidesecurityjssesamplessocketsserver下面把testkey放在你编译的路径下面,就可以了,我已经编译过了,完全没有问题。不行再讨论吧。
另外,你的代码有问题,
public ServeOneJabber (Socket s) throws IOException{
这句话的Socket应改为SSLSocket
呵呵,好了,下次不要再说没有高手了。
我搞过一个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/guidesecurityjssesamplessocketsserver下面把testkey放在你编译的路径下面,就可以了,我已经编译过了,完全没有问题。不行再讨论吧。
另外,你的代码有问题,
public ServeOneJabber (Socket s) throws IOException{
这句话的Socket应改为SSLSocket
呵呵,好了,下次不要再说没有高手了。