Java網路編程Socket

来源:https://www.cnblogs.com/cxy-sky/archive/2022/10/06/sky01.html
-Advertisement-
Play Games

作者:IT王小二 博客:https://itwxe.com 前面小二介紹過使用Typora+PicGo+LskyPro打造舒適寫作環境,那時候需要使用水印功能,但是小二在升級LskyPro2.x版本發現有很多不如人意的東西,遂棄用LskyPro使用MinIO結合代碼實現自己需要的圖床功能,也適合以後 ...


Java網路多線程

2022年8月31日

筆記 韓順平Java自學筆記 網路多線程_關註永雛塔菲喵的博客-CSDN博客

網課 【韓順平講Java】Java網路多線程專題 - TCP UDP Socket編程 多線程 併發處理 文件傳輸 新聞推送 Java_嗶哩嗶哩_bilibili

通信項目上傳到github:cxy-sky/JavaSocket: Java 網路多線程專題,包括 TCP UDP Socket編程 多線程 併發處理 文件傳輸 新聞推送 (github.com)
2022年9月7日完結

InetAddress

package com.jiao;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {

    public static void main(String[] args) throws UnknownHostException {
//       獲得本機的InetAddress對象
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost);
//      根據指定的主機名獲得InetAddress對象
        InetAddress byName = InetAddress.getByName("sky");
        System.out.println(byName);
//      根據功能變數名稱返回對應的ip
        InetAddress byName1 = InetAddress.getByName("www.baidu.com");
        System.out.println(byName1);
//      通過getHostAddress對象使他獲得對應的地址ipv4地址
        String hostAddress = byName1.getHostAddress();
        System.out.println(hostAddress);
//      獲取對應的功能變數名稱
        String hostName = byName1.getHostName();
        System.out.println(hostName);
    }
}

Socket

簡介

請添加圖片描述

理解

請添加圖片描述

TCP網路通信

簡介

請添加圖片描述

案例1 位元組流

服務端

package com.jiao.socket.tcp01;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * auth:@highSky
 * create:2022/8/31
 * email:[email protected]
 **/
public class Server {

    public static void main(String[] args) throws IOException {

//        要求電腦上沒有其他程式在監聽9999介面
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("伺服器在9999埠進行監聽");
        //程式阻塞,不再向下執行  如果有客戶端socket鏈接,返回一個socket鏈接,開始向下執行
        Socket socket = serverSocket.accept();
        System.out.println("客戶端 socket = "+socket.getClass());
//      客戶端沒有發送消息的話,會堵塞在這裡
        InputStream inputStream = socket.getInputStream();
        byte[] msg = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(msg)) != -1) {
            System.out.println("收到消息: " + socket.getLocalAddress().getHostName() + " " + new String(msg, 0, len));
        }
        inputStream.close();
        socket.close();
        serverSocket.close();
        System.out.println("服務關閉");
    }

}

客戶端

package com.jiao.socket.tcp01;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * auth:@highSky
 * create:2022/8/31
 * email:[email protected]
 **/
public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getLocalHost().getHostAddress(), 9999);
        System.out.println("正在發送消息");
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("hello jiaojiao".getBytes());
        //發送完成結束標記
        socket.shutdownOutput();
        System.out.println("消息發送完成");
        outputStream.close();
        socket.close();
    }
}

案例2 互發消息

server

package com.jiao.socket.tcp02;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * auth:@highSky
 * create:2022/8/31
 * email:[email protected]
 **/
public class Server {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("server" + InetAddress.getLocalHost().getHostAddress() + "正在監聽");
        Socket socket = serverSocket.accept();
        InputStream inputStream = socket.getInputStream();
        byte[] msg = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(msg)) != -1) {
            System.out.println("收到消息: " + socket.getLocalAddress().getHostName() + " " + new String(msg, 0, len));
        }
        
		//給client發送消息
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("hello tiantian".getBytes());
        //發送完成結束標記
        socket.shutdownOutput();

        outputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
        System.out.println("服務關閉");
    }

}

