简体   繁体   English

如何用Java实现多线程Web服务器

[英]How to implement a multithreaded web server in Java

My assignment is to implement a multithreaded web server in Java but i have no idea where to start. 我的任务是用Java实现多线程Web服务器,但我不知道从哪里开始。 All we've seen in the lectures is how tcp works,layers etc.but i have no idea about how to implement a web server. 我们在讲座中看到的只是tcp的工作方式,层等。但是我不知道如何实现Web服务器。

Can you give me a starting point? 你能给我一个起点吗?

First of all, you have to make choice about the programming language you want use. 首先,您必须选择要使用的编程语言。 Several languages are particulary useful and more adapted for network programming. 几种语言特别有用,并且更适合于网络编程。 I thinking about Erlang for example. 我以Erlang为例。

You haven't mention which language should you use. 您还没有提到您应该使用哪种语言。 I assumed C/C++. 我假设使用C / C ++。

First, you can look at Boost.Asio library an it's examples . 首先,您可以看一下Boost.Asio库及其示例 There are two HTTP server examples. 有两个HTTP服务器示例。

Second, if you need a rough plan on what to do here it is: 其次,如果您需要在这里做一个粗略的计划,那就是:

  1. Create socket, bind() it on needed address (0.0.0.0 for example), run listen() on it and get ready to accept() connections. 创建套接字,在所需的地址(例如0.0.0.0)上绑定()它,在其上运行listen()并准备好接受()连接。
  2. After accepting new connection, you can recv() data from it, which would be plain text accorfing to HTTP protocol. 接受新的连接后,您可以从中接收数据,该数据将是符合HTTP协议的纯文本。
  3. After determining what client wants you should make up buffer and send() it back. 在确定客户端需要什么之后,您应该组成缓冲区并将send()返回。

Exact function names and overall details are dependent on your programming language. 确切的函​​数名称和总体详细信息取决于您的编程语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM