簡單的Socket通信(簡單的線上聊天)---winform

来源:https://www.cnblogs.com/liangweitao/archive/2019/05/14/10862611.html
-Advertisement-
Play Games

註:本博客適合剛開始學習winform程式的初學者,大牛請繞道(跪求大牛指導文中不足) .....10w字廢話自動省略,直接開始正題. 首先從最基本的建立winform開始(本項目用的Vs2017) 新建->項目->選中C#->選擇Windows窗體應用->確定 創建完成後可以點擊工具欄進行拖拽控制項 ...


註:本博客適合剛開始學習winform程式的初學者,大牛請繞道(跪求大牛指導文中不足)

.....10w字廢話自動省略,直接開始正題.

首先從最基本的建立winform開始(本項目用的Vs2017)

 新建->項目->選中C#->選擇Windows窗體應用->確定

創建完成後可以點擊工具欄進行拖拽控制項 ,雙擊控制項進入事件的邏輯處理,右擊滑鼠選擇屬性進行配置

各屬性的作用可參考微軟文檔     https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.forms.control.accessiblename?view=netframework-4.8

 

 

首先展示一下效果圖

 

 

註:

博主在一臺電腦上創建了兩個項目,打開兩個VisualStudio(一個運行服務端,一個運行客戶端),

由於socked埠占用問題,

服務端和客戶端運行起來後,

         客戶端IP輸入自己電腦的IP->埠port隨便輸入->點擊連接,提示連接成功。例:IP:192.168.1.129  port:80

         服務端IP輸入自己電腦IP->埠port隨便輸入--->點擊開始監聽,提示監聽成功。例:IP:192.168.1.129 port:81

         客戶端IP輸入自己電腦IP->埠port輸入服務端埠號->點擊連接,提示連接成功可以開始發送消息:例:IP:192.168.1.129 port:81

(從上到下按順序執行)

以上操作是因為服務端、客戶端在同一臺電腦上,造成埠占用,  如果服務端和客戶端分開的話客戶端可以直接輸入服務端ip和埠連接監聽。

 

 

 

 

接下來貼代碼:

服務端

↓↓↓↓↓

↓↓↓

↓↓

Designer.cs文件代碼     (小白不會設置控制項屬性的話,可以跳過拖拽控制項,直接複製以下代碼)

↓↓↓↓↓↓↓

↓↓↓↓

↓↓

namespace Mywindows
{
    partial class Form1
    {
        /// <summary>
        /// 必需的設計器變數。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的資源。
        /// </summary>
        /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗體設計器生成的代碼