client

package com.jiao.socket.tcp02;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * auth:@highSky
 * create:2022/8/31
 * email:[email protected]
 **/
public class Client {
    public static void main(String[] args) throws IOException {
//        Socket socket = new Socket("106.14.219.106", 9999);
        Socket socket = new Socket(InetAddress.getLocalHost().getHostAddress(), 9999);
        System.out.println("正在發送消息");
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("hello jiaojiao".getBytes());
        //發送完成結束標記
        socket.shutdownOutput();
        System.out.println("消息發送完成");

        //接收服務端發來的消息
        InputStream inputStream = socket.getInputStream();
        byte[] msg = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(msg)) != -1) {
            System.out.println(new String(msg, 0, len));
        }

        inputStream.close();
        outputStream.close();
        socket.close();
    }
}

案例3 位元組流

使用writer時,寫入數據後,要調用flush刷新

//input位元組流轉換為reader字元流
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//output位元組流轉換為writer字元流
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
package com.jiao.socket.tcp03;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * auth:@highSky
 * create:2022/8/31
 * email:[email protected]
 **/
public class Server {

    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("server" + InetAddress.getLocalHost().getHostAddress() + "正在監聽");
        Socket socket = serverSocket.accept();
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String msg = bufferedReader.readLine();
        System.out.println("server 收到消息: " + socket.getLocalAddress().getHostName() + " " + msg);

        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write("hello tiantian");
        bufferedWriter.newLine();
        bufferedWriter.flush();

        bufferedWriter.close();
        bufferedReader.close();
        socket.close();
        serverSocket.close();
        System.out.println("服務關閉");
    }

}

客戶端

package com.jiao.socket.tcp03;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * auth:@highSky
 * create:2022/8/31
 * email:[email protected]
 **/
public class Client {
    public static void main(String[] args) throws IOException {
//        Socket socket = new Socket("106.14.219.106", 9999);
        Socket socket = new Socket(InetAddress.getLocalHost().getHostAddress(), 9999);
        System.out.println("正在發送消息");
        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write("hello jiaojiao");
        bufferedWriter.newLine();
        bufferedWriter.flush();
        System.out.println("消息發送完成");

        //接收服務端發來的消息
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String msg = bufferedReader.readLine();
        System.out.println("client 收到消息: " + socket.getLocalAddress().getHostName() + " " + msg);

        bufferedReader.close();
        bufferedWriter.close();
        socket.close();
    }
}

上傳文件

img

工具類

package com.jiao.upload;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
 * auth:@highSky
 * create:2022/9/1
 * email:[email protected]
 **/

public class StreamUtils {
    public static byte[] streamToByteArray(InputStream is) throws Exception {
        // 創建輸出流對象
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        // 位元組數組
        byte[] b = new byte[1024];
        int len;
        while ((len = is.read(b)) != -1) {
            // 迴圈讀取
            // 把讀取到的數據,寫入 bos
            bos.write(b, 0, len);
        }
        byte[] array = bos.toByteArray();
        bos.close();
        return array;
    }

    public static String streamToString(InputStream is) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line + "\r\n");
        }
        return builder.toString();
    }
}

server

package com.jiao.upload;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * auth:@highSky
 * create:2022/9/1
 * email:[email protected]
 **/
public class Server {

    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("》》服務開啟,等待連接,發送圖片");
        //得到連接
        Socket socket = serverSocket.accept();
        //準備圖片
        String imgName = "src/com/jiao/upload/jiaojiao.png";
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(imgName));
        //將圖片保存為byte數組中
        byte[] img = StreamUtils.streamToByteArray(bis);
        //獲取socket的輸出流
        OutputStream outputStream = socket.getOutputStream();
        //轉換為緩存輸出流
        BufferedOutputStream bos = new BufferedOutputStream(outputStream);
        bos.write(img);
        System.out.println("》》圖片發送完成");
        socket.shutdownOutput();

        //客戶端收到圖片後的反饋信息
        InputStream inputStream = socket.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        System.out.println(reader.readLine());
        bos.close();
        bis.close();
        reader.close();
        socket.close();

    }

}

