我正在尝试创建一个简单的服务器,但是在尝试接受连接时遇到问题,特别是线路" connectionSocket = serverSocket.accept();"。我还有另一个类SimpleHttpHandler,它处理此代码中引用的连接。
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class SimpleHttpServer {
private String rootDirectory;
private int port;
private ServerSocket serverSocket;
private Socket connectionSocket;
public SimpleHttpServer(String rootDirectory, int port) {
this.rootDirectory = rootDirectory;
this.port = port;
}
public void start() {
// Establish the listen socket
try {
serverSocket = new ServerSocket(port);
System.out.println("Server started");
} catch (IOException e1) {
e1.printStackTrace();
}
while (true) {
System.out.println("Inside while");
// Listen for a TCP connection request
try {
connectionSocket = serverSocket.accept();
System.out.println("Client connected");
SimpleHttpHandler simpleHandler = new SimpleHttpHandler(rootDirectory);
simpleHandler.handle(connectionSocket);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SimpleHttpServer server = new SimpleHttpServer(args[0], Integer.parseInt(args[1]));
server.start();
}
}
当我接受新连接时,我需要创建一个处理它的处理程序。
Handler类是:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import org.omg.CORBA.Request;
public class SimpleHttpHandler {
private String rootDirectory;
private StringBuffer readFile;
private FileInputStream fileInputStream;
private File file;
private int b;
public SimpleHttpHandler(String rootDirectory) {
this.rootDirectory = rootDirectory;
}
public void handle(Socket remote) {
try {
// Create in and out streams
BufferedReader in = new BufferedReader(new InputStreamReader(remote.getInputStream()));
PrintStream out = new PrintStream(remote.getOutputStream());
// HTTP requests resolved here based on the protocol
// Read a string line from client
String line = in.readLine();
// Send a string to client
out.println("Not yet implemented");
// Send an empty line to client
out.println();
// Send a byte to client
out.write(123);
// Read a byte from file
file = this.requestFile(rootDirectory, line);
fileInputStream = new FileInputStream(file);
b = fileInputStream.read(); // it returns -1 at end of file
// Read the file
BufferedReader fileReader = new BufferedReader(new FileReader(file));
readFile = null;
while(fileReader.readLine() != null) {
readFile.append(fileReader);
}
if(!file.equals(null)) {
responseMessage(readFile.toString());
} else {
errorMessage();
}
// Close the remote socket and r/w objects
in.close();
out.close();
remote.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void errorMessage() {
System.out.println(readFile);
System.out.println("HTTP/1.0 500 Internal Server Error");
System.out.println();
System.out.println("^_^ Internal Server Error!");
;
}
private void responseMessage(String string) {
System.out.println("HTTP/1.0 200 OK");
System.out.println();
System.out.println("Hello World!");
}
public File requestFile(String rootDirectory, String path) {
// Construct a full path by connecting <rootDirectory> and requested relative path
File file = new File(rootDirectory, path);
// If it is a directory
// Then load the file index.html
if (file.isDirectory()) {
file = new File(file, "index.html");
}
// If the file exists, the file object is returned
// Otherwise null is returned
if (file.exists()) {
return file;
} else {
return null;
}
}
}