概述
本文以图示的方式简单介绍了如何使用Java来实现心跳程序,心跳的英文单词是heartbeat. 心跳的目的是当客户端第一次向服务器端发送了请求后,在一定时间内服务器端未能将响应返回到客户端,那么客户端为了继续保持和服务器端的连接,这时客户端就会发送一个心跳到服务器端来维持这种连接,我个人的理解心跳其实也是一种请求,只不过这个请求并不携带要求服务器端要进行处理的信息(个人看法,仅供参考)。
好了,下面我就把客户端和服务器端的程序共享出来供大家参考。说明一下,我采用的是https响应方式,所以我会把详细的步骤呈现出来。我的操作系统是英文的,大家就将就着看吧,不过有截图,看起来也很方便啦!
1. 创建https服务器端所需要的证书。https是一种安全链接协议,所以在服务器端需要一个证书,关于这方面的知识大家可以去参考更为专业和详细的介绍,在这里就简单跳过了。
1.1. startrun
1.2. 在下列界面中输入”cmd”, 点击”Ok”
1.3. 输入创建证书的命令(Windows和Unix系统都适用) keytool -genkey -alias test -keyalg RSA -keystore D:/mykey.store, 这样就会在D:下创建一个名为mykey.store的证书
说明:红框里面的都是要自己输入的,关键是密码,服务器端程序要使用到。
2. 服务器端程序
模式很固定,至于为什么要写成这样,you ask me, I ask whom, you’d better ask Google. 业务逻辑在class MyHandler的public void handle(HttpExchange t) throws IOException中实现。另外实现这个服务器端程序要用到 这是MyEclipse自带的,JDK1.6
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import sun.net.httpserver.HttpsServerImpl;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpsConfigurator;
publicclass HttpsServer {
publicstaticvoid main(String[] args) {
com.sun.net.httpserver.HttpsServer hss;
try {
//Set port as 8000
hss = HttpsServerImpl.create(new InetSocketAddress(8000),0);
//Create certification lib
KeyStore ks = KeyStore.getInstance("JKS");
// Load certification
ks.load(new FileInputStream("D:/key.store" ), "123456".toCharArray());
//Create a KeyManageeFactory
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
//Initialize the KeyManageeFactory
kmf.init(ks, "123456".toCharArray());
//Create certification instance
SSLContext sslContext = SSLContext.getInstance("SSLv3");
//Initialize certification
sslContext.init(kmf.getKeyManagers(), null, null);
//Confighttps
HttpsConfigurator conf = new HttpsConfigurator(sslContext);
//Load configuration in https server
hss.setHttpsConfigurator(conf);
// creates a default executor
hss.setExecutor(null);
// apply MyHandler to process "/" request
hss.createContext("/mytest", new MyHandler());
hss.start();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
}
}
class MyHandler implements HttpHandler{
privateintcount;
publicvoid handle(HttpExchange t) throws IOException {
InputStream is = t.getRequestBody();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String msg = null;
while ((msg = br.readLine()) != null) {
System.out.println("Request from client: " + msg);
}
is.close();
br.close();
count ++;
System.out.println("count: " + count);
String response1 = "first";
String response2 = "second";
t.sendResponseHeaders(200, 200);
OutputStream os = t.getResponseBody();
try {
if (count == 1) {
System.out.println("First send response to client");
Thread.sleep(6000);
os.write(response1.getBytes());
} elseif (count == 2){
System.out.println("Second send response to client");
// Thread.sleep(6000);
Thread.sleep(2000);
os.write(response2.getBytes());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
os.flush();
os.close();
}
}
3. 客户端程序
如果把本机作为服务器,url中的服务器地址理论上可以写成实际IP或127.0.0.1或localhost, 但有时写成实际的IP, 程序会出问题,就不得不写成127.0.0.1或localhost, 至于原因嘛,you ask me, I ask whom, you’d better ask Google, 因为我就是个打酱油的。多看我写的注释,关键代码就是那一句啊
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;
publicclass HttpsClient {
privatestatic String url = "https://127.0.0.1:8000/mytest";
private myX509TrustManager xtm = new myX509TrustManager();
private myHostnameVerifier hnv = new myHostnameVerifier();
public HttpsClient() {
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS"); // 或SSL
X509TrustManager[] xtmArray = new X509TrustManager[] { xtm };
sslContext.init(null, xtmArray, new java.security.SecureRandom());
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
if (sslContext != null) {
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext
.getSocketFactory());
}
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
}
privateintcount = 0;
publicvoid send(String msg) {
count ++;
HttpsURLConnection urlCon = null;
OutputStream os = null;;
InputStream is = null;
BufferedReader br = null;
try {
urlCon = (HttpsURLConnection) new URL(url).openConnection();
urlCon.setDoOutput(true);
urlCon.setRequestProperty("Content-Length", "1024");
urlCon.setUseCaches(false);
urlCon.setDoInput(true);
urlCon.setRequestMethod("POST");
// key point, set response time.
urlCon.setReadTimeout(5000);
os = urlCon.getOutputStream();
os.write(msg.getBytes());
os.flush();
os.close();
is = urlCon.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("Response from server: " + line);
System.exit(0);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
if (count == 1) {
System.out.println("Send heartbeat");
send("Heartbeat");
} elseif (count == 2) {
System.out.println("Time is up");
}
} finally {
urlCon.disconnect();
try {
os.close();
is.close();
br.close();
} catch (IOException e) {}
}
}
publicstaticvoid main(String[] args) {
HttpsClient t = new HttpsClient();
System.out.println("Send request to server");
// try {
// Thread.sleep(6000);
t.send("Hello Server");
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}
class myX509TrustManager implements X509TrustManager {
publicvoid checkClientTrusted(X509Certificate[] chain, String authType) {}
publicvoid checkServerTrusted(X509Certificate[] chain, String authType) {}
public X509Certificate[] getAcceptedIssuers() {
returnnull;
}
}
class myHostnameVerifier implements HostnameVerifier {
publicboolean verify(String hostname, SSLSession session) {
returntrue;
}
}
}
最后
以上就是壮观裙子为你收集整理的图解用Java实现的https心跳程序的全部内容,希望文章能够帮你解决图解用Java实现的https心跳程序所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复