client

package com.jiao.upload;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * auth:@highSky
 * create:2022/9/1
 * email:[email protected]
 **/
public class Client {

    public static void main(String[] args) throws Exception {
        Socket socket = new Socket(InetAddress.getLocalHost().getHostAddress(), 9999);
        System.out.println("》》等待圖片");

        //獲取socket接收到的二進位圖片數據
        InputStream inputStream = socket.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(inputStream);
        byte[] img = StreamUtils.streamToByteArray(bis);

        //保存到本地
        String imgName = "src/com/jiao/upload/tiantian.png";
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(imgName));
        bos.write(img);
        System.out.println("》》保存完成");


        //給服務端發送接受完成信息
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        writer.write(InetAddress.getLocalHost().getHostName() + "已收到圖片");
        writer.newLine();
        writer.flush();
        writer.close();
        bos.close();
        bis.close();
        socket.close();

    }

}

UDP

原理

請添加圖片描述

user1

package com.jiao.udp;

import java.io.IOException;
import java.net.*;

/**
 * auth:@highSky
 * create:2022/9/2
 * email:[email protected]
 **/
public class User1 {

    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(9999);
        //向user2發送消息
        byte[] bag = new byte[1024];
        DatagramPacket packet = new DatagramPacket(bag, bag.length, InetAddress.getByName("192.168.1.3"), 9998);
        byte[] msg = "Hello jiaojiao".getBytes();
        packet.setData(msg, 0, msg.length);
        socket.send(packet);

        System.out.println("發送完成");
        System.out.println("等待user2回覆");

        packet = new DatagramPacket(bag, bag.length);
        socket.receive(packet);
        msg = packet.getData();
        System.out.println(new String(msg, 0, msg.length));

        System.out.println("通信完成");
        socket.close();
    }
}

user2

package com.jiao.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * auth:@highSky
 * create:2022/9/2
 * email:[email protected]
 **/
public class User2 {

    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(9998);
        //接受user1的消息
        byte[] bag = new byte[1024];
        DatagramPacket packet = new DatagramPacket(bag, bag.length);
        System.out.println("等待user1的消息");
        socket.receive(packet);
        byte[] msg = packet.getData();
        System.out.println(new String(msg, 0, msg.length));
        //回覆user1
        packet = new DatagramPacket(bag, bag.length, InetAddress.getByName("192.168.1.3"), 9999);
        msg = "hello tiantian".getBytes();
        packet.setData(msg, 0, msg.length);
        socket.send(packet);
        System.out.println("通信結束");
        socket.close();
    }
}

練習

tcp

請添加圖片描述

server

package com.jiao.homework.test1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * auth:@highSky
 * create:2022/9/2
 * email:[email protected]
 **/
public class Server {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println(">>服務已開啟,等待連接");
        Socket socket = serverSocket.accept();
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//        System.out.println("client:  " + reader.readLine());

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        if ("name".equals(reader.readLine()))
            writer.write("nova");
        else
            writer.write("你說啥呢");
        writer.newLine();
        writer.flush();

        System.out.println("client:  " + reader.readLine());

        writer.write("編寫java程式");
        writer.newLine();
        writer.flush();

        writer.close();
        reader.close();
        socket.close();

    }
}

client

package com.jiao.homework.test1;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * auth:@highSky
 * create:2022/9/2
 * email:[email protected]
 **/
public class Client {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getLocalHost().getHostAddress(), 9999);
        System.out.println(">>給伺服器發送消息");
        //給server發送消息
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        writer.write("name");
        writer.newLine();
        writer.flush();


        //接受回覆
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//        System.out.println("server:  " + reader.readLine());
        if ("name".equals(reader.readLine()))
            writer.write("hobby");
        else
            writer.write("你說啥呢");
        writer.newLine();
        writer.flush();

        System.out.println("server:  " + reader.readLine());

        writer.close();
        reader.close();
        socket.close();

    }
}

