MyServer

时间:2023-03-09 19:16:56
MyServer

MyServer

 //一、设置一个8089端口的本地IP服务器
1 package myserver; import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket; public class MyServer {
public MyServer() {
try {
ServerSocket socket = new ServerSocket(8089);
while (true) {
Socket s = socket.accept();
new SocketThread(s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new MyServer();
}
}
 //二、多线程监听客户端连接
1 package myserver; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket; public class SocketThread implements Runnable{
private Socket socket; public SocketThread(Socket socket) {
this.socket = socket; Thread th = new Thread(this);
th.start();
} @Override
public void run() {
InputStream in = null;
OutputStream out = null;
try {
in = socket.getInputStream();//获取文件读取流
out = socket.getOutputStream();//获取文件写入流
Response response = new Response(out);//相应数据到浏览器
Request request = new Request(in);//从网页获取数据
System.out.println("********"+request);
String url = request.getUrl();//获取URL路径
//response.sendMessage("hello");
response.sendFile(url);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
 //三、请求相应处理
1 package myserver; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; public class Response {
private OutputStream out; public Response(OutputStream out) {
this.out = out;
} //发送消息到浏览器
public void sendMessage(String msg) {
try {
out.write(msg.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void sendFile(String filePath) throws IOException {
File f = new File(filePath);
if (f.exists() == false) {//判断文件是否存在
return;
}
InputStream in = null;
try {
in = new FileInputStream(filePath);
byte[] by = new byte[1024];
int len = 0;
while ((len=in.read(by)) != -1) {
out.write(by, 0, len);//读取1024字节并写入到浏览器
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
in.close();
}
}
}
//接受客户端请求
1 package myserver; import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map; public class Request {
private String url;
//封装表单数据
private Map<String,String> paramMap = new HashMap<String,String>(); public Request(InputStream in) {
byte[] by = new byte[1024];
try {
in.read(by);
String str = new String(by).trim();//去掉两边空格
System.out.println(str); if (str.startsWith("GET")){//GET开头的流数据
this.getGet(str);
}else{//POST开头的流数据
this.getPOST(str);
}
} catch (IOException e) {
e.printStackTrace();
}
} private void getPOST(String str) {//获取POST的URL和表单数据
String[] s = str.split("\\s+");//按空格割分字符串为字符串数组
this.url = s[1].substring(1);//表单的POST提交
getMap(s[s.length-1]);//获取表单数据
} private void getGet(String str) {//获取GET的URL和表单数据
String[] s = str.split("\\s+");//按空格割分字符串为字符串数组
if (s[1].indexOf("?") == -1){
this.url = s[1].substring(1);//文本的GET提交
}else{
String s1 = s[1];
//表单的GET提交
this.url = s1.split("[?]")[0].substring(1);
this.getMap(s1.split("[?]")[1]);//获取表单数据
}
}
//GET /url POST|GET /url?userName=""&pwd=""&age=""&sex=""
private Map<String,String> getMap(String string) {//获取表单数据
String[] s = string.split("[&]");
for (String str : s) {
this.paramMap.put(str.split("[=]")[0], str.split("[=]")[1]);
}
return this.paramMap;
} public String getUrl() {
return this.url;
} @Override
public String toString() {
return "Request [url=" + url + ", paramMap=" + paramMap + "]";
}
}
 //模拟GET,POST请求表单提交的登录界面
1 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<img alt="" src="1.jpg" width="300" height="200"/><br/>
<img alt="" src="1.jpg" width="300" height="200"/><br/>
<p>智商不够,努力来凑</p> <form action="login" method="PUT">
<input type="text" name="userName"><br/>
<input type="password" name="pwd"><br/>
<input type="submit" value="登录">
</form>
</body>
</html>

相应的数据流信息如下:

GET /login.html HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C)
UA-CPU: AMD64
Accept-Encoding: gzip, deflate
Host: localhost:8089
Connection: Keep-Alive
********Request [url=login.html, paramMap={}]

GET /1.jpg HTTP/1.1
Accept: */*
Referer: http://localhost:8089/login.html
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C)
UA-CPU: AMD64
Accept-Encoding: gzip, deflate
Host: localhost:8089
Connection: Keep-Alive
********Request [url=1.jpg, paramMap={}]

GET /login?userName=123&pwd=123 HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*
Referer: http://localhost:8089/login.html
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C)
UA-CPU: AMD64
Accept-Encoding: gzip, deflate
Host: localhost:8089
Connection: Keep-Alive
********Request [url=login, paramMap={userName=123, pwd=123}]

GET /login?userName=123&pwd=123 HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C)
UA-CPU: AMD64
Accept-Encoding: gzip, deflate
Host: localhost:8089
Connection: Keep-Alive
********Request [url=login, paramMap={userName=123, pwd=123}]

GET /a.txt HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*
Accept-Language: zh-CN
Cache-Control: no-cache
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C)
UA-CPU: AMD64
Accept-Encoding: gzip, deflate
Host: localhost:8089
Connection: Keep-Alive
********Request [url=a.txt, paramMap={}]

GET /1.jpg HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C)
UA-CPU: AMD64
Accept-Encoding: gzip, deflate
Host: localhost:8089
Connection: Keep-Alive
********Request [url=1.jpg, paramMap={}]

GET / HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*
Accept-Language: zh-CN
Cache-Control: no-cache
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C)
UA-CPU: AMD64
Accept-Encoding: gzip, deflate
Host: localhost:8089
Connection: Keep-Alive
********Request [url=, paramMap={}]

总结:以上是用代码模拟实现的一个tomcat。