fix logic, ui

main
syneffort 2 years ago
parent 5c95777400
commit 5c46e11c52
  1. BIN
      Rosource/HD_Sandwich_Favorites.ico
  2. BIN
      Rosource/HD_Sandwich_Removable.ico
  3. 176
      SocketStudy/ClientForm/MainForm.Designer.cs
  4. 65
      SocketStudy/ClientForm/MainForm.cs
  5. 3371
      SocketStudy/ClientForm/MainForm.resx
  6. 18
      SocketStudy/PComm/PClient.cs
  7. 23
      SocketStudy/PUtil/PUtil.cs
  8. 3405
      SocketStudy/ServerForm/MainForm.resx
  9. 3
      SocketStudy/ServerForm/mainForm.Designer.cs
  10. 7
      SocketStudy/ServerForm/mainForm.cs

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 199 KiB

@ -29,18 +29,25 @@ namespace ClientForm
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.logConsole = new System.Windows.Forms.TextBox(); this.logConsole = new System.Windows.Forms.TextBox();
this.mainTablePanel = new System.Windows.Forms.TableLayoutPanel();
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.panel1 = new System.Windows.Forms.Panel();
this.stringButton = new System.Windows.Forms.Button(); this.stringButton = new System.Windows.Forms.Button();
this.fileButton = new System.Windows.Forms.Button();
this.objectButton = new System.Windows.Forms.Button(); this.objectButton = new System.Windows.Forms.Button();
this.imageButton = new System.Windows.Forms.Button(); this.imageButton = new System.Windows.Forms.Button();
this.fileButton = new System.Windows.Forms.Button(); this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
this.mainTablePanel = new System.Windows.Forms.TableLayoutPanel(); this.startButton = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.statusStrip = new System.Windows.Forms.StatusStrip(); this.statusStrip = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
this.heartBeatStatus = new System.Windows.Forms.ToolStripStatusLabel(); this.heartBeatStatus = new System.Windows.Forms.ToolStripStatusLabel();
this.addressTextBox = new System.Windows.Forms.TextBox();
this.mainTablePanel.SuspendLayout(); this.mainTablePanel.SuspendLayout();
this.tableLayoutPanel1.SuspendLayout();
this.panel1.SuspendLayout(); this.panel1.SuspendLayout();
this.tableLayoutPanel2.SuspendLayout();
this.statusStrip.SuspendLayout(); this.statusStrip.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
@ -49,17 +56,61 @@ namespace ClientForm
this.logConsole.BackColor = System.Drawing.Color.Black; this.logConsole.BackColor = System.Drawing.Color.Black;
this.logConsole.Dock = System.Windows.Forms.DockStyle.Fill; this.logConsole.Dock = System.Windows.Forms.DockStyle.Fill;
this.logConsole.ForeColor = System.Drawing.Color.SpringGreen; this.logConsole.ForeColor = System.Drawing.Color.SpringGreen;
this.logConsole.Location = new System.Drawing.Point(153, 4); this.logConsole.Location = new System.Drawing.Point(3, 64);
this.logConsole.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.logConsole.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.logConsole.Multiline = true; this.logConsole.Multiline = true;
this.logConsole.Name = "logConsole"; this.logConsole.Name = "logConsole";
this.logConsole.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.logConsole.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.logConsole.Size = new System.Drawing.Size(572, 354); this.logConsole.Size = new System.Drawing.Size(578, 393);
this.logConsole.TabIndex = 1; this.logConsole.TabIndex = 1;
// //
// mainTablePanel
//
this.mainTablePanel.ColumnCount = 1;
this.mainTablePanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.mainTablePanel.Controls.Add(this.logConsole, 0, 1);
this.mainTablePanel.Controls.Add(this.tableLayoutPanel1, 0, 0);
this.mainTablePanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.mainTablePanel.Location = new System.Drawing.Point(0, 0);
this.mainTablePanel.Name = "mainTablePanel";
this.mainTablePanel.RowCount = 2;
this.mainTablePanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 60F));
this.mainTablePanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.mainTablePanel.Size = new System.Drawing.Size(584, 461);
this.mainTablePanel.TabIndex = 3;
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnCount = 2;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 200F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.tableLayoutPanel1.Controls.Add(this.panel1, 1, 0);
this.tableLayoutPanel1.Controls.Add(this.tableLayoutPanel2, 0, 0);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 1;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(584, 60);
this.tableLayoutPanel1.TabIndex = 2;
//
// panel1
//
this.panel1.Controls.Add(this.stringButton);
this.panel1.Controls.Add(this.fileButton);
this.panel1.Controls.Add(this.objectButton);
this.panel1.Controls.Add(this.imageButton);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(203, 3);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(378, 54);
this.panel1.TabIndex = 4;
//
// stringButton // stringButton
// //
this.stringButton.Location = new System.Drawing.Point(32, 22); this.stringButton.Location = new System.Drawing.Point(29, 15);
this.stringButton.Name = "stringButton"; this.stringButton.Name = "stringButton";
this.stringButton.Size = new System.Drawing.Size(75, 23); this.stringButton.Size = new System.Drawing.Size(75, 23);
this.stringButton.TabIndex = 2; this.stringButton.TabIndex = 2;
@ -67,9 +118,19 @@ namespace ClientForm
this.stringButton.UseVisualStyleBackColor = true; this.stringButton.UseVisualStyleBackColor = true;
this.stringButton.Click += new System.EventHandler(this.stringButton_Click); this.stringButton.Click += new System.EventHandler(this.stringButton_Click);
// //
// fileButton
//
this.fileButton.Location = new System.Drawing.Point(272, 15);
this.fileButton.Name = "fileButton";
this.fileButton.Size = new System.Drawing.Size(75, 23);
this.fileButton.TabIndex = 2;
this.fileButton.Text = "file";
this.fileButton.UseVisualStyleBackColor = true;
this.fileButton.Click += new System.EventHandler(this.fileButton_Click);
//
// objectButton // objectButton
// //
this.objectButton.Location = new System.Drawing.Point(32, 69); this.objectButton.Location = new System.Drawing.Point(110, 15);
this.objectButton.Name = "objectButton"; this.objectButton.Name = "objectButton";
this.objectButton.Size = new System.Drawing.Size(75, 23); this.objectButton.Size = new System.Drawing.Size(75, 23);
this.objectButton.TabIndex = 2; this.objectButton.TabIndex = 2;
@ -79,7 +140,7 @@ namespace ClientForm
// //
// imageButton // imageButton
// //
this.imageButton.Location = new System.Drawing.Point(32, 111); this.imageButton.Location = new System.Drawing.Point(191, 15);
this.imageButton.Name = "imageButton"; this.imageButton.Name = "imageButton";
this.imageButton.Size = new System.Drawing.Size(75, 23); this.imageButton.Size = new System.Drawing.Size(75, 23);
this.imageButton.TabIndex = 2; this.imageButton.TabIndex = 2;
@ -87,42 +148,31 @@ namespace ClientForm
this.imageButton.UseVisualStyleBackColor = true; this.imageButton.UseVisualStyleBackColor = true;
this.imageButton.Click += new System.EventHandler(this.imageButton_Click); this.imageButton.Click += new System.EventHandler(this.imageButton_Click);
// //
// fileButton // tableLayoutPanel2
// //
this.fileButton.Location = new System.Drawing.Point(32, 152); this.tableLayoutPanel2.ColumnCount = 1;
this.fileButton.Name = "fileButton"; this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.fileButton.Size = new System.Drawing.Size(75, 23); this.tableLayoutPanel2.Controls.Add(this.startButton, 0, 1);
this.fileButton.TabIndex = 2; this.tableLayoutPanel2.Controls.Add(this.addressTextBox, 0, 0);
this.fileButton.Text = "file"; this.tableLayoutPanel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.fileButton.UseVisualStyleBackColor = true; this.tableLayoutPanel2.Location = new System.Drawing.Point(3, 3);
this.fileButton.Click += new System.EventHandler(this.fileButton_Click); this.tableLayoutPanel2.Name = "tableLayoutPanel2";
// this.tableLayoutPanel2.RowCount = 2;
// mainTablePanel this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
// this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.mainTablePanel.ColumnCount = 2; this.tableLayoutPanel2.Size = new System.Drawing.Size(194, 54);
this.mainTablePanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 150F)); this.tableLayoutPanel2.TabIndex = 5;
this.mainTablePanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); //
this.mainTablePanel.Controls.Add(this.logConsole, 1, 0); // startButton
this.mainTablePanel.Controls.Add(this.panel1, 0, 0); //
this.mainTablePanel.Dock = System.Windows.Forms.DockStyle.Fill; this.startButton.Dock = System.Windows.Forms.DockStyle.Fill;
this.mainTablePanel.Location = new System.Drawing.Point(0, 0); this.startButton.Location = new System.Drawing.Point(3, 30);
this.mainTablePanel.Name = "mainTablePanel"; this.startButton.Name = "startButton";
this.mainTablePanel.RowCount = 1; this.startButton.Size = new System.Drawing.Size(188, 21);
this.mainTablePanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); this.startButton.TabIndex = 0;
this.mainTablePanel.Size = new System.Drawing.Size(728, 362); this.startButton.Text = "Connect";
this.mainTablePanel.TabIndex = 3; this.startButton.UseVisualStyleBackColor = true;
// this.startButton.Click += new System.EventHandler(this.startButton_Click);
// panel1
//
this.panel1.Controls.Add(this.stringButton);
this.panel1.Controls.Add(this.fileButton);
this.panel1.Controls.Add(this.objectButton);
this.panel1.Controls.Add(this.imageButton);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(3, 3);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(144, 356);
this.panel1.TabIndex = 2;
// //
// statusStrip // statusStrip
// //
@ -130,35 +180,47 @@ namespace ClientForm
this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabel1, this.toolStripStatusLabel1,
this.heartBeatStatus}); this.heartBeatStatus});
this.statusStrip.Location = new System.Drawing.Point(0, 344); this.statusStrip.Location = new System.Drawing.Point(0, 439);
this.statusStrip.Name = "statusStrip"; this.statusStrip.Name = "statusStrip";
this.statusStrip.Size = new System.Drawing.Size(728, 18); this.statusStrip.Size = new System.Drawing.Size(584, 22);
this.statusStrip.TabIndex = 4; this.statusStrip.TabIndex = 4;
this.statusStrip.Text = "statusStrip1"; this.statusStrip.Text = "statusStrip1";
// //
// toolStripStatusLabel1 // toolStripStatusLabel1
// //
this.toolStripStatusLabel1.Name = "toolStripStatusLabel1"; this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
this.toolStripStatusLabel1.Size = new System.Drawing.Size(0, 13); this.toolStripStatusLabel1.Size = new System.Drawing.Size(0, 17);
// //
// heartBeatStatus // heartBeatStatus
// //
this.heartBeatStatus.Name = "heartBeatStatus"; this.heartBeatStatus.Name = "heartBeatStatus";
this.heartBeatStatus.Size = new System.Drawing.Size(0, 13); this.heartBeatStatus.Size = new System.Drawing.Size(0, 17);
//
// addressTextBox
//
this.addressTextBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.addressTextBox.Location = new System.Drawing.Point(3, 3);
this.addressTextBox.Name = "addressTextBox";
this.addressTextBox.Size = new System.Drawing.Size(188, 21);
this.addressTextBox.TabIndex = 1;
this.addressTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
// //
// MainForm // MainForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(910, 452); this.ClientSize = new System.Drawing.Size(584, 461);
this.Controls.Add(this.statusStrip); this.Controls.Add(this.statusStrip);
this.Controls.Add(this.mainTablePanel); this.Controls.Add(this.mainTablePanel);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "MainForm"; this.Name = "MainForm";
this.Text = "Client"; this.Text = "Client";
this.mainTablePanel.ResumeLayout(false); this.mainTablePanel.ResumeLayout(false);
this.mainTablePanel.PerformLayout(); this.mainTablePanel.PerformLayout();
this.tableLayoutPanel1.ResumeLayout(false);
this.panel1.ResumeLayout(false); this.panel1.ResumeLayout(false);
this.tableLayoutPanel2.ResumeLayout(false);
this.tableLayoutPanel2.PerformLayout();
this.statusStrip.ResumeLayout(false); this.statusStrip.ResumeLayout(false);
this.statusStrip.PerformLayout(); this.statusStrip.PerformLayout();
this.ResumeLayout(false); this.ResumeLayout(false);
@ -169,15 +231,19 @@ namespace ClientForm
#endregion #endregion
private System.Windows.Forms.TextBox logConsole; private System.Windows.Forms.TextBox logConsole;
private System.Windows.Forms.Button stringButton;
private System.Windows.Forms.Button objectButton;
private System.Windows.Forms.Button imageButton;
private System.Windows.Forms.Button fileButton;
private System.Windows.Forms.TableLayoutPanel mainTablePanel; private System.Windows.Forms.TableLayoutPanel mainTablePanel;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.StatusStrip statusStrip; private System.Windows.Forms.StatusStrip statusStrip;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1; private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
private System.Windows.Forms.ToolStripStatusLabel heartBeatStatus; private System.Windows.Forms.ToolStripStatusLabel heartBeatStatus;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button stringButton;
private System.Windows.Forms.Button fileButton;
private System.Windows.Forms.Button objectButton;
private System.Windows.Forms.Button imageButton;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2;
private System.Windows.Forms.Button startButton;
private System.Windows.Forms.TextBox addressTextBox;
} }
} }