udp

請添加圖片描述

userA

package com.jiao.homework.udp;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

/**
 * auth:@highSky
 * create:2022/9/2
 * email:[email protected]
 **/
public class UserA {

    public static void main(String[] args) throws IOException {

        Scanner in = new Scanner(System.in);

        //發送方
        DatagramSocket socket = new DatagramSocket(9999);

        byte[] bag = new byte[1024];
        DatagramPacket packet = new DatagramPacket(bag, bag.length, InetAddress.getByName("192.168.1.3"), 9998);
        System.out.println("請輸入問題:");
        String msgS = in.nextLine();
        byte[] msgB = msgS.getBytes();
        packet.setData(msgB);
        socket.send(packet);

        packet = new DatagramPacket(bag, bag.length);
        socket.receive(packet);
        msgB = packet.getData();
        System.out.println(new String(msgB, 0, packet.getLength()));

        socket.close();
    }

}

userB

package com.jiao.homework.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * auth:@highSky
 * create:2022/9/2
 * email:[email protected]
 **/
public class UserB {


    public static void main(String[] args) throws IOException {
        //接受方
        DatagramSocket socket = new DatagramSocket(9998);
        byte[] bag = new byte[1024];
        DatagramPacket packet = new DatagramPacket(bag, bag.length);
        socket.receive(packet);
        byte[] msgB = packet.getData();
        System.out.println(new String(msgB, 0, packet.getLength()));

        packet = new DatagramPacket(bag, bag.length, InetAddress.getByName("192.168.1.3"), 9999);
        packet.setData("四大名著為。。。。。。".getBytes());
        socket.send(packet);

        socket.close();
    }
}

文件傳輸

請添加圖片描述

server

package com.jiao.homework.trans;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * auth:@highSky
 * create:2022/9/3
 * email:[email protected]
 **/
public class Server {

    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(9999);
        //服務端等待客戶端下載請求
        System.out.println("等待請求");
        Socket socket = serverSocket.accept();
        //得到socket請求
        InputStream is = socket.getInputStream();
        byte[] data = new byte[1024];
        int len = 0;
        //文件名稱,這裡是單線程可以用stringBuilder
        StringBuilder fileName = new StringBuilder();
        while ((len = is.read(data, 0, data.length)) != -1) {
            fileName.append(new String(data, 0, len));
        }
        System.out.println(fileName);
        BufferedInputStream bis = null;
        if ("碧藍之海".equals(fileName.toString())) {
            fileName = new StringBuilder("src/com/jiao/homework/trans/碧藍之海.mp3");
        } else {
            fileName = new StringBuilder("src/com/jiao/homework/trans/sorry.txt");
        }
        bis = new BufferedInputStream(new FileInputStream(fileName.toString()));
        byte[] file = StreamUtils.streamToByteArray(bis);

        //發送給客戶端
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
        bos.write(file);
        bos.flush();
        socket.shutdownOutput();

        bos.close();
        bis.close();
        socket.close();
        serverSocket.close();

    }
}

client

package com.jiao.homework.trans;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * auth:@highSky
 * create:2022/9/3
 * email:[email protected]
 **/
public class Client {

    public static void main(String[] args) throws Exception {

        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入文件名");
        String filename = scanner.nextLine();
        //給服務端發送下載文件的文件名
        Socket socket = new Socket(InetAddress.getLocalHost().getHostAddress(), 9999);
        OutputStream os = socket.getOutputStream();
        os.write(filename.getBytes());
        os.flush();
        socket.shutdownOutput();
        //獲取返回的文件,下載
        //將文件讀取到數組中
        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
        byte[] file = StreamUtils.streamToByteArray(bis);
        //保存到本地
        if ("碧藍之海".equals(filename))
            filename = "E:\\" + filename + ".mp3";
        else
            filename = "E:\\" + filename + ".txt";

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filename));
        bos.write(file);
        bos.flush();

        bos.close();
        bis.close();
        os.close();
        socket.close();

    }
}

