gpt4 book ai didi

Java Socket 编程 => 只有 IE9 才能工作?

转载 作者:行者123 更新时间:2023-12-02 07:14:38 27 4
gpt4 key购买 nike

我正在创建一个多线程服务器程序,以在尝试连接到本地主机时显示浏览器的请求。

我发现我的 Windows 上只有 IE9 可以按预期工作,但 Firefox 19、Chrome、Opera 则不能。他们只是坐在那里等待我的程序响应。

我错过了什么?

import java.io.*;
import java.net.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HTTPEchoServer {

private static final String serverName = "HTTPEchoServer";
private static final int port = 80;
private static final String CRLF = "\r\n";
private static final Logger logger = Logger.getLogger(serverName);

private static void printHeader(PrintWriter out) {
out.println("HTTP/1.0 200 OK\r\n" + "Server: " + serverName + CRLF
+ "Content-Type: text/html" + CRLF + CRLF
+ "<!DOCTYPE HTML PUBLIC "
+ "\"-//W3C//DTD HTML 4.0 Transitional//EN\">\n"
+ "<HTML>\n"
+ "<HEAD>\n"
+ " <TITLE>" + "HTTP Echo Server Result</TITLE>\n"
+ "</HEAD>\n"
+ "<H1>HTML Received from HTTP Echo Server</H1>\n"
+ "<B>Here is the request sent by your browser:</B>\n"
+ "<PRE>");
}

private static void printTrailer(PrintWriter out) {
out.println("</PRE>\n" + "</BODY>\n" + "</HTML>\n");
}

static class ClientHandler extends Thread {

Socket socket = null;

public ClientHandler(Socket socket) {
this.socket = socket;
}

@Override
public void run() {

logger.log(Level.INFO, "Accepted client {0}:{1}",
new Object[]{socket.getInetAddress(), socket.getPort()});
try {

InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
try (PrintWriter writer = new PrintWriter(os, true)) {
synchronized (this) {
printHeader(writer);
writer.flush();
BufferedReader reader = new BufferedReader
(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null) {
writer.println(line);
writer.flush();
}
printTrailer(writer);
writer.flush();
writer.close();
}
}
socket.close();
} catch (IOException ex) {
logger.log(Level.SEVERE, null, ex);
}
logger.log(Level.INFO, "Disconnected client {0}:{1}",
new Object[]{socket.getInetAddress(), socket.getPort()});
}
}

public static void main(String[] args) {

try {
ServerSocket server = new ServerSocket(port);
logger.log(Level.INFO, "Server started, listening at port {0} ...", port);
ExecutorService executor = Executors.newCachedThreadPool();
while (true) {
Socket socket = server.accept();
ClientHandler handler = new ClientHandler(socket);
executor.execute(handler);
}
} catch (IOException ex) {
logger.log(Level.SEVERE, null, ex);
}
}
}
<小时/>

此外,我发现除非将端口更改为 8080,否则无法在 OS X 上运行此程序。我已经在 OS X 10.8.2 计算机上禁用了防火墙。我得到的错误是:

<pre>
java.net.BindException: Permission denied
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:376)
at java.net.ServerSocket.bind(ServerSocket.java:376)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at java.net.ServerSocket.<init>(ServerSocket.java:128)
at HTTPEcho.HTTPEchoServer.main(HTTPEchoServer.java:80)
</pre>

最佳答案

您无法在除 MSIE 之外的所有浏览器中的小程序中打开服务器套接字。这样做是因为打开服务器套接字违反安全性。如果您确实想这样做,您必须签署您的小程序。

我希望我正确理解您的代码正在小程序环境中运行(因为您提到了浏览器),尽管我无法从从 main() 开始的堆栈跟踪中看到这个方面。

编辑:我再次阅读了您的帖子,了解到您的问题实际上包含两个问题:第一个关于小程序,第二个关于在 Unix 下作为应用程序运行。对于 unix @Anders R. Bystrup 给了你答案:只有 root 可以监听 1024 以下的端口。因此,你必须以 root 身份运行你的程序或使用 sudo

顺便说一句,您似乎走错了路。您能否解释一下您想要实现什么目标,社区可能可以为您提供更好的解决方案的提示。

关于Java Socket 编程 => 只有 IE9 才能工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15086157/

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