資源簡介
c#源碼 Socket 負載均衡 實例
通過本實例可以方便的管理多服務器 大量客戶連接管理
可自動按連接數量分配適合的服務器
有助于想要實現這樣功能的朋友參考或改寫

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Configuration;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Net;
using?System.Net.Sockets;
using?System.Text;
using?System.Threading;
using?System.Web.script.Serialization;
using?System.Windows.Forms;
namespace?負載均衡
{
????public?partial?class?MainForm?:?Form
????{
????????///?
????????///?初始化
????????///?
????????public?MainForm()
????????{
????????????InitializeComponent();
????????????Config.Config.ServerInfoChangeEvent?=?ServerInfoChangeEvent;
????????}
????????///?
????????///?當前監聽線程
????????///?
????????private?Thread?Listener_Th?{?get;?set;?}
????????///?
????????///?當前監聽對象
????????///?
????????private?TcpListener?Listener?{?get;?set;?}
????????///?
????????///?開啟監聽按鈕被單擊事件
????????///?
????????///?
????????///?
????????private?void?Ts_Open_Click(object?sender?EventArgs?e)
????????{
????????????Config.Config.IsOpen?=?Ts_Open.Text?==?“開啟“;
????????????Ts_Open.Text?=?Config.Config.IsOpen???“關閉“?:?“開啟“;
????????????if?(Config.Config.IsOpen)?{
????????????????Listener_Th?=?new?Thread(TcpListener);
????????????????Listener_Th.IsBackground?=?true;
????????????????Listener_Th.Start();
????????????}
????????????else
????????????{
????????????????Listener_Th.Abort();
????????????????Listener.Stop();
????????????????Config.Config.InitializationServers();
????????????}
????????}
????????///?
????????///?服務器信息改變事件
????????///?
????????private?void?ServerInfoChangeEvent()
????????{
????????????this.Invoke(new?Action(()?=>
????????????{
????????????????dataGridView1.DataSource?=?Config.Config.GetServerList().ToList();
????????????????dataGridView1.Update();
????????????}));
????????}
????????///?
????????///?監聽主線程
????????///?
????????private?void?TcpListener()
????????{
????????????Listener?=?new?TcpListener(IPAddress.Parse(Config.Config.ServerAddress)?Config.Config.ListenerPort);
????????????Listener.Start();
????????????while?(Config.Config.IsOpen)
????????????{
????????????????var?P?=?Config.Config.GetServerInfo();
????????????????try
????????????????{
????????????????????TcpClient?NewClient?=?Listener.AcceptTcpClient();
????????????????????DateTime?dt?=?DateTime.Now;
????????????????????NewClient.SendTimeout?=?3000;
????????????????????NewClient.ReceiveTimeout?=?0;
????????????????????TcpClient?LocalClient;
????????????????????Boolean?IsConect?=?false;
????????????????????while?(!IsConect)
????????????????????{
????????????????????????try
????????????????????????{
????????????????????????????LocalClient?=?new?TcpClient(P.Address?P.Port);
????????????????????????????IsConect?=?true;
????????????????????????????LocalClient.SendTimeout?=?3000;
????????????????????????????LocalClie
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????..A..H.?????53248??2017-10-26?13:53??負載均衡\.vs\負載均衡\v14\.suo
?????文件????????335??2017-10-26?13:40??負載均衡\負載均衡\App.config
?????文件??????22528??2017-10-26?13:52??負載均衡\負載均衡\bin\Debug\負載均衡.exe
?????文件????????331??2017-10-26?17:11??負載均衡\負載均衡\bin\Debug\負載均衡.exe.config
?????文件??????46592??2017-10-26?13:52??負載均衡\負載均衡\bin\Debug\負載均衡.pdb
?????文件??????22688??2017-10-26?13:50??負載均衡\負載均衡\bin\Debug\負載均衡.vshost.exe
?????文件????????335??2017-10-26?13:40??負載均衡\負載均衡\bin\Debug\負載均衡.vshost.exe.config
?????文件????????490??2016-07-16?19:44??負載均衡\負載均衡\bin\Debug\負載均衡.vshost.exe.manifest
?????文件???????4605??2017-10-26?13:47??負載均衡\負載均衡\Config\Config.cs
?????文件???????1943??2017-10-26?13:20??負載均衡\負載均衡\Currency\UtilConvert.cs
?????文件????????609??2017-10-26?13:22??負載均衡\負載均衡\Info\ForwardInfo.cs
?????文件???????1081??2017-10-26?13:24??負載均衡\負載均衡\Info\ServerInfo.cs
?????文件???????6004??2017-10-26?13:51??負載均衡\負載均衡\MainForm.cs
?????文件???????5333??2017-10-26?13:39??負載均衡\負載均衡\MainForm.Designer.cs
?????文件??????11941??2017-10-26?13:39??負載均衡\負載均衡\MainForm.resx
?????文件???????1020??2017-10-25?15:48??負載均衡\負載均衡\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????7182??2017-10-26?13:50??負載均衡\負載均衡\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件???????3584??2017-10-25?15:48??負載均衡\負載均衡\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll
?????文件????????715??2017-10-26?13:52??負載均衡\負載均衡\obj\Debug\負載均衡.csproj.FileListAbsolute.txt
?????文件???????1015??2017-10-26?13:52??負載均衡\負載均衡\obj\Debug\負載均衡.csproj.GenerateResource.Cache
?????文件??????22528??2017-10-26?13:52??負載均衡\負載均衡\obj\Debug\負載均衡.exe
?????文件???????4289??2017-10-26?13:52??負載均衡\負載均衡\obj\Debug\負載均衡.MainForm.resources
?????文件??????46592??2017-10-26?13:52??負載均衡\負載均衡\obj\Debug\負載均衡.pdb
?????文件????????221??2017-10-26?13:52??負載均衡\負載均衡\obj\Debug\負載均衡.Properties.Resources.resources
?????文件????????496??2017-10-25?16:09??負載均衡\負載均衡\Program.cs
?????文件???????1332??2017-10-25?15:47??負載均衡\負載均衡\Properties\AssemblyInfo.cs
?????文件???????3153??2017-10-25?15:48??負載均衡\負載均衡\Properties\Resources.Designer.cs
?????文件???????5939??2017-10-25?15:48??負載均衡\負載均衡\Properties\Resources.resx
?????文件???????1099??2017-10-25?15:47??負載均衡\負載均衡\Properties\Settings.Designer.cs
?????文件????????249??2017-10-25?15:47??負載均衡\負載均衡\Properties\Settings.settings
............此處省略20個文件信息
- 上一篇:C# 掃雷游戲源碼 很適合新手
- 下一篇:C#.net udp 點對點聊天程序
評論
共有 條評論