使用 Netty 实现 HTTP 和 TCP_netty tcpclient-程序员宅基地

技术标签: java  

Http Client

HttpClient 客户端

package com.example.nettydemo.eyue.httpClient;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.HttpRequestEncoder;
import io.netty.handler.codec.http.HttpResponseDecoder;

/**
 * @author dong
 * @date 2022/3/31
 * @AapiNote
 */
public class HttpClient {
    
    public void connect(String host, int port) throws Exception {
    
        EventLoopGroup group = new NioEventLoopGroup();
        try {
    
            Bootstrap b = new Bootstrap();
            b.group(group);
            b.channel(NioSocketChannel.class);
            b.option(ChannelOption.SO_KEEPALIVE, true);
            b.handler(new ChannelInitializer<SocketChannel>() {
    
                @Override
                public void initChannel(SocketChannel ch) {
    
                    ch.pipeline().addLast(new HttpResponseDecoder());//HTTP编码
                    ch.pipeline().addLast(new HttpRequestEncoder());//HTTP解码
                    ch.pipeline().addLast(new HttpClientHandler());//业务处理器
                }
            });

            //建立长连接
            ChannelFuture f = b.connect(host, port).sync();
            System.out.println("netty http client connected on host(" + host + ") port(" + port + ")");
            f.channel().closeFuture().sync();
        } finally {
    
            group.shutdownGracefully();
        }
    }



    public static void main(String[] args) throws Exception {
    
        new HttpClient().connect("127.0.0.1", 8801);
    }
}

HttpClientHandler 处理类

package com.example.nettydemo.eyue.httpClient;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

import java.net.URI;
import java.nio.charset.StandardCharsets;

/**
 * @author dong
 * @date 2022/3/31
 * @AapiNote
 */
public class HttpClientHandler extends ChannelInboundHandlerAdapter {
    

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
    
        //发送http请求
        URI uri = new URI("http://127.0.0.1:8800:/test?data=100");

        String content = "hello netty http Server!";
        DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
                uri.toASCIIString(), Unpooled.wrappedBuffer(content.getBytes(StandardCharsets.UTF_8)));
//        request.headers().set(HttpHeaderNames.HOST, host);
        request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
        request.headers().set(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes());


        ctx.channel().writeAndFlush(request);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
    
        if (msg instanceof HttpContent) {
    
            HttpContent content = (HttpContent) msg;
            ByteBuf buf = content.content();
            System.out.println("收到服务端的消息:" + buf.toString(CharsetUtil.UTF_8));
        }
    }
}

Http Server

NettyHttpServer 服务端

package com.example.nettydemo.eyue.httpserver;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;

/**
 * @author dong
 * @date 2022/3/31
 * @AapiNote
 */
@Slf4j
public class NettyHttpServer implements Runnable{
    
    int port ;

    public NettyHttpServer(int port){
    
        this.port = port;
    }

    EventLoopGroup boss = new NioEventLoopGroup();
    EventLoopGroup work = new NioEventLoopGroup();

    @Override
    public void run() {
    
        try {
    
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(boss,work)
                    .handler(new LoggingHandler(LogLevel.DEBUG))
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new HttpServerInitializer());


            ChannelFuture f = bootstrap.bind(new InetSocketAddress(port)).sync();
//        System.out.println(" server start up on port : " + port);

            if (f.isSuccess()) {
    
                System.out.println("HTTP服务端启动成功");
            } else {
    
                System.out.println("HTTP服务端启动失败");
                f.cause().printStackTrace();
            }

            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
    
            e.printStackTrace();
        } finally {
    
            boss.shutdownGracefully();
            work.shutdownGracefully();
        }
    }


//    public static void main(String[] args) throws Exception{
    
//        NettyHttpServer server = new NettyHttpServer(8801);// 8081为启动端口
//        server.start();
//    }
}

HttpServerInitializer 初始化类

package com.example.nettydemo.eyue.httpserver;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;

/**
 * @author dong
 * @date 2022/3/31
 * @AapiNote
 */