        /// <summary>
        /// 設計器支持所需的方法 - 不要修改
        /// 使用代碼編輯器修改此方法的內容。
        /// </summary>
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.tb_ip = new System.Windows.Forms.TextBox();
            this.lb_Ip = new System.Windows.Forms.Label();
            this.lb_port = new System.Windows.Forms.Label();
            this.tb_port = new System.Windows.Forms.TextBox();
            this.bt_connnect = new System.Windows.Forms.Button();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.txt_msg = new System.Windows.Forms.TextBox();
            this.bt_send = new System.Windows.Forms.Button();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.bt_connnect);
            this.panel1.Controls.Add(this.lb_port);
            this.panel1.Controls.Add(this.tb_port);
            this.panel1.Controls.Add(this.lb_Ip);
            this.panel1.Controls.Add(this.tb_ip);
            this.panel1.Location = new System.Drawing.Point(21, 12);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(580, 70);
            this.panel1.TabIndex = 0;
            // 
            // tb_ip
            // 
            this.tb_ip.Location = new System.Drawing.Point(44, 18);
            this.tb_ip.Name = "tb_ip";
            this.tb_ip.Size = new System.Drawing.Size(100, 21);
            this.tb_ip.TabIndex = 0;
            this.tb_ip.Text = "127.0.0.1";
            // 
            // lb_Ip
            // 
            this.lb_Ip.AutoSize = true;
            this.lb_Ip.Location = new System.Drawing.Point(15, 21);
            this.lb_Ip.Name = "lb_Ip";
            this.lb_Ip.Size = new System.Drawing.Size(23, 12);
            this.lb_Ip.TabIndex = 1;
            this.lb_Ip.Text = "IP:";
            // 
            // lb_port
            // 
            this.lb_port.AutoSize = true;
            this.lb_port.Location = new System.Drawing.Point(158, 24);
            this.lb_port.Name = "lb_port";
            this.lb_port.Size = new System.Drawing.Size(35, 12);
            this.lb_port.TabIndex = 3;
            this.lb_port.Text = "Port:";
            // 
            // tb_port
            // 
            this.tb_port.Location = new System.Drawing.Point(199, 21);
            this.tb_port.Name = "tb_port";
            this.tb_port.Size = new System.Drawing.Size(100, 21);
            this.tb_port.TabIndex = 2;
            this.tb_port.Text = "80";
            // 
            // bt_connnect
            // 
            this.bt_connnect.Location = new System.Drawing.Point(316, 15);
            this.bt_connnect.Name = "bt_connnect";
            this.bt_connnect.Size = new System.Drawing.Size(132, 42);
            this.bt_connnect.TabIndex = 4;
            this.bt_connnect.Text = "開始監聽";
            this.bt_connnect.UseVisualStyleBackColor = true;
            this.bt_connnect.Click += new System.EventHandler(this.bt_connnect_Click);
            // 
            // listBox1
            // 
            this.listBox1.FormattingEnabled = true;
            this.listBox1.ItemHeight = 12;
            this.listBox1.Location = new System.Drawing.Point(21, 112);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(580, 124);
            this.listBox1.TabIndex = 1;
            // 
            // txt_msg
            // 
            this.txt_msg.Location = new System.Drawing.Point(21, 255);
            this.txt_msg.Multiline = true;
            this.txt_msg.Name = "txt_msg";
            this.txt_msg.Size = new System.Drawing.Size(424, 73);
            this.txt_msg.TabIndex = 2;
            // 
            // bt_send
            // 
            this.bt_send.Location = new System.Drawing.Point(471, 266);
            this.bt_send.Name = "bt_send";
            this.bt_send.Size = new System.Drawing.Size(119, 52);
            this.bt_send.TabIndex = 3;
            this.bt_send.Text = "發送";
            this.bt_send.UseVisualStyleBackColor = true;
            this.bt_send.Click += new System.EventHandler(this.bt_send_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(695, 340);
            this.Controls.Add(this.bt_send);
            this.Controls.Add(this.txt_msg);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "SocketForm";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        #endregion
        private System.Windows.Forms.Panel panel1;
         private System.Windows.Forms.Button bt_connnect;
         private System.Windows.Forms.Label lb_port;
         private System.Windows.Forms.TextBox tb_port;
         private System.Windows.Forms.Label lb_Ip;
         private System.Windows.Forms.TextBox tb_ip;
         private System.Windows.Forms.ListBox listBox1;
         private System.Windows.Forms.TextBox txt_msg;
         private System.Windows.Forms.Button bt_send;

    }
}

 控制項事件邏輯處理   (小白可以隨便選中一個控制項雙擊進去, 直接將下麵的代碼覆蓋到項目中)

↓↓↓↓↓↓↓

↓↓↓↓

↓↓

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mywindows
{
    partial class Form1 : Form
    {

        public Form1()
         {
             InitializeComponent();
         }
 
         private void bt_connnect_Click(object sender, EventArgs e)
         {
 
             try
             {
                 //點擊開始監聽時 在服務端創建一個負責監聽IP和埠號的Socket
                 Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                 IPAddress ip = IPAddress.Any;
                 //創建對象埠
                 IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(tb_port.Text));
 
                 socketWatch.Bind(point);//綁定埠號
                 ShowMsg("監聽成功!");
                 socketWatch.Listen(10);//設置監聽
 
                 //創建監聽線程
                 Thread thread = new Thread(Listen);
                 thread.IsBackground = true;
                 thread.Start(socketWatch);
             }
             catch { }
            
         }
        
         /// <summary>
         /// 等待客戶端的連接 並且創建與之通信的Socket
         /// </summary>
         Socket socketSend;
         void Listen(object o)
         {
             try
             {
                 Socket socketWatch = o as Socket;
                 while (true)
                 {
                    
                     socketSend = socketWatch.Accept();//等待接收客戶端連接
                     ShowMsg(socketSend.RemoteEndPoint.ToString() + ":" + "連接成功!");
                     //開啟一個新線程,執行接收消息方法
                     Thread r_thread = new Thread(Received);
                     r_thread.IsBackground = true;
                     r_thread.Start(socketSend);
                 }
             }
             catch { }
         }
         /// <summary>
         /// 伺服器端不停的接收客戶端發來的消息
         /// </summary>
         /// <param name="o"></param>
         void Received(object o)
         {
             try
             {
                 Socket socketSend = o as Socket;
                 while (true)
                 {
                     //客戶端連接伺服器成功後,伺服器接收客戶端發送的消息
                     byte[] buffer = new byte[1024 * 1024 * 3];
                     //實際接收到的有效位元組數
                     int len = socketSend.Receive(buffer);
                     if (len == 0)
                     {
                         break;
                     }
                     string str = Encoding.UTF8.GetString(buffer, 0, len);
                     ShowMsg(socketSend.RemoteEndPoint + ":" + str);
                 }
             }
             catch { }
         }
         /// <summary>
         /// 伺服器向客戶端發送消息
         /// </summary>
         /// <param name="str"></param>
         void Send(string str)
{
                 byte[] buffer = Encoding.UTF8.GetBytes(str);
                 socketSend.Send(buffer);
             }
 
         void ShowMsg(string msg)
         {
             listBox1.Items.Add(msg + "\r\n");
         }
 
         private void Form1_Load(object sender, EventArgs e)
         {
             Control.CheckForIllegalCrossThreadCalls = false;
         }
 
         private void bt_send_Click(object sender, EventArgs e)
         {
             Send(txt_msg.Text.Trim());
         }

    }
}

 客戶端

