大家好,又见面了,我是你们的朋友全栈君。
package client.cfca;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
/**
*
* @author liuxin
* @date 2018年6月24日
*/
public class SslServerTest {
private static String path = "E:/eclipse_work/tomcat-key/tomcat.keystore";
private static char[] password = "tanglei".toCharArray();
/**
* @param args
*/
public static void main(String[] args) throws Exception{
boolean flag = true;
SSLContext context = null;
try {
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(path), password);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, password);
KeyManager[] km = kmf.getKeyManagers();
context = SSLContext.getInstance("SSL");
context.init(km, null, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
SSLServerSocketFactory ssf = (SSLServerSocketFactory) context.getServerSocketFactory();
try {
SSLServerSocket ss = (SSLServerSocket) ssf.createServerSocket(8000);
System.out.println("等待客户点连接。。。");
while (flag) {
Socket s = ss.accept();
System.out.println("接收到客户端连接");
DataInputStream dis=new DataInputStream(s.getInputStream());
dis.readUTF();
System.out.println("-----");
ObjectOutputStream os = new ObjectOutputStream(s.getOutputStream());
os.writeObject("echo : Hello");
os.flush();
os.close();
System.out.println();
s.close();
}
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端:
package client.cfca;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.UnknownHostException;
import java.security.KeyStore;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
/**
*
* @author liuxin
* @date 2018年6月24日
*/
public class TestSSLSocketClient {
private static String path = "E:/eclipse_work/tomcat-key/tomcat.keystore";
private static char[] password = "tanglei".toCharArray();
/**
* @param args
*/
public static void main(String[] args) {
SSLContext context = null;
try {
KeyStore ts = KeyStore.getInstance("JKS");
ts.load(new FileInputStream(path), password);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ts);
TrustManager [] tm = tmf.getTrustManagers();
context = SSLContext.getInstance("SSL");
context.init(null, tm, null);
} catch ( Exception e) { //省略捕获的异常信息
e.printStackTrace();
}
SSLSocketFactory ssf = context.getSocketFactory();
try {
SSLSocket ss = (SSLSocket) ssf.createSocket("127.0.0.1", 8000);
System.out.println("客户端就绪。");
DataOutputStream outputStream=new DataOutputStream(ss.getOutputStream());
// outputStream.write("ww".getBytes());
outputStream.writeUTF("22");
System.out.println("=======");
ObjectOutputStream os = new ObjectOutputStream(ss.getOutputStream());
os.writeObject("echo : Hello");
// ObjectInputStream br = new ObjectInputStream(ss.getInputStream());
System.out.println("客户端测试ok");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106086.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...