public class HttpServerInitializer extends ChannelInitializer<SocketChannel> {
    

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
    
        System.out.println("初始化通道: Http server initChannel..");
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast(new HttpServerCodec());// http 编解码
        pipeline.addLast("httpAggregator",new HttpObjectAggregator(512*1024)); // http 消息聚合器                                                                     512*1024为接收的最大contentlength
        pipeline.addLast(new HttpServerRequestHandler());// 请求处理器
    }
}

HttpServerRequestHandler 处理类

// An highlighted block
var foo = 'bar';package com.example.nettydemo.eyue.httpserver;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

import java.net.SocketAddress;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dong
 * @date 2022/3/31
 * @AapiNote
 */
@Slf4j
public class HttpServerRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
    

        //100 Continue
        if (HttpUtil.is100ContinueExpected(req)) {
    
            ctx.write(new DefaultFullHttpResponse(
                    HttpVersion.HTTP_1_1,
                    HttpResponseStatus.CONTINUE));
        }


        //返回此通道绑定到的本地地址(IP 地址 和 端口号)
        SocketAddress socketAddress = ctx.channel().localAddress();
        String str = socketAddress.toString();
        System.out.println("ip"+str);
        //截取端口号
        String str1=str.substring(0, str.indexOf(":"));
        String str2=str.substring(str1.length()+1, str.length());
        System.out.println("port: "+str2);



        // 获取请求的uri
        String uri = req.uri();
        System.out.println("收到客户端发来的消息"+uri);

        Map<String,String> resMap = new HashMap<>();
        resMap.put("method",req.method().name());
        resMap.put("uri",uri);
        String msg = "你请求uri为:" + uri+"";
        // 创建http响应
        FullHttpResponse response = new DefaultFullHttpResponse(
                HttpVersion.HTTP_1_1,
                HttpResponseStatus.OK,
                Unpooled.copiedBuffer(msg, CharsetUtil.UTF_8));
        // 设置头信息
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");

        //response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");

        // 将html write到客户端
        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    }


    //通知处理器最后的channelRead()是当前批处理中的最后一条消息时调用
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    
        System.out.println("服务端接收数据完毕..");
        ctx.flush();
    }

    //读操作时捕获到异常时调用
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    
        ctx.close();
    }

    //客户端去和服务端连接成功时触发
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
    
        ctx.writeAndFlush("hello client");
    }
}

Tcp Client

RpcRequest 实体类

package com.example.nettydemo.eyue.entiy;

import lombok.Data;

/**
 * @author dong
 * @date 2022/4/2
 * @AapiNote
 */
@Data
public class RpcRequest {
    
    private String id;
    private Object data;

    @Override
    public String toString() {
    
        return "RpcRequest{" + "id='" + id + '\'' + ", data=" + data + '}';
    }
}

RpcResponse 实体类

package com.example.nettydemo.eyue.entiy;

import lombok.Data;

/**
 * @author dong
 * @date 2022/4/2
 * @AapiNote
 */
@Data
public class RpcResponse {
    
    private String id;
    private Object data;
    private int status;

    @Override
    public String toString() {
    
        return "RpcResponse{" + "id='" + id + '\'' + ", data=" + data + ", status=" + status + '}';
    }
}

RpcEncoder 编码

package com.example.nettydemo.eyue;

import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @author dong
 * @date 2022/4/2
 * @AapiNote
 */
public class RpcEncoder extends MessageToByteEncoder {
    
    //目标对象类型进行编码
    private Class<?> target;

    public RpcEncoder(Class target) {
    
        this.target = target;
    }

    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
    
        if (target.isInstance(msg)) {
    
            byte[] data = JSON.toJSONBytes(msg); //使用fastJson将对象转换为byte
            out.writeInt(data.length); //先将消息长度写入,也就是消息头
            out.writeBytes(data); //消息体中包含我们要发送的数据
        }
    }
}

RpcDecoder 解码

package com.example.nettydemo.eyue;

import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @author dong
 * @date 2022/4/2
 * @AapiNote
 */