↓↓↓↓↓↓

↓↓↓

↓↓

namespace MyClient
{
    partial class Form1
    {
        /// <summary>
        /// 必需的設計器變數。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的資源。
        /// </summary>
        /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗體設計器生成的代碼

        /// <summary>
        /// 設計器支持所需的方法 - 不要修改
        /// 使用代碼編輯器修改此方法的內容。
        /// </summary>
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.lb_ip = new System.Windows.Forms.Label();
            this.txt_ip = new System.Windows.Forms.TextBox();
            this.txt_port = new System.Windows.Forms.TextBox();
            this.lb_port = new System.Windows.Forms.Label();
            this.bt_connect = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.txt_msg = new System.Windows.Forms.TextBox();
            this.bt_send = new System.Windows.Forms.Button();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.bt_connect);
            this.panel1.Controls.Add(this.txt_port);
            this.panel1.Controls.Add(this.lb_port);
            this.panel1.Controls.Add(this.txt_ip);
            this.panel1.Controls.Add(this.lb_ip);
            this.panel1.Location = new System.Drawing.Point(12, 12);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(463, 75);
            this.panel1.TabIndex = 0;
            // 
            // lb_ip
            // 
            this.lb_ip.AutoSize = true;
            this.lb_ip.Location = new System.Drawing.Point(14, 23);
            this.lb_ip.Name = "lb_ip";
            this.lb_ip.Size = new System.Drawing.Size(23, 12);
            this.lb_ip.TabIndex = 0;
            this.lb_ip.Text = "IP:";
            // 
            // txt_ip
            // 
            this.txt_ip.Location = new System.Drawing.Point(43, 20);
            this.txt_ip.Name = "txt_ip";
            this.txt_ip.Size = new System.Drawing.Size(100, 21);
            this.txt_ip.TabIndex = 1;
            this.txt_ip.Text = "127.0.0.1";
            // 
            // txt_port
            // 
            this.txt_port.Location = new System.Drawing.Point(207, 23);
            this.txt_port.Name = "txt_port";
            this.txt_port.Size = new System.Drawing.Size(100, 21);
            this.txt_port.TabIndex = 3;
            this.txt_port.Text = "2001";
            // 
            // lb_port
            // 
            this.lb_port.AutoSize = true;
            this.lb_port.Location = new System.Drawing.Point(166, 29);
            this.lb_port.Name = "lb_port";
            this.lb_port.Size = new System.Drawing.Size(35, 12);
            this.lb_port.TabIndex = 2;
            this.lb_port.Text = "port:";
            // 
            // bt_connect
            // 
            this.bt_connect.Location = new System.Drawing.Point(313, 9);
            this.bt_connect.Name = "bt_connect";
            this.bt_connect.Size = new System.Drawing.Size(113, 47);
            this.bt_connect.TabIndex = 4;
            this.bt_connect.Text = "連接";
            this.bt_connect.UseVisualStyleBackColor = true;
            this.bt_connect.Click += new System.EventHandler(this.bt_connect_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(12, 103);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(463, 114);
            this.textBox1.TabIndex = 1;
            // 
            // txt_msg
            // 
            this.txt_msg.Location = new System.Drawing.Point(12, 246);
            this.txt_msg.Multiline = true;
            this.txt_msg.Name = "txt_msg";
            this.txt_msg.Size = new System.Drawing.Size(291, 54);
            this.txt_msg.TabIndex = 2;
            // 
            // bt_send
            // 
            this.bt_send.Location = new System.Drawing.Point(325, 246);
            this.bt_send.Name = "bt_send";
            this.bt_send.Size = new System.Drawing.Size(113, 54);
            this.bt_send.TabIndex = 3;
            this.bt_send.Text = "發送";
            this.bt_send.UseVisualStyleBackColor = true;
            this.bt_send.Click += new System.EventHandler(this.bt_send_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(493, 323);
            this.Controls.Add(this.bt_send);
            this.Controls.Add(this.txt_msg);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label lb_ip;
        private System.Windows.Forms.Button bt_connect;
        private System.Windows.Forms.TextBox txt_port;
        private System.Windows.Forms.Label lb_port;
        private System.Windows.Forms.TextBox txt_ip;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox txt_msg;
        private System.Windows.Forms.Button bt_send;

    }
}

雙擊控制項進入(.cs文件)

↓↓↓↓↓↓↓

↓↓↓↓

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace MyClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