@ -8,6 +8,7 @@ using System.Data;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Net;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -20,7 +21,8 @@ namespace ClientForm
public partial class MainForm : Form public partial class MainForm : Form
{ {
private readonly string SERVER_IP = "124.56.13.173";//"10.233.96.172", "124.56.13.173"; private readonly int PORT = 37777;
private readonly int HB_PORT = 37778;
private PClient heartBeatClient; private PClient heartBeatClient;
private PClient client; private PClient client;
@ -34,31 +36,38 @@ namespace ClientForm
public MainForm() public MainForm()
{ {
InitializeComponent(); InitializeComponent();
InitInstance();
} }
private void InitInstance() private void ConnectToServer(string address)
{ {
IPAddress ipAddress = PUtil.ValidateAddress(address);
if (ipAddress == null)
return;
string clientId = PUtil.GetRandomString(5); string clientId = PUtil.GetRandomString(5);
string heartBeatClientId = clientId + "_hb"; string heartBeatClientId = clientId + "_hb";
logHandler = new LogHandler(WriteLog); logHandler = new LogHandler(WriteLog);
formHandler = new FormHandler(ShowForm); formHandler = new FormHandler(ShowForm);
heartBeatClient = new PClient(SERVER_IP, 37778, heartBeatClientId); heartBeatClient = new PClient(ipAddress, HB_PORT, heartBeatClientId);
heartBeatClient.OnConnected += CommonClient_OnConnected; heartBeatClient.OnConnected += CommonClient_OnConnected;
heartBeatClient.OnSend += HeartBeatClient_OnSend; heartBeatClient.OnSend += HeartBeatClient_OnSend;
heartBeatClient.OnReceived += HeartBeatClient_OnReceived; heartBeatClient.OnReceived += HeartBeatClient_OnReceived;
heartBeatClient.OnErrorCatched += CommonClient_OnErrorCatched; heartBeatClient.OnErrorCatched += CommonClient_OnErrorCatched;
heartBeatClient.OnDisconnected += CommonClient_OnDisconnected; heartBeatClient.OnDisconnected += CommonClient_OnDisconnected;
client = new PClient(SERVER_IP, 37777, clientId); client = new PClient(ipAddress, PORT, clientId);
client.OnConnected += CommonClient_OnConnected; client.OnConnected += CommonClient_OnConnected;
client.OnSend += Client_OnSend; client.OnSend += Client_OnSend;
client.OnReceived += Client_OnReceived; client.OnReceived += Client_OnReceived;
client.OnErrorCatched += CommonClient_OnErrorCatched; client.OnErrorCatched += CommonClient_OnErrorCatched;
client.OnDisconnected += CommonClient_OnDisconnected; client.OnDisconnected += CommonClient_OnDisconnected;
heartBeatClient.Connect();
UpdateHeartBeat();
client.Connect();
heartBeatTimer = new System.Timers.Timer(); heartBeatTimer = new System.Timers.Timer();
heartBeatTimer.Interval = 1000 * 10; heartBeatTimer.Interval = 1000 * 10;
heartBeatTimer.Elapsed += HeartBeatTimer_Elapsed; heartBeatTimer.Elapsed += HeartBeatTimer_Elapsed;
@ -70,22 +79,33 @@ namespace ClientForm
GarbageTimer.Start(); GarbageTimer.Start();
} }
private void DisconnectFromServer()
{
heartBeatClient.Close();
heartBeatClient.Dispose();
client.Close();
client.Dispose();
heartBeatTimer.Stop();
GarbageTimer.Stop();
}
private void Client_OnSend(PClient sender, PDataType dataType, byte[] data) private void Client_OnSend(PClient sender, PDataType dataType, byte[] data)
{ {
string log = $"Send data: target: {sender.EndPoint.ToString()}, type: {dataType.ToString()}, size: {data.Length.ToString("#,###")}"; string log = $"Send data: target: {sender.EndPoint.ToString()}, type: {dataType.ToString()}, size: {data.Length.ToString("#,###")}bytes";
logHandler(client, log); logHandler(sender, log);
} }
private void CommonClient_OnDisconnected(PClient sender) private void CommonClient_OnDisconnected(PClient sender)
{ {
string log = $"Disconnected: {sender.EndPoint.ToString()}"; string log = $"Disconnected: {sender.EndPoint.ToString()}";
logHandler(client, log); logHandler(sender, log);
} }
private void CommonClient_OnErrorCatched(PClient sender, string msg) private void CommonClient_OnErrorCatched(PClient sender, string msg)
{ {
string log = $"Error: {msg}"; string log = $"Error: {msg}";
logHandler(client, log); logHandler(sender, log);
} }
private void HeartBeatClient_OnSend(PClient sender, PDataType dataType, byte[] data) private void HeartBeatClient_OnSend(PClient sender, PDataType dataType, byte[] data)
@ -96,7 +116,7 @@ namespace ClientForm
private void CommonClient_OnConnected(PClient sender) private void CommonClient_OnConnected(PClient sender)
{ {
string log = $"Connected: {sender.EndPoint.ToString()}"; string log = $"Connected: {sender.EndPoint.ToString()}";
logHandler(client, log); logHandler(sender, log);
} }
private void ShowForm(Form form) private void ShowForm(Form form)
@ -225,5 +245,30 @@ namespace ClientForm
GC.Collect(); GC.Collect();
} }
private void startButton_Click(object sender, EventArgs e)
{
if (startButton.Text == "Connect")
{
startButton.Text = "Disconnect";
string address = addressTextBox.Text;
if (string.IsNullOrEmpty(address))
{
MessageBox.Show("Empty address");
return;
}
ConnectToServer(address);
addressTextBox.Enabled = false;
}
else
{
startButton.Text = "Connect";
DisconnectFromServer();
addressTextBox.Enabled = true;
}
}
} }
} }