public class RpcDecoder extends ByteToMessageDecoder {
    
    //目标对象类型进行解码
    private Class<?> target;

    public RpcDecoder(Class target) {
    
        this.target = target;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    
        if (in.readableBytes() < 4) {
     //不够长度丢弃
            return;
        }
        in.markReaderIndex(); //标记一下当前的readIndex的位置
        int dataLength = in.readInt(); // 读取传送过来的消息的长度。ByteBuf 的readInt()方法会让他的readIndex增加4

        if (in.readableBytes() < dataLength) {
     //读到的消息体长度如果小于我们传送过来的消息长度,则resetReaderIndex. 这个配合markReaderIndex使用的。把readIndex重置到mark的地方
            in.resetReaderIndex();
            return;
        }
        byte[] data = new byte[dataLength];
        in.readBytes(data);

        Object obj = JSON.parseObject(data, target); //将byte数据转化为我们需要的对象
        out.add(obj);
    }
}

TcpClient 客户端

package com.example.nettydemo.eyue.tcpClient;

import com.example.nettydemo.eyue.entiy.RpcRequest;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.SneakyThrows;

/**
 * @author dong
 * @date 2022/3/31
 * @AapiNote
 */
public class TcpClient {
    

    // 要请求的服务器的ip地址
    private String ip;
    // 服务器的端口
    private int port;

    public TcpClient(String ip, int port){
    
        this.ip = ip;
        this.port = port;
        this.connect(ip,port);
    }

    @SneakyThrows
    public void connect(String host, int port){
    
        EventLoopGroup group = new NioEventLoopGroup();
        try {
    
            Bootstrap b = new Bootstrap();
            b.group(group);
            b.channel(NioSocketChannel.class);
            b.option(ChannelOption.SO_KEEPALIVE, true);
            b.handler(new TcpClientInitializer()); // 初始化

            //开启客户端
            ChannelFuture f = b.connect(host, port).sync();
            System.out.println("向 ip: " + host + " , port:" + port + " 的服务器 发送请求 ");

            if(f.isSuccess()){
    
                System.out.println("客户端启动成功");
            } else {
    
                System.out.println("客户端启动失败");
                f.cause().printStackTrace();
            }

            //要发送的数据
            RpcRequest rpcRequest = new RpcRequest();
            rpcRequest.setId("100");
            rpcRequest.setData("我是 tcp 客户端。。。。。。。。。。。。");

            //输出
            f.channel().writeAndFlush(rpcRequest);

            //等待直到连接中断
            f.channel().closeFuture().sync();
        } finally {
    
            group.shutdownGracefully();
        }
    }



    public static void main(String[] args){
    
        new TcpClient("127.0.0.1", 8800);
    }
}

TcpClientInitializer 初始化类

package com.example.nettydemo.eyue.tcpClient;


import com.example.nettydemo.eyue.RpcDecoder;
import com.example.nettydemo.eyue.RpcEncoder;
import com.example.nettydemo.eyue.entiy.RpcRequest;
import com.example.nettydemo.eyue.entiy.RpcResponse;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;

/**
 * @author dong
 * @date 2022/4/2
 * @AapiNote
 */
public class TcpClientInitializer extends ChannelInitializer<SocketChannel> {
    
    @Override
    protected void initChannel(SocketChannel socketChannel){
    
        System.out.println("初始化通道: Tcp client initChannel..");
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast(new RpcEncoder(RpcRequest.class));//TCP编码
        pipeline.addLast(new RpcDecoder(RpcResponse.class));//TCP解码
        pipeline.addLast(new TcpClientHandler());//业务处理器
    }
}

TcpClientHandler 处理类

package com.example.nettydemo.eyue.tcpClient;


import com.example.nettydemo.eyue.entiy.RpcResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author dong
 * @date 2022/3/31
 * @AapiNote
 */
public class TcpClientHandler extends SimpleChannelInboundHandler<RpcResponse> {
    


