socketjava web服务器程序
我们的目标是开发一个web服务器,当通过web浏览器访问该服务器时,它将返回接收到的HTTP请求消息
我从虚拟机环境运行程序,并从本地机器访问服务器。下面是我的代码。当我在服务器运行时输入IP地址时,我只会得到一个空指针异常。任何指导都将不胜感激
import java.io.*;
import java.net.*;
import java.util.*;
public final class proj1 {
public static void main(String[] args) throws Exception{
int port = 9000;
ServerSocket socket = new ServerSocket(port);
while(true) {
Socket connection = socket.accept();
HttpRequest request = new HttpRequest(connection);
Thread thread = new Thread(request);
thread.start();
}
}
}
final class HttpRequest implements Runnable {
//Declare Variables
final static String CRLF = "\r\n";
Socket socket;
//Constructor
public HttpRequest(Socket socket) throws Exception {
this.socket = socket;
}
//Unimplemented Runnable Method
@Override
public void run() {
try {
processRequest();
} catch (Exception e) {
System.out.println(e);
}
}
private void processRequest() throws Exception {
//Reference Socket Input and Output Streams
InputStream is = socket.getInputStream();
DataOutputStream os = null;
//Input Stream Filters
BufferedReader br = new BufferedReader(null);
//Request Line of HTTP Request Message
String requestLine = br.readLine();
//Extract FileName From Request Line
StringTokenizer tokens = new StringTokenizer(requestLine);
tokens.nextToken();
String fileName = tokens.nextToken();
fileName = "." + fileName;
//Open Requested File
FileInputStream fis = null;
boolean fileExists = true;
try {
fis = new FileInputStream(fileName);
} catch (FileNotFoundException e) {
fileExists = false;
}
//Debug
System.out.println(requestLine);
String headerLine = null;
while ((headerLine = br.readLine()).length() != 0) {
System.out.println(headerLine);
}
//Construct Response Message
String statusLine = null;
String contentTypeLine = null;
String entityBody = null;
if (fileExists) {
statusLine = "statusLine";
contentTypeLine = "Content-Type: " + contentType(fileName) + CRLF;
} else {
statusLine = "HTTP/1.0 404 Not Found" + CRLF;
contentTypeLine = "Content-Type: text/html" + CRLF;
entityBody = "<HTML>" + "<HEAD><TITLE>Not Found</TITLE></HEAD>" + "<BODY>Not Found</BODY></HTML>";
}
//Send Lines
os.writeBytes(statusLine);
os.writeBytes(contentTypeLine);
os.writeBytes(CRLF);
//Send Entity Body
if (fileExists) {
sendBytes(fis, os);
fis.close();
} else {
os.writeBytes(entityBody);
}
//Close Streams and Sockets
os.close();
br.close();
socket.close();
}
private static void sendBytes(FileInputStream fis, OutputStream os) throws Exception {
byte[] buffer = new byte[1024];
int bytes = 0;
while ((bytes = fis.read(buffer)) != -1) {
os.write(buffer, 0, bytes);
}
}
private static String contentType(String fileName) {
if (fileName.endsWith(".htm") || fileName.endsWith(".html")) {
return "text/html";
}
return "application/octet-stream";
}
}
Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at proj1.main(proj1.java:10)
这是我试图在eclipse中运行程序时的错误代码。。。当我在我的虚拟机上运行代码时,它运行正常,但每当我访问我机器上服务器的IP地址时,我在虚拟机上都不会收到响应消息和空指针异常
# 1 楼答案
new BufferedReader(null)
将始终throw NullPointerException
。你除了什么顺便说一句,这总是个坏主意:
如果不知道如何处理异常,至少重新抛出它以保留堆栈跟踪:
这将引导您到达抛出异常的那一行