StreamUtils

package com.jiao.homework.trans;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * auth:@highSky
 * create:2022/9/1
 * email:[email protected]
 **/

public class StreamUtils {
    public static byte[] streamToByteArray(InputStream is) throws Exception {
        // 創建輸出流對象
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        // 位元組數組
        byte[] b = new byte[1024];
        int len;
        while ((len = is.read(b)) != -1) {
            // 迴圈讀取
            // 把讀取到的數據,寫入 bos
            bos.write(b, 0, len);
        }
        byte[] array = bos.toByteArray();
        bos.close();
        return array;
    }

    public static String streamToString(InputStream is) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line + "\r\n");
        }
        return builder.toString();
    }
}

項目-多用戶通信請添加圖片描述


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • admin後臺管理 django給您提供了一個可視化圖形界面,來方便您來對資料庫里的表進行增刪改查的管理 但是!使用admin後臺管理你自己註冊的模型表時,需要自行進行先註冊該表! 在應用下的admin.py里進行註冊: from django.contrib import admin from a ...
  • Google翻譯大概是目前機器翻譯中翻譯最為準確的了,本文分別使用了兩種可行的方式編寫了簡易的谷歌翻譯軟體。將詳細介紹調用谷歌翻譯API和自行定義谷歌翻譯介面的方式,最後講解如何通過pyqt5實現軟體UI界面並附上全部程式文件。要點如下:調用Google翻譯API方式、自行定義谷歌翻譯介面方式、谷歌... ...
  • 這篇博文在早前本人寫的介紹拼圖游戲的基礎上推出帶有GUI用戶界面的增強版,這裡將通過上、中、下三篇博文詳細介紹利用MATLAB GUI設計的拼圖游戲完整實現過程,每篇都會附上相應代碼及解釋。中篇主要講解拼圖游戲中游戲難度選擇(拼圖階數設置)與拼圖塊數字提示功能的詳細實現過程。中篇的要點如下:拼圖游戲... ...
  • 什麼是敏感性分析 敏感性分析(sensitivity analysis)是指從定量分析的角度研究有關因素髮生某種變化對某一個或一組關鍵指標影響程度的一種不確定分析技術。每個輸入的靈敏度用某個數值表示即敏感性指數(sensitivity index) 敏感性指數包括以下幾種: 一階指數:度量單個模型輸 ...
  • 1、概念 遞歸就是方法自己調用自己,每次調用時傳入不同的變數.遞歸有助於編程者解決複雜的問題,同時可以讓代碼變得簡潔。並且遞歸用到了虛擬機棧 2、能解決的問題 數學問題 八皇後問題 漢諾塔 求階乘 迷宮問題 球和籃子 各種排序演算法 3、規則 方法的變數是獨立的,不會相互影響的 如果方法中使用的是引用 ...
  • 前端代碼搭建 主要利用的是bootstrap3中js插件里的模態框版塊 <li><a href="" data-toggle="modal" data-target=".bs-example-modal-lg">修改密碼</a></li> <div class="modal fade bs-exam ...
  • python爬蟲瀏覽器偽裝 #導入urllib.request模塊 import urllib.request #設置請求頭 headers=("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, l ...
  • OpenAI Gym是一款用於研發和比較強化學習演算法的工具包,本文主要介紹Gym模擬環境的功能和工具包的使用方法,並詳細介紹其中的經典控制問題中的倒立擺(CartPole-v0/1)問題。最後針對倒立擺問題如何建立控制模型並採用爬山演算法優化進行了介紹,並給出了相應的完整python代碼示例和解釋。要... ...
