初始化版本

This commit is contained in:
LL
2025-11-14 16:12:32 +08:00
commit ea40f18aa6
326 changed files with 137063 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>

View File

@@ -0,0 +1,460 @@

namespace Socket.Server
{
partial class FrmServer
{
/// <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()
{
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
this.chBoxIsHeartCheck = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtServerIp = new System.Windows.Forms.TextBox();
this.txtServerPort = new System.Windows.Forms.TextBox();
this.panel2 = new System.Windows.Forms.Panel();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.txtReceviceData = new System.Windows.Forms.RichTextBox();
this.panel4 = new System.Windows.Forms.Panel();
this.lblCurrentClient = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.groupBox5 = new System.Windows.Forms.GroupBox();
this.txtMessage = new System.Windows.Forms.RichTextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.txtSendData = new System.Windows.Forms.TextBox();
this.btnSendData = new System.Windows.Forms.Button();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.txtInterval = new System.Windows.Forms.TextBox();
this.btnListeningStop = new System.Windows.Forms.Button();
this.btnListeningStart = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.panel1 = new System.Windows.Forms.Panel();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.dgvClientView = new System.Windows.Forms.DataGridView();
this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.panel3 = new System.Windows.Forms.Panel();
this.lblClientCount = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.panel2.SuspendLayout();
this.groupBox1.SuspendLayout();
this.panel4.SuspendLayout();
this.groupBox5.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.panel1.SuspendLayout();
this.groupBox4.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dgvClientView)).BeginInit();
this.panel3.SuspendLayout();
this.SuspendLayout();
//
// chBoxIsHeartCheck
//
this.chBoxIsHeartCheck.AutoSize = true;
this.chBoxIsHeartCheck.Checked = true;
this.chBoxIsHeartCheck.CheckState = System.Windows.Forms.CheckState.Checked;
this.chBoxIsHeartCheck.Location = new System.Drawing.Point(141, 159);
this.chBoxIsHeartCheck.Name = "chBoxIsHeartCheck";
this.chBoxIsHeartCheck.Size = new System.Drawing.Size(72, 16);
this.chBoxIsHeartCheck.TabIndex = 0;
this.chBoxIsHeartCheck.Text = "心跳检测";
this.chBoxIsHeartCheck.UseVisualStyleBackColor = true;
this.chBoxIsHeartCheck.CheckedChanged += new System.EventHandler(this.chBoxIsHeartCheck_CheckedChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(58, 50);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(77, 12);
this.label1.TabIndex = 1;
this.label1.Text = "服务器地址:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(82, 105);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(53, 12);
this.label2.TabIndex = 2;
this.label2.Text = "端口号:";
//
// txtServerIp
//
this.txtServerIp.Location = new System.Drawing.Point(141, 46);
this.txtServerIp.Name = "txtServerIp";
this.txtServerIp.Size = new System.Drawing.Size(202, 21);
this.txtServerIp.TabIndex = 3;
this.txtServerIp.Text = "192.168.1.5";
//
// txtServerPort
//
this.txtServerPort.Location = new System.Drawing.Point(141, 101);
this.txtServerPort.Name = "txtServerPort";
this.txtServerPort.Size = new System.Drawing.Size(202, 21);
this.txtServerPort.TabIndex = 4;
this.txtServerPort.Text = "7001";
//
// panel2
//
this.panel2.Controls.Add(this.groupBox1);
this.panel2.Controls.Add(this.groupBox5);
this.panel2.Controls.Add(this.groupBox2);
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel2.Location = new System.Drawing.Point(410, 10);
this.panel2.Name = "panel2";
this.panel2.Padding = new System.Windows.Forms.Padding(10, 0, 0, 0);
this.panel2.Size = new System.Drawing.Size(628, 669);
this.panel2.TabIndex = 6;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.txtReceviceData);
this.groupBox1.Controls.Add(this.panel4);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.groupBox1.Location = new System.Drawing.Point(10, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(618, 460);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "已接收数据";
//
// txtReceviceData
//
this.txtReceviceData.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtReceviceData.Location = new System.Drawing.Point(3, 47);
this.txtReceviceData.Name = "txtReceviceData";
this.txtReceviceData.ReadOnly = true;
this.txtReceviceData.Size = new System.Drawing.Size(612, 410);
this.txtReceviceData.TabIndex = 0;
this.txtReceviceData.Text = "";
//
// panel4
//
this.panel4.Controls.Add(this.lblCurrentClient);
this.panel4.Controls.Add(this.label6);
this.panel4.Dock = System.Windows.Forms.DockStyle.Top;
this.panel4.Location = new System.Drawing.Point(3, 17);
this.panel4.Name = "panel4";
this.panel4.Size = new System.Drawing.Size(612, 30);
this.panel4.TabIndex = 1;
//
// lblCurrentClient
//
this.lblCurrentClient.AutoSize = true;
this.lblCurrentClient.Location = new System.Drawing.Point(126, 9);
this.lblCurrentClient.Name = "lblCurrentClient";
this.lblCurrentClient.Size = new System.Drawing.Size(17, 12);
this.lblCurrentClient.TabIndex = 1;
this.lblCurrentClient.Text = "无";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(18, 9);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(101, 12);
this.label6.TabIndex = 0;
this.label6.Text = "当前通讯客户端:";
//
// groupBox5
//
this.groupBox5.Controls.Add(this.txtMessage);
this.groupBox5.Dock = System.Windows.Forms.DockStyle.Bottom;
this.groupBox5.Location = new System.Drawing.Point(10, 460);
this.groupBox5.Name = "groupBox5";
this.groupBox5.Size = new System.Drawing.Size(618, 139);
this.groupBox5.TabIndex = 70;
this.groupBox5.TabStop = false;
this.groupBox5.Text = "消息";
//
// txtMessage
//
this.txtMessage.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtMessage.Location = new System.Drawing.Point(3, 17);
this.txtMessage.Name = "txtMessage";
this.txtMessage.ReadOnly = true;
this.txtMessage.Size = new System.Drawing.Size(612, 119);
this.txtMessage.TabIndex = 101;
this.txtMessage.Text = "";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.txtSendData);
this.groupBox2.Controls.Add(this.btnSendData);
this.groupBox2.Dock = System.Windows.Forms.DockStyle.Bottom;
this.groupBox2.Location = new System.Drawing.Point(10, 599);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(618, 70);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "发送数据";
//
// txtSendData
//
this.txtSendData.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtSendData.Location = new System.Drawing.Point(3, 17);
this.txtSendData.Multiline = true;
this.txtSendData.Name = "txtSendData";
this.txtSendData.Size = new System.Drawing.Size(537, 50);
this.txtSendData.TabIndex = 0;
//
// btnSendData
//
this.btnSendData.Dock = System.Windows.Forms.DockStyle.Right;
this.btnSendData.Location = new System.Drawing.Point(540, 17);
this.btnSendData.Name = "btnSendData";
this.btnSendData.Size = new System.Drawing.Size(75, 50);
this.btnSendData.TabIndex = 1;
this.btnSendData.Text = "发送数据";
this.btnSendData.UseVisualStyleBackColor = true;
this.btnSendData.Click += new System.EventHandler(this.btnSendData_Click);
//
// groupBox3
//
this.groupBox3.Controls.Add(this.txtInterval);
this.groupBox3.Controls.Add(this.btnListeningStop);
this.groupBox3.Controls.Add(this.btnListeningStart);
this.groupBox3.Controls.Add(this.txtServerPort);
this.groupBox3.Controls.Add(this.chBoxIsHeartCheck);
this.groupBox3.Controls.Add(this.label3);
this.groupBox3.Controls.Add(this.label2);
this.groupBox3.Controls.Add(this.txtServerIp);
this.groupBox3.Controls.Add(this.label1);
this.groupBox3.Dock = System.Windows.Forms.DockStyle.Top;
this.groupBox3.Location = new System.Drawing.Point(0, 0);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(400, 263);
this.groupBox3.TabIndex = 7;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "服务器参数设置";
//
// txtInterval
//
this.txtInterval.Location = new System.Drawing.Point(308, 157);
this.txtInterval.Name = "txtInterval";
this.txtInterval.Size = new System.Drawing.Size(35, 21);
this.txtInterval.TabIndex = 6;
this.txtInterval.Text = "500";
//
// btnListeningStop
//
this.btnListeningStop.Location = new System.Drawing.Point(219, 214);
this.btnListeningStop.Name = "btnListeningStop";
this.btnListeningStop.Size = new System.Drawing.Size(100, 36);
this.btnListeningStop.TabIndex = 5;
this.btnListeningStop.Text = "停止监听";
this.btnListeningStop.UseVisualStyleBackColor = true;
this.btnListeningStop.Click += new System.EventHandler(this.btnListeningStop_Click);
//
// btnListeningStart
//
this.btnListeningStart.Location = new System.Drawing.Point(89, 214);
this.btnListeningStart.Name = "btnListeningStart";
this.btnListeningStart.Size = new System.Drawing.Size(100, 36);
this.btnListeningStart.TabIndex = 5;
this.btnListeningStart.Text = "开启监听";
this.btnListeningStart.UseVisualStyleBackColor = true;
this.btnListeningStart.Click += new System.EventHandler(this.btnListeningStart_Click);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(234, 161);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(77, 12);
this.label3.TabIndex = 2;
this.label3.Text = "间隔(毫秒)";
//
// panel1
//
this.panel1.Controls.Add(this.groupBox4);
this.panel1.Controls.Add(this.groupBox3);
this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
this.panel1.Location = new System.Drawing.Point(10, 10);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(400, 669);
this.panel1.TabIndex = 8;
//
// groupBox4
//
this.groupBox4.Controls.Add(this.dgvClientView);
this.groupBox4.Controls.Add(this.panel3);
this.groupBox4.Dock = System.Windows.Forms.DockStyle.Fill;
this.groupBox4.Location = new System.Drawing.Point(0, 263);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(400, 406);
this.groupBox4.TabIndex = 8;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "客户端列表";
//
// dgvClientView
//
this.dgvClientView.AllowUserToAddRows = false;
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dgvClientView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
this.dgvClientView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgvClientView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column1,
this.Column2,
this.Column3,
this.Column4});
this.dgvClientView.Dock = System.Windows.Forms.DockStyle.Fill;
this.dgvClientView.Location = new System.Drawing.Point(3, 47);
this.dgvClientView.Name = "dgvClientView";
this.dgvClientView.RowHeadersVisible = false;
this.dgvClientView.RowTemplate.Height = 23;
this.dgvClientView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dgvClientView.Size = new System.Drawing.Size(394, 356);
this.dgvClientView.TabIndex = 0;
//
// Column1
//
dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
this.Column1.DefaultCellStyle = dataGridViewCellStyle2;
this.Column1.HeaderText = "序号";
this.Column1.Name = "Column1";
this.Column1.Width = 70;
//
// Column2
//
this.Column2.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
this.Column2.HeaderText = "客户端IP";
this.Column2.Name = "Column2";
//
// Column3
//
this.Column3.HeaderText = "端口号";
this.Column3.Name = "Column3";
//
// Column4
//
this.Column4.HeaderText = "状态";
this.Column4.Name = "Column4";
this.Column4.Width = 80;
//
// panel3
//
this.panel3.Controls.Add(this.lblClientCount);
this.panel3.Controls.Add(this.label4);
this.panel3.Dock = System.Windows.Forms.DockStyle.Top;
this.panel3.Location = new System.Drawing.Point(3, 17);
this.panel3.Name = "panel3";
this.panel3.Size = new System.Drawing.Size(394, 30);
this.panel3.TabIndex = 1;
//
// lblClientCount
//
this.lblClientCount.AutoSize = true;
this.lblClientCount.Location = new System.Drawing.Point(102, 9);
this.lblClientCount.Name = "lblClientCount";
this.lblClientCount.Size = new System.Drawing.Size(11, 12);
this.lblClientCount.TabIndex = 1;
this.lblClientCount.Text = "0";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(18, 9);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(77, 12);
this.label4.TabIndex = 0;
this.label4.Text = "客户端总数:";
//
// FrmServer
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1048, 689);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Name = "FrmServer";
this.Padding = new System.Windows.Forms.Padding(10);
this.Text = "Socket通讯 服务端";
this.Load += new System.EventHandler(this.FrmServer_Load);
this.panel2.ResumeLayout(false);
this.groupBox1.ResumeLayout(false);
this.panel4.ResumeLayout(false);
this.panel4.PerformLayout();
this.groupBox5.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
this.panel1.ResumeLayout(false);
this.groupBox4.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dgvClientView)).EndInit();
this.panel3.ResumeLayout(false);
this.panel3.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.CheckBox chBoxIsHeartCheck;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtServerIp;
private System.Windows.Forms.TextBox txtServerPort;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Button btnListeningStop;
private System.Windows.Forms.Button btnListeningStart;
private System.Windows.Forms.Button btnSendData;
private System.Windows.Forms.TextBox txtSendData;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.RichTextBox txtReceviceData;
private System.Windows.Forms.DataGridView dgvClientView;
private System.Windows.Forms.TextBox txtInterval;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
private System.Windows.Forms.DataGridViewTextBoxColumn Column4;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.Label lblClientCount;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Panel panel4;
private System.Windows.Forms.Label lblCurrentClient;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.GroupBox groupBox5;
private System.Windows.Forms.RichTextBox txtMessage;
}
}

