我正在尝试编写一个多线程服务器,它应该能够一次接受多个HTTP请求。
服务器代码:
package test.thread.server;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer implements Runnable {
private int serverPort = 8080;
private ServerSocket serverSocket;
private Thread runningThread;
private boolean isStopped;
public MyServer(int port){
this.serverPort = port;
}
@Override
public void run() {
synchronized (this) {
this.runningThread = Thread.currentThread();
}
openServerSocket();
while(!isStopped){
Socket clientSocket = null;
try {
clientSocket = this.serverSocket.accept();
} catch (IOException e) {
e.printStackTrace();
}
//start a new thread for processing each request
new Thread(new RequestHandler(clientSocket)).start();
}
}
public synchronized void stop(){
this.isStopped = true;
try {
this.serverSocket.close();
} catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}
private void openServerSocket(){
try {
this.serverSocket = new ServerSocket(this.serverPort);
} catch (IOException e) {
e.printStackTrace();
}
}
}
处理请求的工作人员:
它从输入流中读取数据并打印出来。之后它应该进行30秒的睡眠[代表一些不需要CPU的工作]。睡眠时间后,服务器将响应客户端。
package test.thread.server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Date;
public class RequestHandler implements Runnable {
Socket clientSocket;
static int counter = 0;
public RequestHandler(Socket clientSocket){
this.clientSocket = clientSocket;
}
@Override
public void run() {
try {
InputStream input = this.clientSocket.getInputStream();
OutputStream output = this.clientSocket.getOutputStream();
DataInputStream inFromClient = new DataInputStream(input);
System.out.println(new Date()+": " +Thread.currentThread().getName() + " - Started : "+inFromClient.readUTF());
Thread.sleep(30000);
/*output.write(("HTTP/1.1 200 OK\n\n<html><body>" +
"Multi-threaded Server " +
"</body></html>").getBytes());*/
DataOutputStream outFromServer = new DataOutputStream(output);
outFromServer.writeUTF("Output");
outFromServer.flush();
output.close();
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
HTTP客户端:
客户端不会担心服务器休眠时间,并会发送5个连续请求。
package test.thread.server;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
public class MyClient {
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
for(int i=0; i<5; i++){
Socket clientSocket = new Socket("localhost", 8080);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
DataInputStream inFromServer = new DataInputStream(clientSocket.getInputStream());
outToServer.writeUTF("Input");
outToServer.flush();
String output = inFromServer.readUTF();
System.out.println(new Date()+": "+output);
clientSocket.close();
}
}
}
现在我希望服务器在一个线程进入休眠状态时处理下一个客户端请求。但不幸的是,服务器只在处理完上一个请求后才接受下一个请求。
期待o / p:
服务器不应该在打印之前等待当前请求过程完成&#39; Thread-X - 已启动:输入&#39;对于下一个请求。
当前o / p:
Server: Wed Feb 15 18:17:06 IST 2017: Thread-1 - Started : Input
Client: Wed Feb 15 18:17:36 IST 2017: Output
Server:Wed Feb 15 18:17:36 IST 2017: Thread-2 - Started : Input
Client:Wed Feb 15 18:18:06 IST 2017: Output
Server:Wed Feb 15 18:18:06 IST 2017: Thread-3 - Started : Input
Client:Wed Feb 15 18:18:36 IST 2017: Output
Server:Wed Feb 15 18:18:43 IST 2017: Thread-4 - Started : Input
Client:Wed Feb 15 18:19:13 IST 2017: Output
Server:Wed Feb 15 18:19:13 IST 2017: Thread-5 - Started : Input
Client:Wed Feb 15 18:19:43 IST 2017: Output
可能是什么问题?
答案 0 :(得分:3)
我相信问题出在你的客户身上。客户端仅在前一个请求被应答后发送下一个请求。 inFromServer.readUTF()
阻止数据可用,这只是服务器发送实际答案时的情况。
我建议您调试程序。