We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
如果您将此作为练习,我建议您使用事件驱动模型。
我认为没有关于这个主题的教程,因为所需的知识范围非常广泛 - HTTP 协议、文件访问、线程和并发、配置文件管理、套接字通信、日志记录、错误处理、MIME 类型......是的,即使只是共享静态资源,它仍然是一个大问题。
阅读一下,祝你好运!
If you're doing this as an exercise, I'd recommend an event-driven model.
I don't think there's one tutorial on this topic because the knowledge required is so far-ranging - the HTTP protocol, file access, threading and concurrency, configuration-file management, socket communication, logging, error handling, MIME types... Yeah, even just sharing static resources, it's still a biggie.
Read up, and good luck!
我认为这就是你想要的
http://java.sun.com/developer/technicalArticles/Networking/Webserver/< /a>
I think that this is what you want
http://java.sun.com/developer/technicalArticles/Networking/Webserver/
我建议您熟悉 HTTP 请求格式 http://datatracker.ietf.org/doc/rfc2616/ 。从头开始实现 HTTP 并不是一件小事,但这无疑是一个很好的学习练习。
在 Java 本身中,为了简单起见,我建议使用每个请求一个线程的服务器 - http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html - 使用
java.nio
提供文件。在并发设置中,java.nio
优于java.io
,因为它可以更好地平衡负载。您可能会发现基准表明java.io
更快,但那是针对顺序单线程代码的。I recommend familiarizing yourself with the HTTP request format http://datatracker.ietf.org/doc/rfc2616/. Implementing HTTP from scratch is no small feat, but it is certainly a good learning exercise.
Within Java itself for simplicity I recommend using a thread-per-request server - http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html - that using
java.nio
for serving files. In a concurrent settingjava.nio
is preferable tojava.io
because it balances load better. You will likely find benchmarks that suggest thatjava.io
is faster, but that is for sequential single-threaded code.您可能会发现 ACME Web 服务器作为起点很有趣。我们用它来进行临时文件传输。当您熟悉它后,您可以看看是否可以发现它的瓶颈,然后思考如何修复它:)
http://acme.com/java/software/Acme.Serve.Serve.html
You might find the ACME web server interesting as a starting point. We use it for ad-hoc file transfers. When you have familarized yourself with it, you can see if you can discover its bottlenecks and then ponder on how to fix it :)
http://acme.com/java/software/Acme.Serve.Serve.html
我建议 Apache MINA 来完成这项工作。它允许您指定编码器/解码器来正确处理 HTT 协议,并在请求到达时调用方法等。它在内部处理线程管理。没有它我永远不会开始一个网络项目。
I would suggest Apache MINA to do the job. It lets you specify Encoders/Decoders to deal with the HTT Protocol properly and calls a method when a request arrives etc. It handles thread management internally. I never start a networking project without it.