zoukankan      html  css  js  c++  java
  • 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();
                        
                    }
                }
            }
        
        }
    
    }
  • 相关阅读:
    使用三星720n液晶显示器的体会
    昨天终于买显示器了
    2005525早上
    抵制日货的结果zt
    读写配置文件类
    递归 访问树节点
    IE条件注释
    闭包 页面渐变
    模块 替换HTML 字符实体(双引号、左右尖括号)
    闭包 查找节点序号
  • 原文地址:https://www.cnblogs.com/lilei2blog/p/9371469.html
Copyright © 2011-2022 走看看