1、); catch (UnknownHostException e) e.printStackTrace(); System.exit(-1); catch (IOException e) /若请求的命令不为SHUTDOWN时,循环处理请求 while(!shutdown) Socket socket = null; InputStream input = null; OutputStream output = null; try /创建socket进行请求处理 socket = serverSocket.accept(); input = socket.getInputStream(); ou
2、tput = socket.getOutputStream(); /接收请求 Request request = new Request(input); request.parser(); /处理请求并返回结果 Response response = new Response(output); response.setRequest(request); response.sendStaticResource(); /关闭socket socket.close(); /若请求命令为关闭,则关闭服务器 shutdown= request.getUri().equals(SHUTDOWN_COMMA
3、ND); catch (IOException e) e.printStackTrace(); continue; 该类本身是一个应用程序,包含main方法,直接通过java命令即可运行。在运行时,它本身会启动一个ServerSocket类用于监听服务器的某个端口。当接收到的命令不是停止服务器的SHUTDOWN时,它会创建一个Socket套接字,用于接收请求及返回响应结果。 Request类则用于请求的接收,对于Http协议来讲,通过浏览器向服务器发送请求有一定的格式,其实Request也就是接收这些请求信息,并对其进行分析,抽取出所需的信息,包括cookie、url等。其中http发送的请求
4、包括三部分:请求方法 统一资源标识符 协议/版本请求头请求实体其中请求头与请求实体间有一个空行,具体的示例代码如下所示:GET /index.htm HTTP/1.1 Host: Connection: keep-alive User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 A
5、ccept-Encoding: gzip,deflate,sdch Accept-Language: zh-CN,zh;q=0.8 Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3 Cookie: BAIDUID=D275C16E04D9BB2CF55FD9B9654AECAC:FG=1; BDREFER=%7Burl%3A%22http%3A/ BDUT=5b2fD275C16E04D9BB2CF55FD9B9654AECAC138a495329b1; MCITY=-%3A; shifen3113720932=1357565900; H_PS_PSSID=144
6、5_1661 Request类用于接收socket套接字发送过来的字节流,并按照http协议请求的格式进行解析。对于简单的web服务器而言,我们只需要解析出它的uri即可,这样即可以通过通过文件匹配的方式找到对应的资源。具体的实现代码如下所示: *接收到的请求串的具体格式如下: * GET /aaa.htm HTTP/1.1 * Host: 127.0.0.1:8080 * Connection: keep-alive * User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome
7、/23.0.1271.97 Safari/537.11 * Accept:q=0.9,q=0.8 * Accept-Encoding: gzip,deflate,sdch * Accept-Language:q=0.8 * Accept-Charset:q=0.3 * public class Request private InputStream input; private String uri; public Request(InputStream input) this.input = input; public void parser() StringBuffer request =
8、 new StringBuffer(); byte buffer = new byte2048; int i = 0; try i = input.read(buffer); i = -1; for(int k = 0; k index1) return requestData.substring(index1 + 1, index2); return null; public String getUri() return uri; 通过上述代码可以看出parser方法用于解析具体请求,它接收socket的字节流,对其进行处理,获取其中的uri信息。 当获取到uri信息后,即可将uri对应到服
9、务器的某个应用或目录中。本文只是实现了一个简单的静态资源服务器,即将uri对应到某个目录下的文件,若文件存在则打开并读取文件信息; 若不存在则直接返回一段错误信息。Response类即用于处理该逻辑,同时它会将文件流写回至socket套接字中,由socket套接字将响应结果返回给客户端。 对于http协议而言,响应也是有一定的格式要求的,不能发送任意格式的信息,否则浏览器是无法接收并处理的。Http响应结果也包括三部分:协议 状态码 描述响应头响应实体段其中响应头与响应实体段间也是有一个空行的,具体的实例如下所示:HTTP/1.1 200 OK Date: Mon, 07 Jan 2013 1
10、4:31:36 GMT Server: BWS/1.0 Content-Length: 4029 Content-Type: text/html;charset=gbkCache-Control: private Expires:Content-Encoding: gzip Set-Cookie: H_PS_PSSID=1445_1661; path=/; domain= Keep-Alivehead/html Response类中当访问请求的文件不存在时,需要发送一段固定的响应文本给客户端,该段响应文档的格式必须严格按照http响应格式进行组织,否则客户端接收不到。具体的实现源码如下所示:i
11、mport java.io.FileInputStream; * 响应结果public class Response private OutputStream output; private Request request; private static final int BUFFER_SIZE = 1024; public Response(OutputStream output) this.output = output; public void setRequest(Request request) this.request = request; /发送一个静态资源给客户端,若本地服务
12、器有对应的文件则返回,否则返回404页面 public void sendStaticResource() byte buffer = new byteBUFFER_SIZE; int ch; FileInputStream fis = null; File file = new File(HttpServer.WEB_ROOT, request.getUri(); if(file.exists() fis = new FileInputStream(file); ch = fis.read(buffer); while(ch ! output.write(buffer, 0, ch); ch
13、 = fis.read(buffer, 0, BUFFER_SIZE); else String errorMessage = HTTP/1.1 404 File Not Found rn + text/htmlrn 24rnrnh1File Not Found!/h1 output.write(errorMessage.getBytes(); catch (Exception e) System.out.println(e.toString(); finally if(fis != null) try fis.close(); catch (IOException e) e.printSta
14、ckTrace();方法2:import java.io.PrintWriter;public class HTTPThread implements Runnable private Socket socket; private int count; public HTTPThread() public HTTPThread(Socket socket, int count) this.socket = socket; this.count = count; Override public void run() / TODO Auto-generated method stub try Ou
15、tputStream os = socket.getOutputStream(); PrintWriter pw = new PrintWriter(os); pw.println(bodyThis my page! You are welcome!/body pw.flush(); pw.close(); os.close(); catch (IOException e) e.printStackTrace();public class TCPServer public static void main(String args) int count = 1; ServerSocket ss
16、= new ServerSocket(8080); Socket s = null; while(s=ss.accept() != null) System.out.println(The visitor: + count); HTTPThread httpThread = new HTTPThread(s, count); Thread thread = new Thread(httpThread); thread.start(); count+;编译运行后,通过浏览器访问http:/localhost:8080/就可以了,是不是很神奇呢!用C语言实现一个简单的web编程#include u
17、nistd.hstdlib.hstring.hsys/types.hsys/socket.hnetinet/in.hfcntl.herrno.h#define EOL #define EOL_SIZE 2typedef struct char *ext; char *mediatype; extn;/Possible media typesextn extensions = gif, image/gif ,txttext/plainjpgimage/jpgjpeg,image/jpeg,pngimage/pngicoimage/icozipimage/zipgzimage/gztarimage
18、/tarhtmtext/htmlhtmlphppdfapplication/pdfapplication/octet-streamrar 0,0 ;/* A helper functionvoid error(const char *msg) perror(msg); exit(1);int get_file_size(int fd) struct stat stat_struct; if (fstat(fd, &stat_struct) = -1) return (1); return (int) stat_struct.st_size;void send_new(int fd, char
19、*msg) int len = strlen(msg); if (send(fd, msg, len, 0) = -1) printf(Error in sendn This function recieves the buffer until an End of line(EOL) byte is recievedint recv_new(int fd, char *buffer) char *p = buffer; / Use of a pointer to the buffer rather than dealing with the buffer directly int eol_ma
20、tched = 0; / Use to check whether the recieved byte is matched with the buffer byte or not while (recv(fd, p, 1, 0) != 0) / Start receiving 1 byte at a time if (*p = EOLeol_matched) / if the byte matches with the first eol byte that is r +eol_matched; if (eol_matched = EOL_SIZE) / if both the bytes matches with the EOL *(p + 1 - EOL_SIZE) = 0 / End the string return (strlen(buffer); / Return the bytes recieved else eol_matched = 0; p+; / Increment the pointer to receive next byte return (0); A helper function: Returns the web root location.char*