• serversocket和serversocketchannel实现http服务


    ServerSocket:

    package com.http;
    
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class MyServer {
    
        public static void main(String[] args) throws java.io.IOException,java.lang.InterruptedException {
    
            ServerSocket ss = new ServerSocket(8000);
            
            Socket socket = null;
            
            while((socket = ss.accept())!= null){
                PrintWriter out = new PrintWriter(socket.getOutputStream());
                
                out.println("HTTP/1.1 200");
                out.println("Content-Type: text/html");
                out.println("Date: Thu, 26 Jul 2018 01:54:53 GMT");
                
                out.println();
                
                out.println("<html><head><title>Hello</title></head><body><h1>hello world aaaa</h1>");
                
                out.flush();
                
                
                out.println("</body></html>");
                
                out.flush();
                
                socket.shutdownOutput();
            }
        }
    
    }

    ServerSocketChannel:

    package com.http;
    
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.util.Iterator;
    
    public class MyNioServer {
    
        public static void main(String[] args) throws Exception {
    
            ServerSocketChannel ssc = ServerSocketChannel.open();
            
            ssc.configureBlocking(false);
            
            Selector selector = Selector.open();
            
            ssc.register(selector, SelectionKey.OP_ACCEPT);
            
            ssc.bind(new InetSocketAddress(8000));
            
            while(true){
                selector.select();
                
                Iterator<SelectionKey> it = selector.selectedKeys().iterator();
                
                while(it.hasNext()){
                    SelectionKey key = it.next();
                    
                    if (key.isValid() && key.isAcceptable()){
                        
                        SocketChannel sc = ssc.accept();
                        
                        sc.configureBlocking(false);
                        
                        ByteBuffer buf = ByteBuffer.allocate(1024);
                        
                        buf.put("HTTP/1.1 200
    ".getBytes());
                        buf.put("Content-Type: text/html
    ".getBytes());
                        buf.put("Date: Thu, 26 Jul 2018 01:54:53 GMT
    ".getBytes());
                        buf.put("
    ".getBytes());
                        buf.put("<html><head><title>Hello</title></head><body><h1>hello world aaaa</h1></body></html>".getBytes());
                        
                        buf.flip();
                        
                        sc.write(buf);
                        
                        
                        sc.shutdownOutput();
                        
                        //sc.close();
                        
                    }
                }
            }
        
        }
    
    }
  • 相关阅读:
    定时任务时间表达式的规则(自己总结)
    本地vagrant配置虚拟域名的坑
    商派onex本地部署无法进入的问题
    一周一篇文章,立贴为证
    Ecshop安装的坑,建议不要使用!
    MYSQL查询语句优化
    .gitignore文件
    剖析Disruptor:为什么会这么快?(二)神奇的缓存行填充
    Disruptor 为什么这么快?
    一篇文章让你成为 NIO 大师 - MyCAT通信模型
  • 原文地址:https://www.cnblogs.com/lilei2blog/p/9371469.html
Copyright © 2020-2023  润新知