服务端:绑定端口,启动服务监听,请求是啥,响应一些啥?
/**
* @author x y
* @description TODO
* @date 2022-01-18 10:11
*/
public class AcceptServer {
ServerSocket serverSocket;
IHandlerFunction iHandlerFunction;
public AcceptServer(IHandlerFunction iHandlerFunction) {
this.iHandlerFunction = iHandlerFunction;
}
public void listenPort(int port) throws IOException {
serverSocket = new ServerSocket(port);
while (true) {
this.accept();
}
}
public void accept() throws IOException {
Socket accept = serverSocket.accept();
new Thread(() -> {
try {
this.acceptHandler(accept);
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
public void acceptHandler(Socket socket) throws IOException {
Request request = new Request(socket);
Response response = new Response(socket);
iHandlerFunction.handler(request,response);
}
public static void main(String[] args) throws IOException {
AcceptServer server = new AcceptServer((req, resp) -> {
System.err.println(req.getRequestMap());
resp.send("<html><body><h1>Hello Word!</h1></body></html>");
});
server.listenPort(8002);
}
}
函数式接口:
/**
* @author x y
* @description TODO
* @date 2022-01-18 10:11
*/
@FunctionalInterface
public interface IHandlerFunction {
void handler(Request request,Response response) throws IOException;
}
请求处理:
/**
* @author x y
* @description TODO
* @date 2022-01-18 10:11
*/
public class Request {
static Pattern pattern = Pattern.compile("(GET|POST|DELETE|PUT)");
private final String method;
private final Map<String, String> requestMap;
private final String requestBody;
public String getMethod() {
return method;
}
public Map<String, String> getRequestMap() {
return requestMap;
}
public String getRequestBody() {
return requestBody;
}
public Request(Socket socket) throws IOException {
// dataInputStream --> char,float数据交互
// InputStream ---> byte
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
//请求方式
String methodPattern = HttpParser.readLine(dataInputStream, "UTF-8");
Matcher matcher = pattern.matcher(methodPattern);
matcher.find();
String method = matcher.group();
requestMethod(method);
//请求头
Header[] headers = HttpParser.parseHeaders(dataInputStream, "utf-8");
Map<String, String> methodRequestMap = Maps.newHashMap();
for (Header header : headers) {
methodRequestMap.put(header.getName(), header.getValue());
}
//请求体
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));
char[] chars = new char[1024];
StringBuilder stringBuilder = new StringBuilder();
if (dataInputStream.available() > 0) {
bufferedReader.read(chars);
stringBuilder.append(chars);
}
this.method = method;
this.requestMap = methodRequestMap;
this.requestBody = stringBuilder.toString();
}
public void requestMethod(String method){
switch (method){
case "GET":
System.err.println("get请求方法");
break;
case "POST":
System.err.println("POST请求方法");
break;
case "DELETE":
System.err.println("DELETE请求方法");
break;
}
}
}
响应处理:
/**
* @author x y
* @description TODO
* @date 2022-01-18 10:11
*/
public class Response {
Socket socket;
private Integer status = 200;
private Map<Integer, String> statusMap;
// 数据转发
public Response(Socket socket) throws IOException {
this.socket = socket;
if (statusMap == null){
statusMap = Maps.newHashMap();
statusMap.put(200,"OK");
}
}
public void send(String msg) throws IOException {
String req = "HTTP/1.1 "+this.status+" "+ this.statusMap.get(this.status)+"\n";
req +="\n";
req += msg;
sendMsg(req);
}
public void sendMsg(String msg) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bufferedWriter.write(msg);
bufferedWriter.flush();
socket.close();
}
}
|