main
syneffort 2 years ago
parent bce6521e43
commit a3a0cc201b
  1. 45
      MySolution/ConsoleApp/TelnetSamples/AsyncSocketTelnetClient.cs
  2. 1
      MySolution/ConsoleApp/TelnetSamples/AsyncStreamTelnetClient.cs
  3. 18
      MySolution/ConsoleApp/TelnetSamples/AsyncTelnetConsole.cs
  4. 1
      MySolution/ConsoleApp/TelnetSamples/IAsyncTelnetClient.cs
  5. 8
      MySolution/ConsoleApp/TelnetSamples/TelnetConsole.cs
  6. 26
      MySolution/SerialComApp/SerialCommApp.Designer.cs
  7. 28
      MySolution/SerialComApp/SerialCommApp.resx

@ -12,9 +12,10 @@ namespace ConsoleApp.TelnetSamples
{
internal class AsyncSocketTelnetClient : IAsyncTelnetClient
{
private readonly CancellationTokenSource CTS = new CancellationTokenSource();
private CancellationTokenSource _cts;
public event EventHandler<string> MessageCallback;
public event EventHandler<Exception> ErrorCallback;
private Socket _socket;
@ -22,7 +23,7 @@ namespace ConsoleApp.TelnetSamples
{
try
{
Close();
//Close();
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_socket.ReceiveTimeout = 1000;
@ -31,11 +32,13 @@ namespace ConsoleApp.TelnetSamples
IPEndPoint iPEndPoint = new IPEndPoint(ipAddress, port);
await _socket.ConnectAsync(iPEndPoint);
Task.Run(ReadAsync);
_cts = new CancellationTokenSource();
Task.Run(ReadAsync, _cts.Token);
}
catch (Exception)
catch (Exception ex)
{
throw;
if (this.ErrorCallback != null)
this.ErrorCallback(this, ex);
}
}
@ -43,14 +46,21 @@ namespace ConsoleApp.TelnetSamples
{
try
{
StringBuilder sb = new StringBuilder();
byte[] readBuffer = new byte[1024];
while (true)
{
int bytesRead = await _socket.ReceiveAsync(readBuffer);
if (_cts != null && _cts.IsCancellationRequested)
break;
int bytesRead = _cts != null && _cts.IsCancellationRequested ? 0 : await _socket.ReceiveAsync(readBuffer);
if (bytesRead < 1)
{
if (sb.Length > 0 && this.MessageCallback != null)
this.MessageCallback(this, sb.ToString());
break;
}
string data = Encoding.ASCII.GetString(readBuffer, 0, bytesRead);
sb.Append(data);
@ -69,9 +79,10 @@ namespace ConsoleApp.TelnetSamples
sb.Clear();
}
}
catch (Exception)
catch (Exception ex)
{
throw;
if (this.ErrorCallback != null)
this.ErrorCallback(this, ex);
}
}
@ -79,13 +90,18 @@ namespace ConsoleApp.TelnetSamples
{
try
{
command = command.Replace(" ", "");
if (string.IsNullOrEmpty(command))
return;
command += "\r\n";
byte[] sendBytes = Encoding.ASCII.GetBytes(command);
await _socket.SendAsync(sendBytes);
}
catch (Exception)
catch (Exception ex)
{
throw;
if (this.ErrorCallback != null)
this.ErrorCallback(this, ex);
}
}
@ -96,13 +112,16 @@ namespace ConsoleApp.TelnetSamples
if (_socket == null)
return;
_cts.Cancel();
_socket.Shutdown(SocketShutdown.Both);
_socket.Close();
_socket.Dispose();
}
catch (Exception)
catch (Exception ex)
{
throw;
if (this.ErrorCallback != null)
this.ErrorCallback(this, ex);
}
}
}

@ -10,6 +10,7 @@ namespace ConsoleApp.TelnetSamples
internal class AsyncStreamTelnetClient : IAsyncTelnetClient
{
public event EventHandler<string> MessageCallback;
public event EventHandler<Exception> ErrorCallback;
private TcpClient _client;
private NetworkStream _stream;

@ -20,21 +20,31 @@ namespace ConsoleApp.TelnetSamples
try
{
_client.MessageCallback += On_Receive;
_client.ErrorCallback += On_ErrorCallback;
_client.Connect(ip, port);
while (true)
{
string command = Console.ReadLine();
string command = Console.ReadLine().ToLower();
if (command == "quit" || command == "exit")
break;
else if (command == "disconnect" || command == "disconn")
_client.Close();
else if (command == "connect" || command == "conn")
_client.Connect(ip, port);
else
_client.SendCommand(command);
}
}
catch (Exception)
catch (Exception ex)
{
throw;
Console.WriteLine($"[ERR] {ex.Message}");
}
}
private void On_ErrorCallback(object? sender, Exception e)
{
Console.WriteLine(e.Message);
}
private void On_Receive(object? sender, string e)