File diff suppressed because it is too large Load Diff

@ -32,6 +32,8 @@ namespace PComm
public delegate void ClientDisconnectedHandler(PClient sender); public delegate void ClientDisconnectedHandler(PClient sender);
public event ClientDisconnectedHandler OnDisconnected; public event ClientDisconnectedHandler OnDisconnected;
private int ConnectionRetry = 0;
public string ID { get; set; } public string ID { get; set; }
public IPEndPoint EndPoint { get; private set; } public IPEndPoint EndPoint { get; private set; }
@ -42,18 +44,13 @@ namespace PComm
private Socket socket; private Socket socket;
public PClient(string ip, int port, string id = null) public PClient(IPAddress ipAddress, int port, string id = null)
{ {
if (string.IsNullOrEmpty(id)) if (string.IsNullOrEmpty(id))
id = Guid.NewGuid().ToString(); id = Guid.NewGuid().ToString();
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.ReceiveBufferSize = BUFF_SIZE;
socket.SendBufferSize = BUFF_SIZE;
this.ID = id; this.ID = id;
this.EndPoint = new IPEndPoint(IPAddress.Parse(ip), port); this.EndPoint = new IPEndPoint(ipAddress, port);
Connect();
} }
public PClient(Socket accepted) public PClient(Socket accepted)
@ -88,6 +85,8 @@ namespace PComm
this.Send(PDataType.ClientID, Encoding.UTF8.GetBytes(this.ID)); this.Send(PDataType.ClientID, Encoding.UTF8.GetBytes(this.ID));
socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null); socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null);
ConnectionRetry = 0;
} }
catch(Exception ex) catch(Exception ex)
{ {
@ -97,6 +96,9 @@ namespace PComm
if (OnErrorCatched != null) if (OnErrorCatched != null)
OnErrorCatched(this, msg); OnErrorCatched(this, msg);
if (++ConnectionRetry >= 5)
return;
Connect(); Connect();
} }
} }
@ -253,7 +255,7 @@ namespace PComm
} }
else else
{ {
msg = $"Socket close ({socket.RemoteEndPoint.ToString()})"; msg = $"Socket close ({this.EndPoint.ToString()})";
Debug.WriteLine(msg); Debug.WriteLine(msg);
PFileManager.Instance.WriteLog(msg); PFileManager.Instance.WriteLog(msg);
if (OnErrorCatched != null) if (OnErrorCatched != null)

@ -9,6 +9,7 @@ using System.Drawing.Imaging;
using System.Windows.Forms; using System.Windows.Forms;
using System.IO.Compression; using System.IO.Compression;
using System.Diagnostics; using System.Diagnostics;
using System.Net;
namespace PUtility namespace PUtility
{ {
@ -163,5 +164,27 @@ namespace PUtility
return resultFileName; return resultFileName;
} }
public static IPAddress ValidateAddress(string address)
{
try
{
IPAddress ip;
if (!string.IsNullOrEmpty(address) && IPAddress.TryParse(address, out ip))
return ip;
// find ip through dns
IPHostEntry hostEntry = Dns.GetHostEntry(address);
if (hostEntry.AddressList == null || hostEntry.AddressList.Length < 1)
return null;
return hostEntry.AddressList[0];
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
return null;
}
}
} }
} }

