下面就是一个http请求的例子,其中的各项参数,请查阅相关资料。(http://www.cnblogs.com/yin-jingyu/archive/2011/08/01/2123548.html)
下面就是http回复的例子,除了这个图之外,后面的部分就是大家看到的页面的源代码
import java.net.*; import java.io.*; public class TCPServer { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(6666); //在本机的TCP6666号端口上监听 while(true) { Socket s = ss.accept(); //ServerSocket的accept为阻塞式方法,只有当它监听到一个请求时 //它才会执行 System.out.println("a client connect!"); DataInputStream dis = new DataInputStream(s.getInputStream());//获得客户端向自己说的"话" //InputStream 从外部指向内存 System.out.println(dis.readUTF()); //按照uft-8的格式读取内容 dis.close(); s.close(); } } } import java.net.*; import java.io.*; public class TCPClient { public static void main(String[] args) throws Exception { Socket s = new Socket("127.0.0.1", 6666); //连接127.0.0.1(本机)的tcp端口6666 OutputStream os = s.getOutputStream(); //获得一条线路,来给服务器"说话" DataOutputStream dos = new DataOutputStream(os);//对这个线路进行包装 Thread.sleep(3000); //"暂停"3秒 dos.writeUTF("hello server!"); //对服务器说 hello server! dos.flush(); dos.close(); s.close(); } }
import java.net.Socket; import java.net.ServerSocket; import java.net.InetAddress; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.io.File; public class HttpServer { /** WEB_ROOT is the directory where our HTML and other files reside. * For this package, WEB_ROOT is the "webroot" directory under the working * directory. * The working directory is the location in the file system * from where the java command was invoked. */ public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot"; // shutdown command private static final String SHUTDOWN_COMMAND = "/SHUTDOWN"; // the shutdown command received private boolean shutdown = false; public static void main(String[] args) { HttpServer server = new HttpServer(); server.await(); } public void await() { ServerSocket serverSocket = null; int port = 8080; try { serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1")); //这个1是什么功能 参加 http://www.51cto.com/art/200702/40196_1.htm } catch (IOException e) { e.printStackTrace(); System.exit(1); } // Loop waiting for a request while (!shutdown) { //最开始的时候 shutdown为false 这段话会执行 Socket socket = null; InputStream input = null; OutputStream output = null; try { socket = serverSocket.accept(); //只有当有客户端请求时 它才会运行 阻塞式方法! System.out.println(new Date()+"AAAAAAAAAAAA"); input = socket.getInputStream(); //里面放的是客户端对服务器说的话 output = socket.getOutputStream();//这里面将要放的是服务器要对客户端说的话 // create Request object and parse Request request = new Request(input); request.parse(); //参见request // create Response object Response response = new Response(output); response.setRequest(request); response.sendStaticResource(); // Close the socket socket.close(); //check if the previous URI is a shutdown command shutdown = request.getUri().equals(SHUTDOWN_COMMAND); } catch (Exception e) { e.printStackTrace(); continue; } } } }
import java.io.InputStream; import java.io.IOException; public class Request { private InputStream input; private String uri; public Request(InputStream input) { this.input = input; } public void parse() { // Read a set of characters from the socket StringBuffer request = new StringBuffer(2048); int i; byte[] buffer = new byte[2048]; try { i = input.read(buffer); } catch (IOException e) { e.printStackTrace(); i = -1; } for (int j=0; j<i; j++) { request.append((char) buffer[j]); //将客户端的请求加到request(StringBuffer)中 } System.out.println("request 如下"); System.out.print(request.toString()); uri = parseUri(request.toString()); System.out.println("uri 如下 "+uri); } /** *看看System.out.println("uri 如下 "+uri); 就不用解释这个方法了 * **/ private String parseUri(String requestString) { int index1, index2; index1 = requestString.indexOf(' '); if (index1 != -1) { index2 = requestString.indexOf(' ', index1 + 1); if (index2 。 index1) return requestString.substring(index1 + 1, index2); } return null; } public String getUri() { return uri; } }
import java.io.OutputStream; import java.io.IOException; import java.io.FileInputStream; import java.io.File; /* HTTP Response = Status-Line *(( general-header | response-header | entity-header ) CRLF) CRLF [ message-body ] Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF */ public class Response { private static final int BUFFER_SIZE = 1024; Request request; OutputStream output; public Response(OutputStream output) { this.output = output; } public void setRequest(Request request) { this.request = request; } public void sendStaticResource() throws IOException { byte[] bytes = new byte[BUFFER_SIZE]; FileInputStream fis = null; try { System.out.println(HttpServer.WEB_ROOT+" **** "+request.getUri()); File file = new File(HttpServer.WEB_ROOT, request.getUri()); //连接用户请求的"文件" if (file.exists()) { fis = new FileInputStream(file); int ch = fis.read(bytes, 0, BUFFER_SIZE); //把文件里的东西读出来放到bytes字符数组里 while (ch!=-1) { //把bytes数组里的东西放到要给客户端回复的流里面 output.write(bytes, 0, ch); ch = fis.read(bytes, 0, BUFFER_SIZE); } } else { //要是文件不存在 不解释 // file not found String errorMessage = "HTTP/1.1 404 File Not Found\r\n" + "Content-Type: text/html\r\n" + "Content-Length: 23\r\n" + "\r\n" + "<h1。File Not Found</h1。"; output.write(errorMessage.getBytes()); } } catch (Exception e) { // thrown if cannot instantiate a File object System.out.println(e.toString() ); } finally { if (fis!=null) fis.close(); } } }
显示如下
明白了吧 我们把index.html放到D:\尚学堂 j2ee\javase\尚学堂科技_马士兵_J2SE_5.0_第01章_JAVA简介_源代码_及重要说明\java\Socket\no\WebContent 目录下
再次请求 截图如下
再试试http://localhost:8080/SHUTDOWN
程序退出