使用 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

智能推荐

c# 调用c++ lib静态库_c#调用lib-程序员宅基地

文章浏览阅读2w次,点赞7次,收藏51次。四个步骤1.创建C++ Win32项目动态库dll 2.在Win32项目动态库中添加 外部依赖项 lib头文件和lib库3.导出C接口4.c#调用c++动态库开始你的表演...①创建一个空白的解决方案,在解决方案中添加 Visual C++ , Win32 项目空白解决方案的创建:添加Visual C++ , Win32 项目这......_c#调用lib

deepin/ubuntu安装苹方字体-程序员宅基地

文章浏览阅读4.6k次。苹方字体是苹果系统上的黑体,挺好看的。注重颜值的网站都会使用,例如知乎:font-family: -apple-system, BlinkMacSystemFont, Helvetica Neue, PingFang SC, Microsoft YaHei, Source Han Sans SC, Noto Sans CJK SC, W..._ubuntu pingfang

html表单常见操作汇总_html表单的处理程序有那些-程序员宅基地

文章浏览阅读159次。表单表单概述表单标签表单域按钮控件demo表单标签表单标签基本语法结构<form action="处理数据程序的url地址“ method=”get|post“ name="表单名称”></form><!--action,当提交表单时,向何处发送表单中的数据,地址可以是相对地址也可以是绝对地址--><!--method将表单中的数据传送给服务器处理,get方式直接显示在url地址中,数据可以被缓存,且长度有限制;而post方式数据隐藏传输,_html表单的处理程序有那些

PHP设置谷歌验证器(Google Authenticator)实现操作二步验证_php otp 验证器-程序员宅基地

文章浏览阅读1.2k次。使用说明:开启Google的登陆二步验证(即Google Authenticator服务)后用户登陆时需要输入额外由手机客户端生成的一次性密码。实现Google Authenticator功能需要服务器端和客户端的支持。服务器端负责密钥的生成、验证一次性密码是否正确。客户端记录密钥后生成一次性密码。下载谷歌验证类库文件放到项目合适位置(我这边放在项目Vender下面)https://github.com/PHPGangsta/GoogleAuthenticatorPHP代码示例://引入谷_php otp 验证器

【Python】matplotlib.plot画图横坐标混乱及间隔处理_matplotlib更改横轴间距-程序员宅基地

文章浏览阅读4.3k次,点赞5次,收藏11次。matplotlib.plot画图横坐标混乱及间隔处理_matplotlib更改横轴间距

docker — 容器存储_docker 保存容器-程序员宅基地

文章浏览阅读2.2k次。①Storage driver 处理各镜像层及容器层的处理细节,实现了多层数据的堆叠,为用户 提供了多层数据合并后的统一视图②所有 Storage driver 都使用可堆叠图像层和写时复制(CoW)策略③docker info 命令可查看当系统上的 storage driver主要用于测试目的,不建议用于生成环境。_docker 保存容器

随便推点

网络拓扑结构_网络拓扑csdn-程序员宅基地

文章浏览阅读834次,点赞27次,收藏13次。网络拓扑结构是指计算机网络中各组件(如计算机、服务器、打印机、路由器、交换机等设备)及其连接线路在物理布局或逻辑构型上的排列形式。这种布局不仅描述了设备间的实际物理连接方式,也决定了数据在网络中流动的路径和方式。不同的网络拓扑结构影响着网络的性能、可靠性、可扩展性及管理维护的难易程度。_网络拓扑csdn

JS重写Date函数,兼容IOS系统_date.prototype 将所有 ios-程序员宅基地

文章浏览阅读1.8k次,点赞5次,收藏8次。IOS系统Date的坑要创建一个指定时间的new Date对象时,通常的做法是:new Date("2020-09-21 11:11:00")这行代码在 PC 端和安卓端都是正常的,而在 iOS 端则会提示 Invalid Date 无效日期。在IOS年月日中间的横岗许换成斜杠,也就是new Date("2020/09/21 11:11:00")通常为了兼容IOS的这个坑,需要做一些额外的特殊处理,笔者在开发的时候经常会忘了兼容IOS系统。所以就想试着重写Date函数,一劳永逸,避免每次ne_date.prototype 将所有 ios

如何将EXCEL表导入plsql数据库中-程序员宅基地

文章浏览阅读5.3k次。方法一:用PLSQL Developer工具。 1 在PLSQL Developer的sql window里输入select * from test for update; 2 按F8执行 3 打开锁, 再按一下加号. 鼠标点到第一列的列头,使全列成选中状态,然后粘贴,最后commit提交即可。(前提..._excel导入pl/sql

Git常用命令速查手册-程序员宅基地

文章浏览阅读83次。Git常用命令速查手册1、初始化仓库git init2、将文件添加到仓库git add 文件名 # 将工作区的某个文件添加到暂存区 git add -u # 添加所有被tracked文件中被修改或删除的文件信息到暂存区,不处理untracked的文件git add -A # 添加所有被tracked文件中被修改或删除的文件信息到暂存区,包括untracked的文件...

分享119个ASP.NET源码总有一个是你想要的_千博二手车源码v2023 build 1120-程序员宅基地

文章浏览阅读202次。分享119个ASP.NET源码总有一个是你想要的_千博二手车源码v2023 build 1120

【C++缺省函数】 空类默认产生的6个类成员函数_空类默认产生哪些类成员函数-程序员宅基地

文章浏览阅读1.8k次。版权声明:转载请注明出处 http://blog.csdn.net/irean_lau。目录(?)[+]1、缺省构造函数。2、缺省拷贝构造函数。3、 缺省析构函数。4、缺省赋值运算符。5、缺省取址运算符。6、 缺省取址运算符 const。[cpp] view plain copy_空类默认产生哪些类成员函数

推荐文章

热门文章

相关标签