您好,登錄后才能下訂單哦!
使用ServerSocket建立與瀏覽器的連接,獲取請求協(xié)議
public class Server {
private ServerSocket serverSocket;
public static void main(String[]args)
{
Server server=new Server();
server.start();
}
//啟動(dòng)服務(wù)
public void start()
{
try {
serverSocket=new ServerSocket(8888);
receive();
} catch (IOException e) {
e.printStackTrace();
System.out.println("服務(wù)器啟動(dòng)失敗");
}
}
//停止服務(wù)
public void stop()
{
}
//接受連接處理
public void receive()
{
try {
Socket client=serverSocket.accept();
System.out.println("一個(gè)客戶端建立了連接");
//獲取請求協(xié)議
InputStream is =client.getInputStream();
byte[] datas=new byte[1024*1024];
int len=is.read(datas); //讀取完畢,并返回長度
String requestInfo =new String(datas,0,len);
System.out.println(requestInfo);
} catch (IOException e) {
e.printStackTrace();
System.out.println("客戶端錯(cuò)誤");
}
}
}
返回響應(yīng)協(xié)議:
public class Server02 {
private ServerSocket serverSocket;
public static void main(String[]args)
{
Server02 server=new Server02();
server.start();
}
//啟動(dòng)服務(wù)
public void start()
{
try {
serverSocket=new ServerSocket(8888);
receive();
} catch (IOException e) {
e.printStackTrace();
System.out.println("服務(wù)器啟動(dòng)失敗");
}
}
//停止服務(wù)
public void stop()
{
}
//接受連接處理
public void receive()
{
try {
Socket client=serverSocket.accept();
System.out.println("一個(gè)客戶端建立了連接");
//獲取請求協(xié)議
InputStream is =client.getInputStream();
byte[] datas=new byte[1024*1024];
int len=is.read(datas); //讀取完畢,并返回長度
String requestInfo =new String(datas,0,len);
System.out.println(requestInfo);
StringBuilder content =new StringBuilder();
content.append("<html>");
content.append("<head>");
content.append("<title>");
content.append("服務(wù)器響應(yīng)成功");
content.append("</title>");
content.append("</head>");
content.append("<body>");
content.append("終于回來了");
content.append("</body>");
content.append("</html>");
int size=content.toString().getBytes().length; //必須獲取字節(jié)長度
StringBuilder responseInfo =new StringBuilder();
String blank =" ";
String CRLF="\r\n";
//拼接響應(yīng)行
responseInfo.append("HTTP/1.1").append(blank);
responseInfo.append(200).append(blank);
responseInfo.append("OK").append(CRLF);
//返回
//1、響應(yīng)行:HTTP/1.1 200 OK
//2、響應(yīng)頭(最后一行存在空行):
/*
Date:Mon,31Dec209904:25:57GMT
Server:shsxt Server/0.0.1;charset=GBK 服務(wù)器內(nèi)容
Content-type:text/html 內(nèi)容類型
Content-length:39725426 內(nèi)容長度
*/
//拼接響應(yīng)頭
responseInfo.append("Date:").append(new Date()).append(CRLF);
responseInfo.append("Server:").append("shsxt Server/0.0.1;charset=GBK").append(CRLF);
responseInfo.append("Content-type:text/html").append(CRLF);
responseInfo.append("Content-length:").append(size).append(CRLF);
responseInfo.append(CRLF); //響應(yīng)頭最后一行存在空行
//3、 正文
responseInfo.append(content.toString());
//寫出到客戶端
BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
bw.write(responseInfo.toString());
bw.flush();
} catch (IOException e) {
e.printStackTrace();
System.out.println("客戶端錯(cuò)誤");
}
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。