@ -9,6 +9,7 @@ namespace ConsoleApp.TelnetSamples
public interface IAsyncTelnetClient
{
public event EventHandler<string> MessageCallback;
public event EventHandler<Exception> ErrorCallback;
void Connect(string ip, int port = 23);
void SendCommand(string command);

@ -24,10 +24,14 @@ namespace ConsoleApp.TelnetSamples
while (true)
{
string command = Console.ReadLine();
string command = Console.ReadLine().ToLower();
if (command == "quit" || command == "exit")
break;
else if (command == "disconnect" || command == "disconn")
_client.Close();
else if (command == "connect" || command == "conn")
_client.Connect(ip, port);
else
Console.Write(_client.SendCommand(command));
}
}

@ -33,6 +33,7 @@
this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
this.connectButton = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.portRefreshButton = new System.Windows.Forms.Button();
this.settingButton = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.portComboBox = new System.Windows.Forms.ComboBox();
@ -40,7 +41,6 @@
this.sendButton = new System.Windows.Forms.Button();
this.commandText = new System.Windows.Forms.TextBox();
this.printText = new System.Windows.Forms.TextBox();
this.portRefreshButton = new System.Windows.Forms.Button();
this.tableLayoutPanel1.SuspendLayout();
this.tableLayoutPanel2.SuspendLayout();
this.panel1.SuspendLayout();
@ -109,6 +109,18 @@
this.panel1.Size = new System.Drawing.Size(350, 32);
this.panel1.TabIndex = 2;
//
// portRefreshButton
//
this.portRefreshButton.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("portRefreshButton.BackgroundImage")));
this.portRefreshButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.portRefreshButton.Location = new System.Drawing.Point(159, 4);
this.portRefreshButton.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.portRefreshButton.Name = "portRefreshButton";
this.portRefreshButton.Size = new System.Drawing.Size(25, 24);
this.portRefreshButton.TabIndex = 2;
this.portRefreshButton.UseVisualStyleBackColor = true;
this.portRefreshButton.Click += new System.EventHandler(this.portRefreshButton_Click);
//
// settingButton
//
this.settingButton.Location = new System.Drawing.Point(207, 3);
@ -191,18 +203,6 @@
this.printText.TabIndex = 2;
this.printText.TabStop = false;
//
// portRefreshButton
//
this.portRefreshButton.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("portRefreshButton.BackgroundImage")));
this.portRefreshButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.portRefreshButton.Location = new System.Drawing.Point(159, 4);
this.portRefreshButton.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.portRefreshButton.Name = "portRefreshButton";
this.portRefreshButton.Size = new System.Drawing.Size(25, 24);
this.portRefreshButton.TabIndex = 2;
this.portRefreshButton.UseVisualStyleBackColor = true;
this.portRefreshButton.Click += new System.EventHandler(this.portRefreshButton_Click);
//
// SerialCommApp
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);

@ -120,20 +120,20 @@
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="portRefreshButton.BackgroundImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
YQUAAAAJcEhZcwAACxIAAAsSAdLdfvwAAALASURBVGhD7dnJy01xHMfxa8yQqYSdBdnYiGxEKJFhZ84Q
pchQeshCRMofoFDGlFnZWFiwUYrM4wJhY2lIGTLz/ixOnU6fc+45557ffdD51GvzdO/v+7v3nt/4NOrU
qVPnn84obMBZ3Mc7fMdXvMZtHMdqDMdfke5Yjhv4XcAvXMV8dEOnZDaew3WwiIeYiralL47BdaYs/SJ7
0BNBMwz34DpRhSsYhCAZimdwhdN8wbfE35q5hQGoNH1wF65gnF6zHeMwGFGGYCJ24wnce+MuodLBfQSu
UEQDcSbypCsWo9kEsAuVRB1zBSJ7oem0aPrjHFybojVkDFqKOpb13Heg1eyDa1suo6UsgWtYVLiK6JE6
D1dDJqB0rsM1+gA9UFU04LXdc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1Q
VTTgtd1wtU6gVEbCNSizUHW0R3K1PqAXCmctXIN3ECKaqt/D1ZyOwjkF19g2hMpBuJpaWwonbeEai1BZ
CVfzNArnDVxjA9FKumAKphnr4GpqoXSvn4TU6CCSbOgTqsg8aKFKtl+EdrDLkBr3AT6jqizADyRr5LUF
mQn1CMWzAj/h6mTZj6Zp1yBeBT0OrpZzAbl2qifhGggxja5Bng9xEzoR5kraQqZfJkTWI+tDvIDOFbkz
AmkN6kAfIhvh6mk86tqmcK7BNfgIVW7m4tmMeC3NfKV3pFnb6aMIlU1QDc1Qc/WHstFof4pk5yP6tkJF
+x89Ui1nBlznI5qTyzxO/bAV2oUGzyG4zkceYw7yRCewhXgFvfcigl9o9YYuZ5MdT9Kl7g6MR3zK0+o9
GTvxEsn3nUHwO1J1KGs8OLrYcnsqR2cB7VSDRrdzOpG5DlRB24rg0aA7DNeBsvRLaYtS5n6pdDQ7FX2k
HN18jEanRN/YUqRdv6TRAqVdZVv/L9AsuobRkVDnV13Bv4Vupj9CGzF9yANYBF3T16lTp85/lUbjD7+Y
OvUDT7UkAAAAAElFTkSuQmCC
</value>
</data>
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">

Loading…
Cancel
Save