File diff suppressed because it is too large Load Diff

@ -29,6 +29,7 @@ namespace ServerForm
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.logConsole = new System.Windows.Forms.TextBox(); this.logConsole = new System.Windows.Forms.TextBox();
this.clientListBox = new System.Windows.Forms.ListBox(); this.clientListBox = new System.Windows.Forms.ListBox();
this.mainTablePanel = new System.Windows.Forms.TableLayoutPanel(); this.mainTablePanel = new System.Windows.Forms.TableLayoutPanel();
@ -99,7 +100,7 @@ namespace ServerForm
this.Controls.Add(this.statusStrip); this.Controls.Add(this.statusStrip);
this.Controls.Add(this.mainTablePanel); this.Controls.Add(this.mainTablePanel);
this.Font = new System.Drawing.Font("맑은 고딕", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129))); this.Font = new System.Drawing.Font("맑은 고딕", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.Name = "MainForm"; this.Name = "MainForm";
this.Text = "Server"; this.Text = "Server";

@ -114,6 +114,7 @@ namespace ServerForm
DataManager.Instance.Process(sender, dataType, data, logHandler, formHandler); DataManager.Instance.Process(sender, dataType, data, logHandler, formHandler);
} }
private void HeartBeatServer_OnDataReceived(PClientInfo sender, PDataType dataType, byte[] data) private void HeartBeatServer_OnDataReceived(PClientInfo sender, PDataType dataType, byte[] data)
{ {
if (dataType != PDataType.SimpleString) if (dataType != PDataType.SimpleString)
@ -127,6 +128,12 @@ namespace ServerForm
logHandler(sender.Client, "WARNING!!! BAD HEARTBEAT!!!"); logHandler(sender.Client, "WARNING!!! BAD HEARTBEAT!!!");
sender.Client.Send(PServer.HB_CHECK); sender.Client.Send(PServer.HB_CHECK);
UpdateHeartBeat();
}
private void UpdateHeartBeat()
{
heartBeatStatus.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
} }
private void ClientCheckTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) private void ClientCheckTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

Loading…
Cancel
Save