               Socket socketSend;
         private void bt_connect_Click(object sender, EventArgs e)
         {
             try
             {
                 //創建客戶端Socket,獲得遠程ip和埠號
                 socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                 IPAddress ip = IPAddress.Parse(txt_ip.Text);
                 IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(txt_port.Text));
 
                 socketSend.Connect(point);
                 ShowMsg("連接成功!");
                 //開啟新的線程,不停的接收伺服器發來的消息
                 Thread c_thread = new Thread(Received);
                 c_thread.IsBackground = true;
                 c_thread.Start();
             }
             catch (Exception){
                 ShowMsg("IP或者埠號錯誤...");
             }
  
         }
         void ShowMsg(string str)
         {
             textBox1.AppendText(str + "\r\n");
         }
         /// <summary>
         /// 接收服務端返回的消息
         /// </summary>
         void Received()
         {
             while (true)
             {
                 try
                 {
                     byte[] buffer = new byte[1024 * 1024 * 3];
                     //實際接收到的有效位元組數
                     int len = socketSend.Receive(buffer);
                     if (len == 0)
                     {
                         break;
                     }
                     string str = Encoding.UTF8.GetString(buffer, 0, len);
                     ShowMsg(socketSend.RemoteEndPoint + ":" + str);
                 }
                 catch { }
             }
         } 
         /// <summary>
         /// 向伺服器發送消息
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void bt_send_Click(object sender, EventArgs e)
         {
             try
             {
                 string msg = txt_msg.Text.Trim();
                 byte[] buffer = new byte[1024 * 1024 * 3];
                 buffer = Encoding.UTF8.GetBytes(msg);
                 socketSend.Send(buffer);
             }
             catch { }
         }
 
         private void Form1_Load(object sender, EventArgs e)
         {
             Control.CheckForIllegalCrossThreadCalls = false;
         }
    }
}

 

 

 

Socket原理可參考 https://www.cnblogs.com/wangkaining/p/6089627.html

以上代碼複製粘貼完成後註意項目命名,可直接運行.有問題評論區探討.

 


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

-Advertisement-
Play Games
更多相關文章
  • 隨著動態語言的流行(Ruby,Groovy,Scala,Node.js),Java的開發顯得格外的笨重;繁多的配置,低下的開發效率,複雜的部署流程以及第三方技術集成難度大. 在上述環境 下,Spring Boot應運而生.它使用"習慣優於配置"(項目中存在大量的配置,此外還內置一個習慣性的配置,讓你 ...
  • java中的同步器是指什麼? 哪些類是使用AQS實現的? 分散式環境中怎麼實現同步? ...
  • 零基礎學編程,用python入門是個不錯的選擇。下麵我們就介紹一下0基礎學習Python的技巧和方法。當然,也包括一些學習的心態指導。 ...
  • 1. 前言 我去年寫過一個在UWP自定義控制項的 "系列博客" ,大部分的經驗都可以用在WPF中(只有一點小區別)。這篇文章的目的是快速入門自定義控制項的開發,所以儘量精簡了篇幅,更深入的概念在以後介紹各控制項的文章中實際運用到才介紹。 "ContentControl" 是WPF中最基礎的一種控制項,Win ...
  • 這裡講下,quartz這種任務調度程式的簡單使用 這是使用的quartz的3.x 版本 2.x 版本與此稍有區別,可以在網上查看2.x版本教程 使用語言為c# quartz的使用分為幾個步驟 其中2步驟也可以放到步驟5後面 另外,別忘記在nuget管理器中引入quartz 代碼如下 運行程式,效果圖 ...
  • 1新建NetCore項目,我這裡NetCoreSDK版本是2.2.0。 2.進入NuGet程式包官網 : https://www.nuget.org,搜索以下兩個包並安裝到項目中。 Microsoft.Extensions.Logging.Log4Net.AspNetCore Log4Net 打開項 ...
  • 背景 一、微信檢測手段 二、功能變數名稱被封常見因素 三、功能變數名稱檢測原理 四、檢測代碼(C#) 五、防封方案 六、參考資料 背景 最近因為業務需要,在研究微信跳轉,功能變數名稱防封檢測等東西,網上搜集了很多很多資料,發現居然這麼簡單的一點東西 居然有人專門做成系統拿去賣錢.. 系統功能就只是個微信跳轉而已,微信跳外部 ...
  • Visual Studio 2019 Enterprise(企業版)BF8Y8-GN2QH-T84XB-QVY3B-RC4DFVisual Studio 2019 Professional(專業版)NYWVH-HT4XC-R2WYW-9Y3CM-X4V3Y ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...