代码如下:
/**
* This is a test program for Echo Server. @author Hu Dennis created at 2008-11-12
*/
import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
public class HttpServer {
public static void main(String[] args) {
int listenPort = 8080;
ServerSocket serverSocket = null;
Socket serviceSocket = null;
DataInputStream inBound = null;
PrintStream outBound = null;
try {
serverSocket = new ServerSocket(listenPort);
serviceSocket = serverSocket.accept();
inBound = new DataInputStream(serviceSocket.getInputStream());
outBound = new PrintStream(serviceSocket.getOutputStream());
BufferedReader bfReader = new BufferedReader(new InputStreamReader(inBound));
String line = null;
while (true){
/* The request must contains a line separator.
* eg.
* StringBuilder request = new StringBuilder();
* request.append("Echo\n");
*/
line = bfReader.readLine();
outBound.println(line);
}
}catch (IOException e){
System.out.println(e);
return ;
}
}
}
3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.com/exam/