MyServer

Posted 沉迷学习,日渐消瘦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyServer相关的知识,希望对你有一定的参考价值。

 

 //一、设置一个8089端口的本地IP服务器
1
package myserver; 2 3 import java.io.IOException; 4 import java.net.ServerSocket; 5 import java.net.Socket; 6 7 public class MyServer { 8 public MyServer() { 9 try { 10 ServerSocket socket = new ServerSocket(8089); 11 while (true) { 12 Socket s = socket.accept(); 13 new SocketThread(s); 14 } 15 } catch (IOException e) { 16 e.printStackTrace(); 17 } 18 } 19 public static void main(String[] args) { 20 new MyServer(); 21 } 22 }

 //二、多线程监听客户端连接
1
package myserver; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 import java.net.Socket; 7 8 public class SocketThread implements Runnable{ 9 private Socket socket; 10 11 public SocketThread(Socket socket) { 12 this.socket = socket; 13 14 Thread th = new Thread(this); 15 th.start(); 16 } 17 18 @Override 19 public void run() { 20 InputStream in = null; 21 OutputStream out = null; 22 try { 23 in = socket.getInputStream();//获取文件读取流 24 out = socket.getOutputStream();//获取文件写入流 25 Response response = new Response(out);//相应数据到浏览器 26 Request request = new Request(in);//从网页获取数据 27 System.out.println("********"+request); 28 String url = request.getUrl();//获取URL路径 29 //response.sendMessage("hello"); 30 response.sendFile(url); 31 out.flush(); 32 } catch (IOException e) { 33 e.printStackTrace(); 34 }finally{ 35 try { 36 out.close(); 37 in.close(); 38 socket.close(); 39 } catch (IOException e) { 40 e.printStackTrace(); 41 } 42 } 43 } 44 }
 //三、请求相应处理
1
package myserver; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.OutputStream; 9 10 public class Response { 11 private OutputStream out; 12 13 public Response(OutputStream out) { 14 this.out = out; 15 } 16 17 //发送消息到浏览器 18 public void sendMessage(String msg) { 19 try { 20 out.write(msg.getBytes()); 21 } catch (IOException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } 25 } 26 27 public void sendFile(String filePath) throws IOException { 28 File f = new File(filePath); 29 if (f.exists() == false) {//判断文件是否存在 30 return; 31 } 32 InputStream in = null; 33 try { 34 in = new FileInputStream(filePath); 35 byte[] by = new byte[1024]; 36 int len = 0; 37 while ((len=in.read(by)) != -1) { 38 out.write(by, 0, len);//读取1024字节并写入到浏览器 39 } 40 } catch (FileNotFoundException e) { 41 e.printStackTrace(); 42 }finally{ 43 in.close(); 44 } 45 } 46 }
//接受客户端请求
1
package myserver; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.HashMap; 6 import java.util.Map; 7 8 public class Request { 9 private String url; 10 //封装表单数据 11 private Map<String,String> paramMap = new HashMap<String,String>(); 12 13 public Request(InputStream in) { 14 byte[] by = new byte[1024]; 15 try { 16 in.read(by); 17 String str = new String(by).trim();//去掉两边空格 18 System.out.println(str); 19 20 if (str.startsWith("GET")){//GET开头的流数据 21 this.getGet(str); 22 }else{//POST开头的流数据 23 this.getPOST(str); 24 } 25 } catch (IOException e) { 26 e.printStackTrace(); 27 } 28 } 29 30 private void getPOST(String str) {//获取POST的URL和表单数据 31 String[] s = str.split("\\\\s+");//按空格割分字符串为字符串数组 32 this.url = s[1].substring(1);//表单的POST提交 33 getMap(s[s.length-1]);//获取表单数据 34 } 35 36 private void getGet(String str) {//获取GET的URL和表单数据 37 String[] s = str.split("\\\\s+");//按空格割分字符串为字符串数组 38 if (s[1].indexOf("?") == -1){ 39 this.url = s[1].substring(1);//文本的GET提交 40 }else{ 41 String s1 = s[1]; 42 //表单的GET提交 43 this.url = s1.split("[?]")[0].substring(1); 44 this.getMap(s1.split("[?]")[1]);//获取表单数据 45 } 46 } 47 //GET /url POST|GET /url?userName=""&pwd=""&age=""&sex="" 48 private Map<String,String> getMap(String string) {//获取表单数据 49 String[] s = string.split("[&]"); 50 for (String str : s) { 51 this.paramMap.put(str.split("[=]")[0], str.split("[=]")[1]); 52 } 53 return this.paramMap; 54 } 55 56 public String getUrl() { 57 return this.url; 58 } 59 60 @Override 61 public String toString() { 62 return "Request [url=" + url + ", paramMap=" + paramMap + "]"; 63 } 64 }
 //模拟GET,POST请求表单提交的登录界面
1
<!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>login</title> 6 </head> 7 <body> 8 <img alt="" src="1.jpg" width="300" height="200"/><br/> 9 <img alt="" src="1.jpg" width="300" height="200"/><br/> 10 <p>智商不够,努力来凑</p> 11 12 <form action="login" method="PUT"> 13 <input type="text" name="userName"><br/> 14 <input type="password" name="pwd"><br/> 15 <input type="submit" value="登录"> 16 </form> 17 </body> 18 </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。

 

以上是关于MyServer的主要内容,如果未能解决你的问题,请参考以下文章

Ubuntu ssh:无法解析主机名 myserver:名称或服务未知

MyServer

Android N 预览版中不允许到 myserver.com 的明文 HTTP 流量

rabbitmqctl 错误:无法连接到节点 rabbit@myserver nodedown

当 URL 路径中出现双斜杠时,如何使用 Elastic Beanstalk (Apache+Tomcat) 解决 HTTP 404 错误,例如https://uat.myserver//rest/s

如何在 Erlang/OTP 中使用 application:get_env()?