网络组件添加UDP引擎,服务器客户端实现示例代码,并在日志功能中添加查看

This commit is contained in:
dathlin
2017-06-07 16:20:40 +08:00
parent 851653a443
commit f169653120
10 changed files with 203 additions and 15 deletions

View File

@@ -43,7 +43,10 @@ namespace CommonLibrary
/// 共享文件的端口号
/// </summary>
public static int Port_Share_File { get; } = 34261;
/// <summary>
/// 用于UDP传输的端口号
/// </summary>
public static int Port_Udp_Server { get; } = 32566;
#endregion
/// <summary>

View File

@@ -50,6 +50,8 @@ namespace CommonLibrary
public static string { get; } = "B010";
public static string { get; } = "B011";
public static string { get; } = "B012";
public static string UDP日志查看 { get; } = "B013";
public static string UDP日志清空 { get; } = "B014";
/**************************************************************************************
*

Binary file not shown.

View File

@@ -1963,6 +1963,60 @@
</summary>
<param name="obj"></param>
</member>
<member name="T:HslCommunication.Enthernet.UdpNetServer">
<summary>
一个用于UDP通信的类服务器端用来接收所有客户端发送数据的功能
</summary>
</member>
<member name="P:HslCommunication.Enthernet.UdpNetServer.ReceiveCacheLength">
<summary>
获取或设置一次接收时的数据长度
</summary>
</member>
<member name="M:HslCommunication.Enthernet.UdpNetServer.ServerStart(System.Int32)">
<summary>
根据指定的端口启动Upd侦听
</summary>
<param name="port"></param>
</member>
<member name="M:HslCommunication.Enthernet.UdpNetServer.ServerClose">
<summary>
关闭引擎
</summary>
</member>
<member name="E:HslCommunication.Enthernet.UdpNetServer.AcceptString">
<summary>
当接收到文本数据的时候,触发此事件
</summary>
</member>
<member name="E:HslCommunication.Enthernet.UdpNetServer.AcceptByte">
<summary>
当接收到字节数据的时候,触发此事件
</summary>
</member>
<member name="T:HslCommunication.Enthernet.UdpNetClient">
<summary>
UDP客户端的类只负责发送数据到服务器该数据经过封装
</summary>
</member>
<member name="M:HslCommunication.Enthernet.UdpNetClient.#ctor(System.Net.IPEndPoint)">
<summary>
实例化对象,
</summary>
<param name="endpoint"></param>
</member>
<member name="M:HslCommunication.Enthernet.UdpNetClient.SendMessage(System.Byte[])">
<summary>
发送字节数据到服务器
</summary>
<param name="data">字节数据</param>
</member>
<member name="M:HslCommunication.Enthernet.UdpNetClient.SendMessage(System.String)">
<summary>
发送字符串数据到服务器
</summary>
<param name="data">字符串数据</param>
</member>
<member name="T:HslCommunication.Enthernet.FileItemShow">
<summary>
文件显示的子控件类

View File

@@ -48,6 +48,8 @@ namespace 软件系统客户端模版
this.userButton9 = new HslCommunication.Controls.UserButton();
this.userButton10 = new HslCommunication.Controls.UserButton();
this.userButton11 = new HslCommunication.Controls.UserButton();
this.userButton12 = new HslCommunication.Controls.UserButton();
this.userButton13 = new HslCommunication.Controls.UserButton();
this.SuspendLayout();
//
// textBox1
@@ -59,7 +61,7 @@ namespace 软件系统客户端模版
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(643, 498);
this.textBox1.Size = new System.Drawing.Size(608, 498);
this.textBox1.TabIndex = 0;
//
// userButton_login
@@ -69,7 +71,7 @@ namespace 软件系统客户端模版
this.userButton_login.CustomerInformation = "";
this.userButton_login.EnableColor = System.Drawing.Color.FromArgb(((int)(((byte)(190)))), ((int)(((byte)(190)))), ((int)(((byte)(190)))));
this.userButton_login.Font = new System.Drawing.Font("微软雅黑", 9F);
this.userButton_login.Location = new System.Drawing.Point(666, 11);
this.userButton_login.Location = new System.Drawing.Point(626, 13);
this.userButton_login.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.userButton_login.Name = "userButton_login";
this.userButton_login.Size = new System.Drawing.Size(103, 26);
@@ -84,7 +86,7 @@ namespace 软件系统客户端模版
this.userButton1.CustomerInformation = "";
this.userButton1.EnableColor = System.Drawing.Color.FromArgb(((int)(((byte)(190)))), ((int)(((byte)(190)))), ((int)(((byte)(190)))));
this.userButton1.Font = new System.Drawing.Font("微软雅黑", 9F);
this.userButton1.Location = new System.Drawing.Point(666, 45);
this.userButton1.Location = new System.Drawing.Point(736, 13);
this.userButton1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.userButton1.Name = "userButton1";
this.userButton1.Size = new System.Drawing.Size(103, 26);
@@ -99,7 +101,7 @@ namespace 软件系统客户端模版
this.userButton2.CustomerInformation = "";
this.userButton2.EnableColor = System.Drawing.Color.FromArgb(((int)(((byte)(190)))), ((int)(((byte)(190)))), ((int)(((byte)(190)))));
this.userButton2.Font = new System.Drawing.Font("微软雅黑", 9F);
this.userButton2.Location = new System.Drawing.Point(666, 96);
this.userButton2.Location = new System.Drawing.Point(626, 62);
this.userButton2.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.userButton2.Name = "userButton2";
this.userButton2.Size = new System.Drawing.Size(103, 26);
@@ -114,7 +116,7 @@ namespace 软件系统客户端模版
this.userButton3.CustomerInformation = "";
this.userButton3.EnableColor = System.Drawing.Color.FromArgb(((int)(((byte)(190)))), ((int)(((byte)(190)))), ((int)(((byte)(190)))));
this.userButton3.Font = new System.Drawing.Font("微软雅黑", 9F);
this.userButton3.Location = new System.Drawing.Point(666, 130);
this.userButton3.Location = new System.Drawing.Point(736, 62);
this.userButton3.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.userButton3.Name = "userButton3";
this.userButton3.Size = new System.Drawing.Size(103, 26);
@@ -129,7 +131,7 @@ namespace 软件系统客户端模版
this.userButton4.CustomerInformation = "";
this.userButton4.EnableColor = System.Drawing.Color.FromArgb(((int)(((byte)(190)))), ((int)(((byte)(190)))), ((int)(((byte)(190)))));
this.userButton4.Font = new System.Drawing.Font("微软雅黑", 9F);
this.userButton4.Location = new System.Drawing.Point(666, 181);
this.userButton4.Location = new System.Drawing.Point(626, 111);
this.userButton4.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.userButton4.Name = "userButton4";
this.userButton4.Size = new System.Drawing.Size(103, 26);
@@ -144,7 +146,7 @@ namespace 软件系统客户端模版
this.userButton5.CustomerInformation = "";
this.userButton5.EnableColor = System.Drawing.Color.FromArgb(((int)(((byte)(190)))), ((int)(((byte)(190)))), ((int)(((byte)(190)))));
this.userButton5.Font = new System.Drawing.Font("微软雅黑", 9F);
this.userButton5.Location = new System.Drawing.Point(666, 215);
this.userButton5.Location = new System.Drawing.Point(736, 111);
this.userButton5.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.userButton5.Name = "userButton5";
this.userButton5.Size = new System.Drawing.Size(103, 26);
@@ -159,7 +161,7 @@ namespace 软件系统客户端模版
this.userButton6.CustomerInformation = "";
this.userButton6.EnableColor = System.Drawing.Color.FromArgb(((int)(((byte)(190)))), ((int)(((byte)(190)))), ((int)(((byte)(190)))));
this.userButton6.Font = new System.Drawing.Font("微软雅黑", 9F);
this.userButton6.Location = new System.Drawing.Point(666, 485);
this.userButton6.Location = new System.Drawing.Point(736, 517);
this.userButton6.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.userButton6.Name = "userButton6";
this.userButton6.Size = new System.Drawing.Size(103, 26);
@@ -174,7 +176,7 @@ namespace 软件系统客户端模版
this.userButton7.CustomerInformation = "";
this.userButton7.EnableColor = System.Drawing.Color.FromArgb(((int)(((byte)(190)))), ((int)(((byte)(190)))), ((int)(((byte)(190)))));
this.userButton7.Font = new System.Drawing.Font("微软雅黑", 9F);
this.userButton7.Location = new System.Drawing.Point(666, 451);
this.userButton7.Location = new System.Drawing.Point(626, 517);
this.userButton7.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.userButton7.Name = "userButton7";
this.userButton7.Size = new System.Drawing.Size(103, 26);
@@ -264,7 +266,7 @@ namespace 软件系统客户端模版
this.userButton8.CustomerInformation = "";
this.userButton8.EnableColor = System.Drawing.Color.FromArgb(((int)(((byte)(190)))), ((int)(((byte)(190)))), ((int)(((byte)(190)))));
this.userButton8.Font = new System.Drawing.Font("微软雅黑", 9F);
this.userButton8.Location = new System.Drawing.Point(666, 301);
this.userButton8.Location = new System.Drawing.Point(736, 159);
this.userButton8.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.userButton8.Name = "userButton8";
this.userButton8.Size = new System.Drawing.Size(103, 26);
@@ -279,7 +281,7 @@ namespace 软件系统客户端模版
this.userButton9.CustomerInformation = "";
this.userButton9.EnableColor = System.Drawing.Color.FromArgb(((int)(((byte)(190)))), ((int)(((byte)(190)))), ((int)(((byte)(190)))));
this.userButton9.Font = new System.Drawing.Font("微软雅黑", 9F);
this.userButton9.Location = new System.Drawing.Point(666, 267);
this.userButton9.Location = new System.Drawing.Point(626, 159);
this.userButton9.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.userButton9.Name = "userButton9";
this.userButton9.Size = new System.Drawing.Size(103, 26);
@@ -294,7 +296,7 @@ namespace 软件系统客户端模版
this.userButton10.CustomerInformation = "";
this.userButton10.EnableColor = System.Drawing.Color.FromArgb(((int)(((byte)(190)))), ((int)(((byte)(190)))), ((int)(((byte)(190)))));
this.userButton10.Font = new System.Drawing.Font("微软雅黑", 9F);
this.userButton10.Location = new System.Drawing.Point(666, 385);
this.userButton10.Location = new System.Drawing.Point(734, 208);
this.userButton10.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.userButton10.Name = "userButton10";
this.userButton10.Size = new System.Drawing.Size(103, 26);
@@ -309,7 +311,7 @@ namespace 软件系统客户端模版
this.userButton11.CustomerInformation = "";
this.userButton11.EnableColor = System.Drawing.Color.FromArgb(((int)(((byte)(190)))), ((int)(((byte)(190)))), ((int)(((byte)(190)))));
this.userButton11.Font = new System.Drawing.Font("微软雅黑", 9F);
this.userButton11.Location = new System.Drawing.Point(666, 351);
this.userButton11.Location = new System.Drawing.Point(626, 208);
this.userButton11.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.userButton11.Name = "userButton11";
this.userButton11.Size = new System.Drawing.Size(103, 26);
@@ -317,11 +319,43 @@ namespace 软件系统客户端模版
this.userButton11.UIText = "建议反馈查看";
this.userButton11.Click += new System.EventHandler(this.userButton11_Click);
//
// userButton12
//
this.userButton12.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.userButton12.BackColor = System.Drawing.Color.Transparent;
this.userButton12.CustomerInformation = "";
this.userButton12.EnableColor = System.Drawing.Color.FromArgb(((int)(((byte)(190)))), ((int)(((byte)(190)))), ((int)(((byte)(190)))));
this.userButton12.Font = new System.Drawing.Font("微软雅黑", 9F);
this.userButton12.Location = new System.Drawing.Point(734, 255);
this.userButton12.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.userButton12.Name = "userButton12";
this.userButton12.Size = new System.Drawing.Size(103, 26);
this.userButton12.TabIndex = 24;
this.userButton12.UIText = "Udp日志清空";
this.userButton12.Click += new System.EventHandler(this.userButton12_Click);
//
// userButton13
//
this.userButton13.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.userButton13.BackColor = System.Drawing.Color.Transparent;
this.userButton13.CustomerInformation = "";
this.userButton13.EnableColor = System.Drawing.Color.FromArgb(((int)(((byte)(190)))), ((int)(((byte)(190)))), ((int)(((byte)(190)))));
this.userButton13.Font = new System.Drawing.Font("微软雅黑", 9F);
this.userButton13.Location = new System.Drawing.Point(626, 255);
this.userButton13.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.userButton13.Name = "userButton13";
this.userButton13.Size = new System.Drawing.Size(103, 26);
this.userButton13.TabIndex = 23;
this.userButton13.UIText = "Udp日志查看";
this.userButton13.Click += new System.EventHandler(this.userButton13_Click);
//
// FormLog
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(779, 547);
this.ClientSize = new System.Drawing.Size(849, 547);
this.Controls.Add(this.userButton12);
this.Controls.Add(this.userButton13);
this.Controls.Add(this.userButton10);
this.Controls.Add(this.userButton11);
this.Controls.Add(this.userButton8);
@@ -371,5 +405,7 @@ namespace 软件系统客户端模版
private UserButton userButton9;
private UserButton userButton10;
private UserButton userButton11;
private UserButton userButton12;
private UserButton userButton13;
}
}

View File

@@ -105,7 +105,15 @@ namespace 软件系统客户端模版
{
ClearFromServer(CommonHeadCode.SimplifyHeadCode.);
}
private void userButton13_Click(object sender, EventArgs e)
{
ReadFromServer(CommonHeadCode.SimplifyHeadCode.UDP日志查看);
}
private void userButton12_Click(object sender, EventArgs e)
{
ClearFromServer(CommonHeadCode.SimplifyHeadCode.UDP日志清空);
}
/// <summary>

View File

@@ -55,6 +55,9 @@ namespace 软件系统客户端模版
private void FormMainWindow_Load(object sender, EventArgs e)
{
//udp测试
//SendServerUdpData("载入了窗体");
//窗口载入
label_userName.Text = UserClient.UserAccount.UserName;
label_grade.Text = AccountGrade.GetDescription(UserClient.UserAccount.Grade);
@@ -90,6 +93,8 @@ namespace 软件系统客户端模版
//窗口显示
IsWindowShow = true;
//udp测试
//SendServerUdpData("显示了窗体");
//是否显示更新日志,显示前进行判断该版本是否已经显示过了
if (UserClient.JsonSettings.IsNewVersionRunning)
@@ -481,6 +486,17 @@ namespace 软件系统客户端模版
#endregion
#region Udp发送示例
/// <summary>
/// 调用该方法并指定参数即可,最长字符串不得
/// </summary>
/// <param name="data"></param>
private void SendServerUdpData(string data)
{
//测试发送udp消息
UserClient.Net_Udp_Client.SendMessage(data);
}
#endregion
}
}

View File

@@ -70,5 +70,11 @@ namespace 软件系统客户端模版
{
KeyToken = CommonHeadCode.KeyToken,
};
/// <summary>
/// 用于使用udp向服务器进行发送即时可丢失数据的对象
/// </summary>
public static UdpNetClient Net_Udp_Client = new UdpNetClient(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(ServerIp),
CommonLibrary.CommonLibrary.Port_Udp_Server));
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 128 KiB

After

Width:  |  Height:  |  Size: 126 KiB

View File

@@ -132,6 +132,7 @@ namespace 软件系统服务端模版
Net_SoftUpdate_Server_Initialization();//软件更新引擎初始化
Net_File_Update_Initialization();//软件异地更新引擎初始化
Simple_File_Initiaization();//共享文件引擎初始化
Net_Udp_Server_Initialization();//UDP引擎服务初始化
ToolStripMenuItem.Text = "已启动";
ToolStripMenuItem.BackColor = Color.LimeGreen;
IsSystemStart = true;
@@ -508,6 +509,17 @@ namespace 软件系统服务端模版
net_simplify_server.SendMessage(object1, "成功");
RuntimeLogHelper.SaveWarnning("建议反馈日志清空");
}
else if (headCode == CommonHeadCode.SimplifyHeadCode.UDP日志查看)
{
net_simplify_server.SendMessage(object1, net_udp_server.LogHelper.GetLogText());
RuntimeLogHelper.SaveInformation("UDP日志查看");
}
else if (headCode == CommonHeadCode.SimplifyHeadCode.UDP日志清空)
{
net_udp_server.LogHelper.ClearLogText();
net_simplify_server.SendMessage(object1, "成功");
RuntimeLogHelper.SaveWarnning("UDP日志清空");
}
else
{
net_simplify_server.SendMessage(object1, object2);
@@ -771,6 +783,57 @@ namespace 软件系统服务端模版
#endregion
#region Udp网络通信块
/*********************************************************************************************************
*
* 说明 一个用于网络间通信的UDP服务引擎客户端调用UserClient.Net_Udp_Client.SendMessage(data);发送
* 详细请参考客户端FormMainWindow中的udp发送说明
* 特点 该Udp引擎非常健壮接收失败了会抛弃本次接收自动进入下一轮接收。
* 安全 本引擎含有数据长度校验机制,确保服务器接收到的数据是正确的,没有丢失的
* 注意 如果服务器配置了ReceiveCacheLength = 1024那么客户端发送的字符串数据长度不能超过1000否则服务器会自动丢弃可在日志中查看
* 警告 如果想要你自己的软件支持向本引擎访问,必须使用该网络组件实现,参考客户端定义,否则发送失败
*
**********************************************************************************************************/
/// <summary>
/// 服务器的UDP核心引擎
/// </summary>
private UdpNetServer net_udp_server { get; set; }
private void Net_Udp_Server_Initialization()
{
try
{
net_udp_server = new UdpNetServer();
net_udp_server.LogHelper.LogSaveFileName = Application.StartupPath + @"\udp_log.txt";//日志路径
net_udp_server.ReceiveCacheLength = 1024;//单次接收数据的缓冲长度
net_udp_server.AcceptByte += Net_udp_server_AcceptByte;//接收到字节数据的时候触发事件
net_udp_server.AcceptString += Net_udp_server_AcceptString;//接收到字符串数据的时候触发事件
net_udp_server.ServerStart(CommonLibrary.CommonLibrary.Port_Udp_Server);
}
catch(Exception ex)
{
SoftBasic.ShowExceptionMessage(ex);
}
}
private void Net_udp_server_AcceptString(AsyncStateOne object1, string object2)
{
//此处为测试
Invoke(new Action(() =>
{
textBox1.AppendText($"{DateTime.Now.ToString("MM-dd HH:mm:ss ")}来自IP:{object1._IpEnd_Point.Address.ToString()} 内容:{object2}{Environment.NewLine}");
}));
}
private void Net_udp_server_AcceptByte(AsyncStateOne object1, byte[] object2)
{
//具体用法参考上面字符串方法
}
#endregion
#region 访PLC块示例代码