    /**
     * 读取 响应的结果
     * @param channelHandlerContext
     * @param response
     * @throws Exception
     */
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, RpcResponse response) throws Exception {
    
        System.out.println("接受到server响应数据: " + response.toString());
    }

    // 数据读取完毕的处理
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    
        System.err.println("客户端读取数据完毕");
    }

    // 出现异常的处理
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    
        System.err.println("client 读取数据出现异常");
        ctx.close();
    }
}

Tcp Server

NettyTcpServer 服务端

package com.example.nettydemo.eyue.tcpserver;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;

/**
 * @author dong
 * @date 2022/3/31
 * @AapiNote
 */
@Slf4j
public class NettyTcpServer implements Runnable{
    
    int port ;

    public NettyTcpServer(int port){
    
        this.port = port;
    }

    EventLoopGroup boss = new NioEventLoopGroup();
    EventLoopGroup work = new NioEventLoopGroup();

    @Override
    public void run() {
    
        try {
    
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(boss,work)
                    .handler(new LoggingHandler(LogLevel.DEBUG))
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new TcpServerInitializer());
//        .childHandler(new TcpServerInitializer())


            ChannelFuture f = bootstrap.bind(new InetSocketAddress(port)).sync();
//        System.out.println(" server start up on port : " + port);

            if (f.isSuccess()) {
    
                System.out.println("Tcp服务端启动成功");
            } else {
    
                System.out.println("Tcp服务端启动失败");
                f.cause().printStackTrace();
            }

            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
    
            e.printStackTrace();
        } finally {
    
            boss.shutdownGracefully();
            work.shutdownGracefully();
        }
    }

TcpServerInitializer 初始化类

package com.example.nettydemo.eyue.tcpserver;

import com.example.nettydemo.eyue.RpcDecoder;
import com.example.nettydemo.eyue.RpcEncoder;
import com.example.nettydemo.eyue.entiy.RpcRequest;
import com.example.nettydemo.eyue.entiy.RpcResponse;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;

/**
 * @author dong
 * @date 2022/4/2
 * @AapiNote
 */
public class TcpServerInitializer extends ChannelInitializer<SocketChannel> {
    

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
    
        System.out.println("初始化通道: Tcp server initChannel..");
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast(new RpcDecoder(RpcRequest.class));
        pipeline.addLast(new RpcEncoder(RpcResponse.class));
        pipeline.addLast(new TcpServerRequestHandler());
    }
}

TcpServerRequestHandler 处理类

package com.example.nettydemo.eyue.tcpserver;


import com.example.nettydemo.eyue.entiy.RpcRequest;
import com.example.nettydemo.eyue.entiy.RpcResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.net.SocketAddress;
import java.util.UUID;

/**
 * @author dong
 * @date 2022/4/2
 * @AapiNote
 */
public class TcpServerRequestHandler extends ChannelInboundHandlerAdapter {
    

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    
        //返回此通道绑定到的本地地址(IP 地址 和 端口号)
        SocketAddress socketAddress = ctx.channel().localAddress();
        String str = socketAddress.toString();
        System.out.println("ip"+str);
        //截取端口号
        String str1=str.substring(0, str.indexOf(":"));
        String str2=str.substring(str1.length()+1, str.length());
        System.out.println("port: "+str2);



       /* 接收到 客户端发送来的数据 */
        RpcRequest request = (RpcRequest) msg;
        System.out.println("接收到客户端信息:" + request.toString());


        /* 返回 响应给 客户端 */
        RpcResponse response = new RpcResponse();
        response.setId(UUID.randomUUID().toString());
        response.setData("server响应结果");
        response.setStatus(1);
        //输出 数据 到 客户端
        ctx.writeAndFlush(response);
    }


    //通知处理器最后的channelRead()是当前批处理中的最后一条消息时调用
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    
        System.out.println("服务端接收数据完毕..");
        ctx.flush();
    }

    //读操作时捕获到异常时调用
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    
        ctx.close();
    }

    //客户端去和服务端连接成功时触发
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
    
        ctx.writeAndFlush("hello client");
    }
}

ThreadPoolConfig 线程池配置类