View File

@@ -0,0 +1,250 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NetWorkHelper.IModels;
using NetWorkHelper.TCP;
namespace Socket.Server
{
public partial class FrmServer : Form
{
/// <summary>
/// 服务端通讯组件
/// </summary>
ITcpServer _server { get; set; }
/// <summary>
/// 客户端列表
/// </summary>
List<IClient> _clientSocketList { get; set; }
public FrmServer()
{
InitializeComponent();
}
/// <summary>
/// 窗体加载事件
/// </summary>
private void FrmServer_Load(object sender, EventArgs e)
{
_server = new ITcpServer();
_server.OnErrorMsg += _server_OnErrorMsg;
_server.OnGetLog += _server_OnGetLog;
_server.OnOfflineClient += _server_OnOfflineClient;
_server.OnOnlineClient += _server_OnOnlineClient;
_server.OnRecevice += _server_OnRecevice;
_server.OnReturnClientCount += _server_OnReturnClientCount;
_server.OnSendDateSuccess += _server_OnSendDateSuccess;
_server.OnStateInfo += _server_OnStateInfo;
_clientSocketList = new List<IClient>();
}
/// <summary>
/// 监听状态改变时返回监听状态事件
/// </summary>
private void _server_OnStateInfo(object sender, NetWorkHelper.ICommond.TcpServerStateEventArgs e)
{
txtMessage.AppendText(e.Msg + Environment.NewLine);
}
/// <summary>
/// 发送消息成功时返回成功消息事件
/// </summary>
private void _server_OnSendDateSuccess(object sender, NetWorkHelper.ICommond.TcpServerSendReturnEventArgs e)
{
}
/// <summary>
/// 用户上线下线时更新客户端在线数量事件
/// </summary>
private void _server_OnReturnClientCount(object sender, NetWorkHelper.ICommond.TcpServerReturnClientCountEventArgs e)
{
lblClientCount.Text = e.ClientCount.ToString();
}
/// <summary>
/// 接收数据事件
/// </summary>
private void _server_OnRecevice(object sender, NetWorkHelper.ICommond.TcpServerReceviceaEventArgs e)
{
string receviceData = Encoding.Default.GetString(e.Data);
txtReceviceData.AppendText(receviceData + Environment.NewLine);
}
/// <summary>
/// 新客户端上线时返回客户端事件
/// </summary>
private void _server_OnOnlineClient(object sender, NetWorkHelper.ICommond.TcpServerClientEventArgs e)
{
ChangeClientList(e.IClient, true);
}
/// <summary>
/// 改变客户端列表
/// </summary>
/// <param name="client"></param>
void ChangeClientList(IClient client, bool isAdd)
{
//新增
if (isAdd)
{
//是否存在于队列中
bool state = _clientSocketList.Exists(p => p.Ip == client.Ip);
if (!state)
{
_clientSocketList.Add(client);
DataGridViewRow dr = new DataGridViewRow();
dr.CreateCells(dgvClientView);
//设置列的值
dr.Cells[0].Value = dgvClientView.Rows.Count;
dr.Cells[1].Value = client.Ip;
dr.Cells[2].Value = client.Port;
dr.Cells[3].Value = client.ClientInfo.SitState.ToString();
dr.Tag = client.Ip;
dgvClientView.Rows.Add(dr);
}
}
else
{
var removeList = new List<DataGridViewRow>();
foreach (DataGridViewRow row in dgvClientView.Rows)
{
var rowClient = (string)row.Tag;
if (rowClient == client.Ip)
{
removeList.Add(row);
_clientSocketList.RemoveAll(p => p.Ip == rowClient);
}
}
//删除列表,避免删除导致的索引错误,另起删除循环
foreach (var row in removeList)
{
dgvClientView.Rows.Remove(row);
}
}
}
/// <summary>
/// 客户端下线时返回客户端事件
/// </summary>
private void _server_OnOfflineClient(object sender, NetWorkHelper.ICommond.TcpServerClientEventArgs e)
{
ChangeClientList(e.IClient, false);
}
/// <summary>
/// 服务端读写操作时返回日志消息
/// </summary>
private void _server_OnGetLog(object sender, NetWorkHelper.ICommond.TcpServerLogEventArgs e)
{
txtMessage.AppendText(e.LogMsg + Environment.NewLine);
}
/// <summary>
/// 错误消息
/// </summary>
private void _server_OnErrorMsg(object sender, NetWorkHelper.ICommond.TcpServerErrorEventArgs e)
{
txtMessage.AppendText(e.ErrorMsg + Environment.NewLine);
}
/// <summary>
/// 是否开启心跳检测
/// </summary>
private void chBoxIsHeartCheck_CheckedChanged(object sender, EventArgs e)
{
_server.IsHeartCheck = chBoxIsHeartCheck.Checked;
}
/// <summary>
/// 开启监听
/// </summary>
private void btnListeningStart_Click(object sender, EventArgs e)
{
if (!_server.IsStartListening)
{
if (string.IsNullOrEmpty(txtServerIp.Text) || string.IsNullOrEmpty(txtServerPort.Text) || string.IsNullOrEmpty(txtInterval.Text))
{
MessageBox.Show("参数不能为空!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
_server.CheckTime = int.Parse(txtInterval.Text.Trim());
_server.ServerIp = txtServerIp.Text.Trim();
_server.ServerPort = int.Parse(txtServerPort.Text.Trim());
_server.Start();
btnListeningStart.Enabled = false;
btnListeningStop.Enabled = true;
}
}
/// <summary>
/// 停止监听
/// </summary>
private void btnListeningStop_Click(object sender, EventArgs e)
{
if (_server.IsStartListening)
{
_server.Stop();
btnListeningStart.Enabled = true;
btnListeningStop.Enabled = false;
}
}
/// <summary>
/// 发送数据按钮
/// </summary>
private void btnSendData_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtSendData.Text.Trim()))
{
byte[] sendData = Encoding.Default.GetBytes(txtSendData.Text.Trim());
var selectedList = dgvClientView.SelectedRows;
if (selectedList.Count == 0)
{
MessageBox.Show("请选择要发送的客户端!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
foreach (DataGridViewRow row in selectedList)
{
var rowIp = (string)row.Tag;
var rowClient = _clientSocketList.Find(p => p.Ip == rowIp);
//发送数据
_server.SendData(rowClient, sendData);
}
txtSendData.Text = string.Empty;
}
}
}
}

View File

@@ -0,0 +1,132 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="Column1.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Column2.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Column3.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Column4.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
</root>

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Socket.Server
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FrmServer());
}
}
}

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("Socket.Server")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Socket.Server")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("5a88af7a-e1a2-4ee9-8e0a-f998b9d1edfb")]
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,70 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由工具生成。
// 运行时版本: 4.0.30319.42000
//
// 对此文件的更改可能导致不正确的行为,如果
// 重新生成代码,则所做更改将丢失。
// </auto-generated>
//------------------------------------------------------------------------------
namespace Socket.Server.Properties
{
/// <summary>
/// 强类型资源类,用于查找本地化字符串等。
/// </summary>
// 此类是由 StronglyTypedResourceBuilder
// 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
// 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen
// (以 /str 作为命令选项),或重新生成 VS 项目。
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources
{
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources()
{
}
/// <summary>
/// 返回此类使用的缓存 ResourceManager 实例。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager
{
get
{
if ((resourceMan == null))
{
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Socket.Server.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// 重写当前线程的 CurrentUICulture 属性,对
/// 使用此强类型资源类的所有资源查找执行重写。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture
{
get
{
return resourceCulture;
}
set
{
resourceCulture = value;
}
}
}
}

View File

@@ -0,0 +1,117 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@@ -0,0 +1,29 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Socket.Server.Properties
{
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
{
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default
{
get
{
return defaultInstance;
}
}
}
}

View File

@@ -0,0 +1,7 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
<Profiles>
<Profile Name="(Default)" />
</Profiles>
<Settings />
</SettingsFile>

View File

@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{5A88AF7A-E1A2-4EE9-8E0A-F998B9D1EDFB}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>Socket.Server</RootNamespace>
<AssemblyName>Socket.Server</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FrmServer.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="FrmServer.Designer.cs">
<DependentUpon>FrmServer.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="FrmServer.resx">
<DependentUpon>FrmServer.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NetWorkHelper\NetWorkHelper.csproj">
<Project>{FB7970FD-F699-4093-83D0-509501B7863C}</Project>
<Name>NetWorkHelper</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>