- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我已经编写了一个非常简单、相当低级别的 HTTP(好吧,HTTP 的一部分)服务器作为练习,以更加熟悉我一直在避免的整个 Web 事物。它在第一次尝试时工作得相当好(不是我会推荐任何人实际使用它,但它会按照我的要求去做)。现在的问题是 GET 操作经常失败(刷新有帮助,但不是很好 - 详情如下),我认为这是因为我读取请求的方式(我相当确定我的路由有效):
void start()
{
//...
try(ServerSocket webSock = new ServerSocket(47000) {
//...
while(running) {
try {
Socket sock = webSock.accept();
//read/write from/to sock
}
//...
Thread.sleep(10);
}
}
}
(完整代码在这里:http://pastebin.com/5B1ZuusH)
虽然我不确定什么我做错了。
我确实得到了错误:
This webpage is not available
The webpage at http://localhost:47000/ might be temporarily down or it may have moved permanently to a new web address.
Error 15 (net::ERR_SOCKET_NOT_CONNECTED): Unknown error.
相当多(整个页面未加载),有时脚本或图像也未加载。如果需要,我可以发布整个代码,但其余大部分是样板文件。
最佳答案
[另一个更新]
好吧,澄清一下我的回答,这是一个简单的网络服务器,展示了如何读取 GET 请求。请注意,它在同一个连接中处理多个请求。如果连接关闭,程序将退出。通常,在连接关闭和程序退出之前,我可以从同一个 Web 浏览器发送多个请求。这意味着您不能使用流结束作为消息结束的信号。
请注意,我从不使用手写的网络服务器来做任何真实的事情。我最喜欢的是 Tomcat,但其他框架也很好。
public class MyWebServer
{
public static void main(String[] args) throws Exception
{
ServerSocket server = new ServerSocket(47000);
Socket conn = server.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
// don't use buffered writer because we need to write both "text" and "binary"
OutputStream out = conn.getOutputStream();
int count = 0;
while (true)
{
count++;
String line = reader.readLine();
if (line == null)
{
System.out.println("Connection closed");
break;
}
System.out.println("" + count + ": " + line);
if (line.equals(""))
{
System.out.println("Writing response...");
// need to construct response bytes first
byte [] response = "<html><body>Hello World</body></html>".getBytes("ASCII");
String statusLine = "HTTP/1.1 200 OK\r\n";
out.write(statusLine.getBytes("ASCII"));
String contentLength = "Content-Length: " + response.length + "\r\n";
out.write(contentLength.getBytes("ASCII"));
// signal end of headers
out.write( "\r\n".getBytes("ASCII"));
// write actual response and flush
out.write(response);
out.flush();
}
}
}
}
[原始响应]
What is the proper way to listen to HTTP requests?
对于我们大多数人来说,正确的方法是使用设计良好的 Web 服务器框架,例如 Tomcat , Jetty , 或 Netty
as an exercise to get more familiar with this whole web thing
但是,如果这是学习 HTTP 的学术练习,那么首先要做的是研究 HTTP 协议(protocol)(参见 http://www.w3.org/Protocols/rfc2616/rfc2616.html)。我很确定你没有这样做,因为你的代码没有尝试识别起始行、标题等来确定 GET 请求何时完成以及发送响应是有意义的。
[更新]
很酷。您已经了解了 TCP 如何面向流并且不保留消息边界。是的,应用程序必须处理它。这是最后一个想法 - 如果您使用 readLine
读取起始行和标题,您可能会让您的实验相当可靠地工作 - 只有 GET 请求请注意。当你得到一个空行时,请求就完成了。这将导致缓冲阅读器在正确的时间阻塞,以便您获得所有内容。
这不适用于 POST 等,因为您随后需要解析 Content-Length header 并读取一些字节数。
希望当您意识到正确和可靠地执行此操作涉及多少内容时,这个实验会让您更加欣赏 Jetty - 所以我认为这是一项值得的努力。
关于java - 监听 HTTP 请求的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13551803/
我遇到了一个奇怪的问题,我以前从未真正体验过这个,这是服务器的代码(在这种情况下客户端是 firefox),我创建它的方式: _Socket = new Socket( AddressFamily.I
我正在使用 C 编程语言在 Ubuntu 中开发原始套接字程序。由于我使用原始套接字,因此需要使用 SOCK_RAW 类型而不是 SOCK_STREAM。使用 SOCK_RAW 反过来会通过抛出 来禁
我实际上在客户端-客户端应用程序方面遇到了麻烦。这个问题中的一切都与Unix网络编程环境有关。 这是我的情况: 我有一个客户端(从现在起称为 C1),它调用 listen()在套接字上。 C1 输入
是否可以监听 QTcpSocket?我在 Tcp 上有一个简单的 p2p 连接。但是我找不到在随机自由端口上启动 QTcpSocket 的方法。我应该为此使用 QTcpServer,还是仅对 1 个连
我正在尝试根据 this documentation 监听码头更改事件和 ACTION_DOCK_EVENT . 我的接收器从未被击中。 我的代码看起来很简单,所以我想知道监听停靠事件是否需要权限?我
在 Linux 3.9 内核和更高版本中运行,我有一个应用程序 X,它在特定套接字上监听连接。我想写一个不相关的应用程序 Y,它跟踪尝试连接到此套接字的次数、源 IP 等。 是否可以在 c++ 中(最
是否可以监听日志条目?即是否存在用于附加日志条目的广播 Intent ? 最佳答案 没有 Intent 。 使用下面的代码: try { Process mLogcatProc
我已经实现了 installTap 方法,它为我提供了音频缓冲区浮点示例。我已经通过我的 C++ DSP 库过滤了它们。我想将此缓冲区“发送”到耳机/扬声器。我从示例中再次执行了 AVAudioPCM
我正在尝试启动一个在后台运行的服务,该服务正在监听 ACTION_SCREEN_OFF,当它找到 ACTION_SCREEN_OFF 时,启动我的 Activity 。 我在某处读到您需要创建一个Br
我正在开发一个 HOC,以帮助处理我的 React 应用程序中的表单(用于练习)。 // components/Wrapper.js import React from 'react'; export
我有“服务器端”mqtt 客户端,用于监视和管理远程 mqtt 客户端。我想扩展此服务器模块以密切关注远程客户端的连接。 在正常操作期间,远程客户端定期 PING 代理,根据代理日志: 1532924
我正在编写一个检查电池容量的守护进程。这是用于运行 Linux 的太阳能嵌入式设备。我读过使用 sleep() 是个坏主意在守护进程中,因此我正在尝试使用事件。所以我写了一些 PoC,但我没有收到任何
是否有一个库或技术来监听 Swing ui 对象上的所有可变事件?具体数据。 例如,我有一个带有 JTextArea、JCheckBox、JComboBox 等的 JPanel。有没有一种通用的方法可
使用 KineticJS,是否可以仅绑定(bind)函数一次?就像 jQuery 的等价物一样......例如。在 jQuery 中 // bad $('.wrap a').on('click', m
我正在使用 jquery 制作一个非常简单的富文本编辑器...我不想使用第三方的。 我需要监听 iframe(同一域等)内的事件,从输入开始。显然我需要经常使用bind()。 这就是我目前所拥有的,它
我有一个函数可以获取 XML 文档并根据 XSL 文档对其进行转换。然后,它将结果放入 id 为 laneconfigdisplay 的 div 中。我想要做的是,与转换逻辑分开,为该 div 设置一
如何检测 NSTextView 中的一行(即“\n”字符)被删除?我可以使用 textView:doCommandBySelector: 轻松监听新行并监听 "insertNewLine:"。 (参见
我有以下代码订阅 VisiblePosition 的属性更改事件Column 的属性(property)类(class): DependencyPropertyDescriptor dpd = Dep
我正在尝试在本地主机上收听浏览器的 DNS 请求。 我写了这段代码: WSADATA wsaData; unsigned char hostname[100]; int sockfd; struct
如何以 JSON 对象的形式接收对我网站上特定页面的回调?这些回调是从第三方 API 发出的,用于报告我与该 API 的通信状态。 我想过使用node.js的http.server.listen,但我
我是一名优秀的程序员,十分优秀!