package com.example.nettydemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author dong
 * @date 2022/4/2
 * @AapiNote
 */
@Configuration
@EnableAsync
public class ThreadPoolConfig {
    

    @Bean("taskExecutor")
    public Executor taskExecutor() {
    
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        //设置线程池参数信息
        taskExecutor.setCorePoolSize(10);
        taskExecutor.setMaxPoolSize(50);
        taskExecutor.setQueueCapacity(200);
        taskExecutor.setKeepAliveSeconds(60);
        taskExecutor.setThreadNamePrefix("myExecutor--");
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        taskExecutor.setAwaitTerminationSeconds(60);
        //修改拒绝策略为使用当前线程执行
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //初始化线程池
        taskExecutor.initialize();
        return taskExecutor;
    }
}

springboot 启动类

package com.example.nettydemo;

import com.example.nettydemo.config.ThreadPoolConfig;
import com.example.nettydemo.eyue.httpserver.NettyHttpServer;
import com.example.nettydemo.eyue.tcpserver.NettyTcpServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.Executor;


@EnableAsync	//开启异步线程
@SpringBootApplication
public class NettyDemoApplication {
    

	public static void main(String[] args) {
    
		SpringApplication.run(NettyDemoApplication.class, args);

		ThreadPoolConfig threadPoolConfig = new ThreadPoolConfig();
		Executor executor = threadPoolConfig.taskExecutor();
		executor.execute(new NettyTcpServer(8800));
		executor.execute(new NettyHttpServer(8801));
	}

}

结果

HTTP服务端启动成功
Tcp服务端启动成功
初始化通道: Tcp server initChannel..
ip/127.0.0.1:8800
port: 8800
接收到客户端信息:RpcRequest{
    id='100', data=我是 tcp 客户端。。。。。。。。。。。。}
服务端接收数据完毕..
初始化通道: Http server initChannel..
ip/127.0.0.1:8801
port: 8801
收到客户端发来的消息http://127.0.0.1:8800:/test?data=100
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_44492502/article/details/123923033

智能推荐

【基础算法】反转链表的三种方法_链表反转-程序员宅基地

文章浏览阅读3.3k次,点赞13次,收藏20次。【基础算法】反转链表的三种方法_链表反转

使用遗传算法优化的BP神经网络实现自变量降维_bp神经网络优化-程序员宅基地

文章浏览阅读317次。本次利用遗传算法筛选出最具有代表的自变量,再利用BP神经网络进行预测。_bp神经网络优化

第十四届蓝桥杯青少组选拔赛Python真题 (2022年11月27日),包含答案_python 蓝桥杯真题-程序员宅基地

