javaseday30(简单客户端 服务端 tomcat)

来源:互联网 发布:西宁网络电视台 编辑:程序博客网 时间:2024/06/07 07:12

可以通过pin console 固定控制台 再次运行不变 实现双窗口客户端 服务端 同时展现
最常见的客户端
浏览器IE
最常见的服务端 tomcat
http 网络协议 应用层的规则
ip网际层规则
tcp 传输层规则
在tomcat 的bin找stat开启服务器 DOS中会显示端口号
为了了解其原理
1、自定义服务端
使用已有的客户端IE 了解一下客户端给服务器端发了什么请求

//1、创建客户端socket        Socket s = new Socket("localhost", 10006);        //2、读取客户端要上传的图片文件        FileInputStream file = new FileInputStream("H:\\1.png");        //3、获取socket输出流 将读到图片数据发送给服务端        OutputStream out = s.getOutputStream();        byte[] buf =new byte[1024];        int len = 0;        while((len=file.read(buf))!=-1) {            out.write(buf, 0, len);        }        //告诉服务端 这边的数据发送完毕 让服务端停止 读取        s.shutdownOutput();        //读取服务端发回的内容        InputStream in = s.getInputStream();        byte[] bufin =new byte[1024];        int lenin = in.read(bufin);        String text = new String(bufin,0,lenin);        System.out.println(text);        file.close();        s.close();
//创建tcp的socket服务端        ServerSocket ss = new ServerSocket(10006);//有一个服务端就够了        //获取客户端        while(true) {            Socket s = ss.accept();            new Thread(new UploadTask(s)).start();        }//      String ip = s.getInetAddress().getHostAddress();//      System.out.println(ip);//      //读取客户端发来的数据//      InputStream in = s.getInputStream();//      //将读取到的数据存储到文件中//      File dir = new File("h:\\pic");//      if(!dir.exists()) {//          dir.mkdirs();//      }//      File file = new File(dir,ip+".bmp");//      FileOutputStream fos = new FileOutputStream(file);//      byte[] buf = new byte[1024];//      int len =0;//      while((len=in.read(buf))!=-1) {//          fos.write(buf, 0, len);//      }//      //获取socket数出来 反馈上传成功给客户端//      OutputStream out = s.getOutputStream();//      out.write("成功".getBytes());//      fos.close();//      s.close();//      ss.close();        //        //

多线程中的任务

public class UploadTask implements Runnable {    private Socket s;    private static final int SIZE = 1024*1024*2;    public UploadTask(Socket s) {        // TODO Auto-generated constructor stub        this.s=s;    }    @Override    public void run() {        // TODO Auto-generated method stub        int count = 0;        String ip = s.getInetAddress().getHostAddress();        System.out.println(ip);        try {        //读取客户端发来的数据        InputStream in = s.getInputStream();        //将读取到的数据存储到文件中        File dir = new File("h:\\pic");        if(!dir.exists()) {            dir.mkdirs();        }        File file = new File(dir,ip+".bmp");        while (file.exists()){            file = new File(dir,ip+"("+(++count)+").bmp");        }        FileOutputStream fos = new FileOutputStream(file);        byte[] buf = new byte[1024];        int len =0;        while((len=in.read(buf))!=-1) {            fos.write(buf, 0, len);            if(file.length()>SIZE){                System.out.println(ip+"文件体积过大");                fos.close();                s.close();                System.out.println(ip+"...."+file.delete());                return ;            }        }        //获取socket数出来 反馈上传成功给客户端        OutputStream out = s.getOutputStream();        out.write("成功".getBytes());        fos.close();        s.close();        } catch(IOException e) {            e.printStackTrace();        }    }}

Tomcat

ServerSocket ss = new ServerSocket(9999);        Socket s = ss.accept();        System.out.println(s.getInetAddress().getHostAddress());        InputStream in = s.getInputStream();        byte[] buf = new byte[1024];        int len =in.read(buf);        String text = new String(buf,0,len);        System.out.println(text);        //给服务端一个反馈信息        PrintWriter out = new PrintWriter(s.getOutputStream(),true);        out.println("hh");        s.close();        ss.close();

GET / HTTP/1.1 请求行 请求方式 /myweb/1.html 请求资源路径 http协议的版本 1.1和1.0用的多是1.1 请求消息头 属性名 属性值
Host: 127.0.0.1:9999
//Host:192.168.1.100:9999 有多个主机 服务器
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate 服务端压缩后返回 客户端解压快
Connection: keep-alive
Upgrade-Insecure-Requests: 1
空行
请求体 客户端给服务端发送一些信息

http解析引擎
根据不同的请求有不同的处理方式

        Socket s = new Socket("127.0.0.1", 8080);        //模拟浏览器给tomcat服务器端 发送符合http协议的请求消息        PrintWriter out = new PrintWriter(s.getOutputStream(),true);        out.println("GET /myweb/1.html HTTP/1.1");//要凑满5个 不然就会出现角标越界异常        out.println("Accept: */*");        out.println("Host: 127.0.0.1:8080");        out.println("Connection: close");        out.println();        InputStream in = s.getInputStream();        byte[] buf = new byte[1024];        int len = in.read(buf);        String str =new String(buf,0,len);        System.out.println(str);        s.close();

服务器发回应答信息
HTTP/1.1 200 应答行 http的协议版本 应答状态码 应答状态描述信息
应答消息属性信息 属性名 属性值
Accept-Ranges: bytes
ETag: W/”332-1503617267889”
Last-Modified: Thu, 24 Aug 2017 23:27:47 GMT
Content-Type: text/html
Content-Length: 332
Date: Sat, 26 Aug 2017 09:01:06 GMT
Connection: close
空行
应答体