一周排行
    -Advertisement-
    Play Games
  • 1、預覽地址:http://139.155.137.144:9012 2、qq群:801913255 一、前言 隨著網路的發展,企業對於信息系統數據的保密工作愈發重視,不同身份、角色對於數據的訪問許可權都應該大相徑庭。 列如 1、不同登錄人員對一個數據列表的可見度是不一樣的,如數據列、數據行、數據按鈕 ...
  • 前言 上一篇文章寫瞭如何使用RabbitMQ做個簡單的發送郵件項目,然後評論也是比較多,也是準備去學習一下如何確保RabbitMQ的消息可靠性,但是由於時間原因,先來說說設計模式中的簡單工廠模式吧! 在瞭解簡單工廠模式之前,我們要知道C#是一款面向對象的高級程式語言。它有3大特性,封裝、繼承、多態。 ...
  • Nodify學習 一:介紹與使用 - 可樂_加冰 - 博客園 (cnblogs.com) Nodify學習 二:添加節點 - 可樂_加冰 - 博客園 (cnblogs.com) 介紹 Nodify是一個WPF基於節點的編輯器控制項,其中包含一系列節點、連接和連接器組件,旨在簡化構建基於節點的工具的過程 ...
  • 創建一個webapi項目做測試使用。 創建新控制器,搭建一個基礎框架,包括獲取當天日期、wiki的請求地址等 創建一個Http請求幫助類以及方法,用於獲取指定URL的信息 使用http請求訪問指定url,先運行一下,看看返回的內容。內容如圖右邊所示,實際上是一個Json數據。我們主要解析 大事記 部 ...
  • 最近在不少自媒體上看到有關.NET與C#的資訊與評價,感覺大家對.NET與C#還是不太瞭解,尤其是對2016年6月發佈的跨平臺.NET Core 1.0,更是知之甚少。在考慮一番之後,還是決定寫點東西總結一下,也回顧一下.NET的發展歷史。 首先,你沒看錯,.NET是跨平臺的,可以在Windows、 ...
  • Nodify學習 一:介紹與使用 - 可樂_加冰 - 博客園 (cnblogs.com) Nodify學習 二:添加節點 - 可樂_加冰 - 博客園 (cnblogs.com) 添加節點(nodes) 通過上一篇我們已經創建好了編輯器實例現在我們為編輯器添加一個節點 添加model和viewmode ...
  • 前言 資料庫併發,數據審計和軟刪除一直是數據持久化方面的經典問題。早些時候,這些工作需要手寫複雜的SQL或者通過存儲過程和觸發器實現。手寫複雜SQL對軟體可維護性構成了相當大的挑戰,隨著SQL字數的變多,用到的嵌套和複雜語法增加,可讀性和可維護性的難度是幾何級暴漲。因此如何在實現功能的同時控制這些S ...
  • 類型檢查和轉換:當你需要檢查對象是否為特定類型,並且希望在同一時間內將其轉換為那個類型時,模式匹配提供了一種更簡潔的方式來完成這一任務,避免了使用傳統的as和is操作符後還需要進行額外的null檢查。 複雜條件邏輯:在處理複雜的條件邏輯時,特別是涉及到多個條件和類型的情況下,使用模式匹配可以使代碼更 ...
  • 在日常開發中,我們經常需要和文件打交道,特別是桌面開發,有時候就會需要載入大批量的文件,而且可能還會存在部分文件缺失的情況,那麼如何才能快速的判斷文件是否存在呢?如果處理不當的,且文件數量比較多的時候,可能會造成卡頓等情況,進而影響程式的使用體驗。今天就以一個簡單的小例子,簡述兩種不同的判斷文件是否... ...
  • 前言 資料庫併發,數據審計和軟刪除一直是數據持久化方面的經典問題。早些時候,這些工作需要手寫複雜的SQL或者通過存儲過程和觸發器實現。手寫複雜SQL對軟體可維護性構成了相當大的挑戰,隨著SQL字數的變多,用到的嵌套和複雜語法增加,可讀性和可維護性的難度是幾何級暴漲。因此如何在實現功能的同時控制這些S ...