文章浏览阅读2.5k次,点赞2次,收藏6次。初始客户编号为 1、2、3 的客户分别在 1、2、3 窗口同时办理业务;窗口 3 用时 2 分钟办理完 3 号客户的业务,变为空闲状态,并按顺序呼叫 4 号客户,4 号客户用时 4 分钟窗口 1 用时 3 分钟办理完 1 号客户的业务,变为空闲状态,并按顺序呼叫 5 号客户,5 号客户用时 7 分钟。例如: N=3.M=7、从编号3 的位置到综号 7 的位置共有5 条路线,分别为: (3->5->7),(3-5->6->7,(3->4-5->7(3->4->5->6>7) ,(3->4>6->7)。_python 蓝桥杯真题

基于RK3399 Android11适配OV13850 MIPI摄像头_camera_etc.mk-程序员宅基地

文章浏览阅读1.4k次,点赞22次,收藏21次。基于RK3399 Android11 适配MIPI摄像头 OV13850_camera_etc.mk

2020公文格式模板及范文_公文写作格式+请示、报告、会议纪要基本模板-程序员宅基地

文章浏览阅读6.4k次。一、排版1.WORD文档页面设置,页边距:上3.7cm,下3.5cm,左2.8cm,右2.6cm。具体操作过程中,根据版式要求,也可适当调整。2.字体要求:文章标题为二号方正小标宋、居中,标题内容多,可分多行,排成梯形或菱形,标题内容换行时注意词意完整,标题行距选择磅值约30-34之间,可根据版面自行设定。正文为三号仿宋,行距选择1.5倍行距,也可根据页面做适当调整。3.正文结束空2-3..._请示的页边距

Pandas-处理文本字符串(拼接)_pandas字符串拼接-程序员宅基地

文章浏览阅读6.8k次。Pandas提供了不同的方法将序列或索引与他们自己或者其他的对象进行拼接,所有的方法都是基于各自的cat()方法1.将单个序列拼接为一个完整字符串输出:2. 如果没有额外声明,sep即分隔符默认为空字串,即sep='':输出:3.默认情况下,缺失值会被忽略。使用na_rep参数,可以对缺失值进行赋值:输出:4.拼接序列和其他类列表型对象为新的序列cat()的第一个参数为类列表对象,但必须要确保长度与序列或索引相同.输出:..._pandas字符串拼接

随便推点

LLaMA 2 - 最全资源汇总,你想要的都有_llama-2-7b-chat-gguf 百度网盘-程序员宅基地

文章浏览阅读553次。LLaMA 2 是 Meta 开发的大型语言模型,是 LLaMA 1 的后继者。LLaMA 2 可通过 AWS、Hugging Face 等提供商免费用于研究和商业用途。LLaMA 2 预训练模型接受了 2 万亿个标记的训练,上下文长度是 LLaMA 1 的两倍。其微调模型已经接受了超过 100 万个人工注释的训练。本文包含 LLama 2 所有相关资源,可帮助您快速入门。它包括以下链接:LLaMA 2 是什么?Lllama 2在线体验Llama2 背后的研究Llama 2 基准测试有多好。_llama-2-7b-chat-gguf 百度网盘

《C语言程序设计》谭浩强-学习笔记以及课后习题答案(考前复习/考研/专升本)_谭浩强课后习题csdn-程序员宅基地

文章浏览阅读1.8k次,点赞5次,收藏26次。《C语言程序设计》谭浩强-学习笔记-课后习题答案(考前复习/考研/专升本/)_谭浩强课后习题csdn

Knowledge Distillation by On-the-Fly Native Ensemble论文解读_on the fly 蒸馏-程序员宅基地

文章浏览阅读1.1k次,点赞2次,收藏2次。1. 网络结构: Gate为全连接网络,用来学习哪个网络更重要。目前利用全连接网络选择网络部件重要性的方法很流行。“三个臭皮匠顶个诸葛亮?”,感觉很像bagging方法。2. 损失函数: 训练时softmax都有温度T=3蒸馏,测试时就恢复T=1。 最终的Loss 第一项代表各个分支的损失,第二项代表最后Teacher的损失,第三项代表各..._on the fly 蒸馏

Mac 屏幕录制 权限 没有可勾选或添加的App选项 产生原因和解决办法_mac屏幕录制没有微信选项-程序员宅基地

文章浏览阅读4.1w次,点赞12次,收藏39次。遇到问题:安装软件需要获取截屏和屏幕录制的功能权限,但是发现打开系统设置->安全性与隐私-屏幕录制,右边竟然没有可勾选或可添加的App选项产生原因:经过网络搜索关键字,发现是因为系统升级到10.5,MacCataLina过程中位于/Library/Application Support/com.apple.TCC目录下的TCC.db文件损坏了。期间遇到的错误提示(Error: table access has 7 columns but 12 values were supplied)..._mac屏幕录制没有微信选项

python一些练手小项目_pycharm 练手程序-程序员宅基地

文章浏览阅读260次。python一些练手小项目参考Pycharm+django2.2+python3.6+MySQL实现简单的考试报名系统Pycharm+Django之使用模型django基础之数据库操作使用pycharm调试django项目_pycharm 练手程序

汇编语言 第三版 王爽 实验四_汇编语言第三版实验4第三题-程序员宅基地

文章浏览阅读8.2k次,点赞4次,收藏17次。百度文库答案有误。特写此博客。_汇编语言第三版实验4第三题

推荐文章

热门文章

相关标签