I have a TCP Listener with NetworkStream
that reads 530 bytes from the Client once a second.
Within the TCP Listener BackgroundWorker
, I write those bytes into a MemoryStream
, and invoke a method that breaks down the MemoryStream
into smaller arrays.
Then, I have another BackgroundWorker
that constantly compares those smaller arrays to a set of predefined byte arrays- based on the outcome it performs different tasks
While this works fine for what I'm doing, I wonder if there's a better, more efficient way of doing it.
Here's my code:
namespace TcpServerTest
{
public partial class Form1 : Form
{
public TcpListener server = null;
public TcpClient client;
public Int32 port = 6100;
public IPAddress localAddr = IPAddress.Parse("127.0.0.1");
public MemoryStream memStream = new MemoryStream(2000);
Byte[] bytesFromTCP = new Byte[530];
byte[] SendtoPlcZeroCalPASS = { 0x0A, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x2B };
byte[] SendtoPlcZeroCalFAIL = { 0x0A, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x58 };
byte[] SendtoPlcSmokeCalPASS = { 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x4C };
byte[] SendtoPlcSmokeCalFAIL = { 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x46, 0xC9 };
public byte[] arrInt1 = new byte[2];
public byte[] _arrInt1 = { 0x10, 0x02 };
public byte[] arrBool1 = new byte[2];
public byte[] _arrBool1 = { 0x01, 0x00 };
public byte[] arrBool2 = new byte[2];
public byte[] _arrBool2 = { 0x01, 0x00 };
public byte[] arrBool3 = new byte[2];
public byte[] _arrBool3 = { 0x01, 0x00 };
public byte[] arrBool4 = new byte[2];
public byte[] _arrBool4 = { 0x01, 0x00 };
public byte[] arrString1 = new byte[256];
public byte[] _arrString1 = new byte[256];
public byte[] arrString2 = new byte[256];
public byte[] arrBool5 = new byte[2];
public byte[] _arrBool5 = { 0x01, 0x00 };
public byte[] arrBool6 = new byte[2];
public byte[] _arrBool6 = { 0x01, 0x00 };
public byte[] arrBool7 = new byte[2];
public byte[] _arrBool7 = { 0x01, 0x00 };
public byte[] arrCrc = new byte[2];
public Form1()
{
InitializeComponent();
bgWorkerSocketListener.DoWork += bgWorkerSocketListener_DoWork;
bgWorkerSocketListener.WorkerSupportsCancellation = true; //Allow for the background worker process to be cancelled
bgWorkerSocketListener.WorkerReportsProgress = true;
bgWorkerSocketListener.RunWorkerAsync();
bgWorkerTcpProcessor.DoWork += bgWorkerTcpProcessor_DoWork;
bgWorkerTcpProcessor.WorkerSupportsCancellation = true; //Allow for the background worker process to be cancelled
bgWorkerTcpProcessor.WorkerReportsProgress = true;
bgWorkerTcpProcessor.RunWorkerAsync();
}
public void bgWorkerSocketListener_DoWork(object sender, DoWorkEventArgs e) //Start the TCP Listener
{
try
{
AppendTBoxTcpBytes("Waiting for a connection... " + Environment.NewLine);
//Create a listener
server = new TcpListener(localAddr, port);
//Start listening for client requests.
server.Start();
String data = null;
//Enter the listening loop.
while (true)
{
//Perform a blocking call to accept requests.
client = server.AcceptTcpClient();
AppendTBoxTcpBytes("Connected!" + Environment.NewLine);
data = null;
//Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
while (client.Connected == true)
{
try
{
//Loop to receive all the data sent by the client.
while ((i = stream.Read(bytesFromTCP, 0, bytesFromTCP.Length)) != 0)
{
//Translate data
data = BitConverter.ToString(bytesFromTCP);
AppendTBoxTcpBytes(" Data: " + Environment.NewLine + data + Environment.NewLine + "Length: " + data.Length
+ Environment.NewLine + DateTime.Now.ToString("dd MMMM yyyy HH:mm:ss"));
memStream.Write(bytesFromTCP, 0, bytesFromTCP.Length);
this.Invoke(new EventHandler(processMessageMemStr)); //Process incoming message
memStream.Position = 0;
}
}
catch (Exception ex){}
}
}
}
catch (SocketException ex)
{
MessageBox.Show("SocketException: " + ex);
}
}
private void processMessageMemStr(object sender, EventArgs e) //Process received messages from Memory Stream
{
try
{
memStream.Position = 0;
memStream.Read(arrInt1, 0, 2);
memStream.Read(arrBool1, 0, 2);
memStream.Read(arrBool2, 0, 2);
memStream.Read(arrBool3, 0, 2);
memStream.Read(arrBool4, 0, 2);
memStream.Read(arrString1, 0, 256);
memStream.Read(arrString2, 0, 256);
memStream.Read(arrBool5, 0, 2);
memStream.Read(arrBool6, 0, 2);
memStream.Read(arrBool7, 0, 2);
memStream.Read(arrCrc, 0, 2);
}
catch (Exception err)
{ MessageBox.Show(err.ToString()); }
}
public void bgWorkerTcpProcessor_DoWork(object sender, DoWorkEventArgs e) //Start the TCP Listener
{
ASCIIEncoding ascii = new ASCIIEncoding();
while (true)
{
try
{
if (string.Join(",", arrBool1) == string.Join(",", _arrBool1))
pictureBox2.Image = Resources.green_on;
else
pictureBox2.Image = Resources.led_off;
if (string.Join(",", arrBool2) == string.Join(",", _arrBool2))
pictureBox1.Image = Resources.green_on;
else
pictureBox1.Image = Resources.led_off;
if (string.Join(",", arrBool3) == string.Join(",", _arrBool3))
pictureBox3.Image = Resources.red_on;
else
pictureBox3.Image = Resources.led_off;
if (string.Join(",", arrBool4) == string.Join(",", _arrBool4))
pictureBox4.Image = Resources.red_on;
else
pictureBox4.Image = Resources.led_off;
if (string.Join(",", arrBool5) == string.Join(",", _arrBool5))
pictureBox5.Image = Resources.green_on;
else
pictureBox5.Image = Resources.red_on;
if (string.Join(",", arrBool6) == string.Join(",", _arrBool6))
pictureBox6.Image = Resources.green_on;
else
pictureBox6.Image = Resources.led_off;
if (string.Join(",", arrBool7) == string.Join(",", _arrBool7))
{
pictureBox7.Image = Resources.green_on;
pictureBox8.Image = Resources.led_off;
}
else
{
pictureBox7.Image = Resources.led_off;
pictureBox8.Image = Resources.green_on;
}
AppendTBoxAlarm(ascii.GetString(arrString1, 0, 256));
AppendTBoxStep(ascii.GetString(arrString2, 0, 256));
}
catch { }
}
}
private void btnSendToClient_Click_1(object sender, EventArgs e)
{
try
{
server = new TcpListener(localAddr, port);
NetworkStream stream = client.GetStream();
if (cBoxSendToPLC.SelectedItem == "Test1 PASS")
stream.Write(SendtoPlcZeroCalPASS, 0, SendtoPlcZeroCalPASS.Length);
if (cBoxSendToPLC.SelectedItem == "Test2 PASS")
stream.Write(SendtoPlcSmokeCalPASS, 0, SendtoPlcZeroCalPASS.Length);
if (cBoxSendToPLC.SelectedItem == "Test1 FAIL")
stream.Write(SendtoPlcZeroCalFAIL, 0, SendtoPlcZeroCalPASS.Length);
if (cBoxSendToPLC.SelectedItem == "Test2 FAIL")
stream.Write(SendtoPlcSmokeCalFAIL, 0, SendtoPlcZeroCalPASS.Length);
else{}
}
catch (Exception err)
{
MessageBox.Show("No client is yet connected to the server.\n" + "Exception: " + err.Message, "Client not connected", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public void AppendTBoxTcpBytes(string value) //Appends text, prevents UI from freezing
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(AppendTBoxTcpBytes), new object[] { value });
return;
}
tBoxTcpBytes.Clear();
tBoxTcpBytes.Text += value;
}
public void AppendTBoxAlarm(string value) //Appends text, prevents UI from freezing
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(AppendTBoxAlarm), new object[] { value });
return;
}
tBoxAlarm.Clear();
tBoxAlarm.Text += value;
}
public void AppendTBoxStep(string value) //Appends text, prevents UI from freezing
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(AppendTBoxStep), new object[] { value });
return;
}
tBoxStep.Clear();
tBoxStep.Text += value;
}
}
}