基于java编程实现一个HTTP服务器程序和HTTP客户端程序,要求 采用多线程技术或线程池编程技术处理客户端请求,支持多客户端同时访问
时间: 2024-03-07 09:50:50 浏览: 101
好的,以下是一个简单的Java HTTP服务器程序和HTTP客户端程序的示例,采用线程池编程技术处理客户端请求,并支持多客户端同时访问。
HTTP服务器程序:
```java
import java.io.*;
import java.net.*;
import java.util.concurrent.*;
public class HttpServer {
private static final int PORT = 8080;
private static final int THREAD_POOL_SIZE = 10;
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(PORT);
ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
while (true) {
Socket socket = serverSocket.accept();
executorService.execute(new HttpRequestHandler(socket));
}
}
}
class HttpRequestHandler implements Runnable {
private static final String WEB_ROOT = "www";
private static final String DEFAULT_PAGE = "index.html";
private Socket socket;
public HttpRequestHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = reader.readLine();
if (line == null) {
socket.close();
return;
}
String[] tokens = line.split(" ");
String method = tokens[0];
String url = tokens[1];
String httpVersion = tokens[2];
if (!method.equals("GET")) {
sendError(405, "Method Not Allowed");
return;
}
if (url.equals("/")) {
url += DEFAULT_PAGE;
}
File file = new File(WEB_ROOT, url.substring(1));
if (!file.exists()) {
sendError(404, "File Not Found");
return;
}
String contentType = guessContentType(file.getName());
int contentLength = (int) file.length();
byte[] content = new byte[contentLength];
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(content);
fileInputStream.close();
sendResponse(200, "OK", contentType, content);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void sendResponse(int statusCode, String statusText, String contentType, byte[] content) throws IOException {
OutputStream outputStream = socket.getOutputStream();
PrintWriter writer = new PrintWriter(outputStream);
writer.println("HTTP/1.1 " + statusCode + " " + statusText);
writer.println("Content-Type: " + contentType);
writer.println("Content-Length: " + content.length);
writer.println();
writer.flush();
outputStream.write(content);
outputStream.flush();
}
private void sendError(int statusCode, String statusText) throws IOException {
String message = "<html><head><title>" + statusText + "</title></head><body><h1>" + statusText + "</h1></body></html>";
byte[] content = message.getBytes();
sendResponse(statusCode, statusText, "text/html", content);
}
private String guessContentType(String fileName) {
if (fileName.endsWith(".html") || fileName.endsWith(".htm")) {
return "text/html";
} else if (fileName.endsWith(".css")) {
return "text/css";
} else if (fileName.endsWith(".js")) {
return "text/javascript";
} else if (fileName.endsWith(".png")) {
return "image/png";
} else if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")) {
return "image/jpeg";
} else {
return "application/octet-stream";
}
}
}
```
HTTP客户端程序:
```java
import java.io.*;
import java.net.*;
public class HttpClient {
private static final String HOST = "localhost";
private static final int PORT = 8080;
public static void main(String[] args) throws IOException {
Socket socket = new Socket(HOST, PORT);
OutputStream outputStream = socket.getOutputStream();
PrintWriter writer = new PrintWriter(outputStream);
writer.println("GET / HTTP/1.1");
writer.println("Host: " + HOST);
writer.println();
writer.flush();
InputStream inputStream = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
socket.close();
}
}
```
这个示例程序中,服务器程序会监听8080端口,并使用线程池处理客户端请求。当客户端发出GET请求时,服务器会返回相应的静态文件。客户端程序会连接到服务器的8080端口,并发送GET请求。服务器返回的响应会被输出到控制台。
阅读全文