這篇文章給大家介紹在java項目中使用 Socket如何實現(xiàn)模擬一個HTTP服務器,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設,彰武企業(yè)網(wǎng)站建設,彰武品牌網(wǎng)站建設,網(wǎng)站定制,彰武網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,彰武網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。HTTP基于TCP協(xié)議,協(xié)議采用了請求/響應模型。客戶端向服務器發(fā)送一個請求,請求頭包含請求的方法、URL、協(xié)議版本、以及包含請求修飾符、客戶信息和內(nèi)容的類似于MIME的消息結(jié)構(gòu)。服務器以一個狀態(tài)行作為響應,響應的內(nèi)容包括消息協(xié)議的版本,成功或者錯誤編碼加上包含服務器信息、實體元信息以及可能的實體內(nèi)容
具體字段這里不作解釋,不懂的請先自己了解http協(xié)議。這里主要是根據(jù)請求響應這一過程模擬HTTP服務器。
下面是代碼:
package com.example.httpserver; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class HttpServer { private static final Integer port = 80;//HTTP默認端口80 public static void main(String[] args) { ServerSocket serverSocket; try { //建立服務器Socket,監(jiān)聽客戶端請求 serverSocket = new ServerSocket(port); System.out.println("Server is running on port:"+serverSocket.getLocalPort()); //死循環(huán)不間斷監(jiān)聽客戶端請求 while(true){ final Socket socket = serverSocket.accept(); System.out.println("biuld a new tcp link with client,the cient address:"+ socket.getInetAddress()+":"+socket.getPort()); //并發(fā)處理HTTP客戶端請求 service(socket); } } catch (IOException e) { e.printStackTrace(); } } public static void service(Socket socket) { new Thread(){ public void run(){ InputStream inSocket; try { //獲取HTTP請求頭 inSocket = socket.getInputStream(); int size = inSocket.available(); byte[] buffer = new byte[size]; inSocket.read(buffer); String request = new String(buffer); System.out.println("ClientBrowser:\n"+request+"\n" + "------------------------------------------------------------------"); String firstLineOfRequest = ""; String[] heads; String uri = "/index.html"; String contentType =""; if(request.length() > 0){ firstLineOfRequest = request.substring(0,request.indexOf("\r\n")); heads = firstLineOfRequest.split(" "); uri = heads[1]; if(uri.indexOf("html") != -1){ contentType = "text/html"; }else{ contentType = "application/octet-stream"; } } //將響應頭發(fā)送給客戶端 String responseFirstLine = "HTTP/1.1 200 OK\r\n"; String responseHead = "Content-Type:" + contentType +"\r\n"; OutputStream outSocket = socket.getOutputStream(); System.out.println("ServerResponse:\n"+responseFirstLine+"\n"+responseHead+"\n" + "--------------------------------------------------------------------"); outSocket.write(responseFirstLine.getBytes()); outSocket.write(responseHead.getBytes()); //通過HTTP請求中的uri讀取相應文件發(fā)送給客戶端 FileInputStream writehtml = new FileInputStream(new File("wwwroot"+uri)); outSocket.write("\r\n".getBytes()); byte[] htmlbuffer = new byte[writehtml.available()]; if(writehtml !=null){ int len = 0; System.out.println("writeHtml"); while((len = writehtml.read(htmlbuffer)) != -1){ outSocket.write(htmlbuffer, 0,len); } } outSocket.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }.start(); } }