gpt4 book ai didi

java - 我的简单 java HTTPS 服务器仅适用于本地主机

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:04:10 25 4
gpt4 key购买 nike

我有一个用 Java 实现的简单 HTTPS 测试服务器,如下所示。它接受客户端连接并发回一段关于套接字的文本。问题是它在使用 https://localhost:8888 的浏览器上本地工作,但是当我使用我机器的真实 IP (129.46.xx.xx) 时,它不适用于远程浏览器:8888)。问题出在哪里?测试时,不要忘记生成示例证书并提供以下信息。

编辑:有趣的是,当我在 Ubuntu 12.04 上运行它时,我可以连接到它。但在 Windows 上运行时不是!

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
public class HttpsEchoer {
public static void main(String[] args) {
String ksName = "myks.jks";
char ksPass[] = "mypass".toCharArray();
char ctPass[] = "mypass".toCharArray();
try {
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(ksName), ksPass);
KeyManagerFactory kmf =
KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, ctPass);
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(kmf.getKeyManagers(), null, null);
SSLServerSocketFactory ssf = sc.getServerSocketFactory();
SSLServerSocket s
= (SSLServerSocket) ssf.createServerSocket(8888);
System.out.println("Server started:");
printServerSocketInfo(s);
// Listening to the port
int count = 0;
while (true) {
SSLSocket c = (SSLSocket) s.accept();
// Someone is calling this server
count++;
System.out.println("Connection #: "+count);
printSocketInfo(c);
BufferedWriter w = new BufferedWriter(
new OutputStreamWriter(c.getOutputStream()));
BufferedReader r = new BufferedReader(
new InputStreamReader(c.getInputStream()));
String m = r.readLine();
// System.out.println(m);
if (m!=null) {
// We have a real data connection
w.write("HTTP/1.1 200 OK");
w.newLine();
w.write("Content-Type: text/html");
w.newLine();
w.newLine();
w.write("<html><body><pre>");
w.newLine();
w.write("Connection #: "+count);
w.newLine();
w.newLine();
w.write(m);
w.newLine();
while ((m=r.readLine())!= null) {
if (m.length()==0) break; // End of a GET call
w.write(m);
w.newLine();
}
w.write("</pre></body></html>");
w.newLine();
w.flush();
}
w.close();
r.close();
c.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void printSocketInfo(SSLSocket s) {
System.out.println("Server socket class: "+s.getClass());
System.out.println(" Remote address = "
+s.getInetAddress().toString());
System.out.println(" Remote port = "
+s.getPort());
System.out.println(" Local socket address = "
+s.getLocalSocketAddress().toString());
System.out.println(" Local address = "
+s.getLocalAddress().toString());
System.out.println(" Local port = "
+s.getLocalPort());
}
private static void printServerSocketInfo(SSLServerSocket s) {
System.out.println("Server socket class: "+s.getClass());
System.out.println(" Socker address = "
+s.getInetAddress().toString());
System.out.println(" Socker port = "
+s.getLocalPort());
System.out.println(" Need client authentication = "
+s.getNeedClientAuth());
System.out.println(" Want client authentication = "
+s.getWantClientAuth());
System.out.println(" Use client mode = "
+s.getUseClientMode());
}
}

这是 printServerSocketInfo() 方法显示的内容:

Server started:
Server socket class: class sun.security.ssl.SSLServerSocketImpl
Socker address = 0.0.0.0/0.0.0.0
Socker port = 8888
Need client authentication = false
Want client authentication = false
Use client mode = false

这是本地浏览器显示的内容:

Connection #: 3

GET / HTTP/1.1
Host: localhost:8888
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36 OPR/22.0.1471.50 (Edition Campaign 38)
Accept-Encoding: gzip,deflate,lzma,sdch
Accept-Language: en-US,en;q=0.8

最佳答案

尝试像这样创建服务器套接字:

SSLServerSocket s = (SSLServerSocket) ssf.createServerSocket(8888, 0, null);

作为最后一个参数的 null 应该告诉服务器套接字工厂创建一个绑定(bind)到所有网络接口(interface)的套接字。

这是一个 link to the javadoc对于该方法的特定版本。

关于java - 我的简单 java HTTPS 服务器仅适用于本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24172449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com