Login Login
MORE

WIDGETS

Widgets

Wanted articles
Who is online?
Article tools

CSharp:Multithreading Windows Form UDP TCP Tester

From Aino Wiki

Jump to: navigation, search

TCP vs UDP

Essenzialmente:

  • UDP è più veloce del TCP perché i pacchetti sono inviati senza garantire la consegna;
  • TCP è sicuro perché ogni pacchetto ha la conferma e l'ordine.

Ottima guida skullbox.net

Codice completo

E' una Windows app che fa da simulatore client e server UDP. Il codice è predisposto anche per il TCP ma non è stato ultimato.
Ci sono 2 TAB, uno per il test del "Talker" che è il client che invia testo ed uno per il "Listner" che funge da server. E' camblato dentro l'IP a cui entrambe i ruoli fanno riferimento.

using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Configuration;
using System.Collections.Generic;
 
namespace UdpCustomClient
{
    public class FrmMain : Form
    {
        private IContainer components = null;
        #region Variabili - Costanti - Proprietà - Oggetti del FORM
        #region Const
        private const string IP_SERVERADDRESS_DEFAULT = "10.150.21.179"; //10.150.21.179 192.168.1.7
        private const string PORT_DEFAULT = "20000";
        private const string LOCALPORT_DEFAULT = "20002";
        private const int MAXITERATIONS_DEFAULT = 500;
        #endregion
 
        #region Properties
        private string IpServerAddress
        {
            get
            {
                string ip = IP_SERVERADDRESS_DEFAULT;
                if (ConfigurationManager.AppSettings["IpServerAddress"] != null)
                {
                    string tmp = ConfigurationManager.AppSettings["IpServerAddress"].ToString();
                    if (!string.IsNullOrEmpty(tmp))
                    {
                        ip = tmp;
                    }
                }
                return ip;
            }
        }
        private string Port
        {
            get
            {
                string port = PORT_DEFAULT;
                if (ConfigurationManager.AppSettings["Port"] != null)
                {
                    string tmp = ConfigurationManager.AppSettings["Port"].ToString();
                    if (!string.IsNullOrEmpty(tmp))
                    {
                        port = tmp;
                    }
                }
                return port;
            }
        }
        private string LocalPort
        {
            get
            {
                string localPort = LOCALPORT_DEFAULT;
                if (ConfigurationManager.AppSettings["LocalPort"] != null)
                {
                    string tmp = ConfigurationManager.AppSettings["LocalPort"].ToString();
                    if (!string.IsNullOrEmpty(tmp))
                    {
                        localPort = tmp;
                    }
                }
                return localPort;
            }
        }
        private int MaxIterations
        {
            get
            {
                int maxIterations = MAXITERATIONS_DEFAULT;
                if (ConfigurationManager.AppSettings["MaxIterations"] != null)
                {
                    int tmp = 0;
                    int.TryParse(ConfigurationManager.AppSettings["MaxIterations"].ToString(), out tmp);
                    if (tmp > 0)
                    {
                        maxIterations = tmp;
                    }
                }
                return maxIterations;
            }
        }
 
        //
        private bool StartUDPListner
        {
            get
            {
                bool startUDPListner = true;
                if (ConfigurationManager.AppSettings["StartUDPListner"] != null)
                {
                    bool tmp = true;
                    if (bool.TryParse(ConfigurationManager.AppSettings["StartUDPListner"].ToString(), out tmp))
                    {
                        startUDPListner = tmp;
                    }
                }
                return startUDPListner;
            }
        }
        #endregion
 
        #region Variables
        private volatile bool _stopLoop = false;
        private volatile bool _isUDPListnerStarted = false;
        List<Thread> _lstAllThreads = new List<Thread>();
        #endregion
 
        #region Oggetti del Form
        private Label label1;
        private Label label2;
        private Label label3;
 
        private TextBox TxtPathFileToLoad;
        private TextBox TxtStringToMessageToSend;
        private TextBox TxtPortServer;
        private TextBox TxtIPServerAddress;
        private TextBox TxtLocalPort;
 
        private Button BtnUPDSend;
        private Button BtnLoadData;
        private volatile CheckBox ChkLoopInputSentences;
        private Button BtnStopSending;
        private Label label4;
        private TabControl tabControl1;
        private TabPage TbUDPTCPTalker;
        private TabPage TbUDPListner;
        private volatile StatusStrip SSMain;
        private volatile ToolStripStatusLabel SSNrIterationLabel;
        private volatile ToolStripStatusLabel SSNrIteration;
        private volatile ToolStripStatusLabel SSStatus;
        private volatile TextBox TxtReceivedUDPMessages;
        private ToolStripStatusLabel SSListnerStatusLabel;
        private ToolStripStatusLabel SSListnerStatus;
        private ToolStripStatusLabel SSNrReceivedMsgLabel;
        private volatile ToolStripStatusLabel SSNrReceivedMsg;
        private Label label5;
        private TextBox TxtUDPListnerPort;
        private Label label6;
        private TextBox TxtUDPListnerAddress;
        private TextBox TxtUDPListnerLocalPort;
        private Label label7;
        private Button BtnRestartListner;
        private Button BtnKillAllThreads;
        private volatile TextBox TxtUDPListnerLog;
        private ToolStripStatusLabel toolStripStatusLabel1;
        private Button BtnTCPSend;
        #endregion
        #endregion
 
        delegate void SetTextCallback(string text);
        public FrmMain()
        {
            this.InitializeComponent();
            TxtIPServerAddress.Text = IpServerAddress;
            TxtPortServer.Text = Port;
            TxtLocalPort.Text = LocalPort;
 
            TxtUDPListnerAddress.Text = IpServerAddress;
            TxtUDPListnerPort.Text = Port;
            TxtUDPListnerLocalPort.Text = LocalPort;
 
            try
            {
                StartThreadUDPListner(); ;
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("{0}", ex.Message), string.Format("FrmMain"),
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
        #region Handlers di Eventi
        #region Pulsanti Send[Protocolo]
        private void BtnUPDSend_Click(object sender, EventArgs e)
        {
            int loopCounter = 0;
            //string strCounter = string.Empty;
 
            try
            {
                int portNr = Convert.ToInt32(this.TxtUDPListnerPort.Text);
                int localPortNr = Convert.ToInt32(this.TxtUDPListnerLocalPort.Text);
                string ipServerAddress = this.TxtUDPListnerAddress.Text;
 
                SSStatus.Text = "UPDSend";
 
                // Avvio il Client Listner UDP
                StartThreadUDPListner();
 
                UdpClient udpClient = new UdpClient(localPortNr);
 
                do
                {
                    //strCounter = " " + (loopCounter + 1);
                    byte[] bytes = Encoding.ASCII.GetBytes(this.TxtStringToMessageToSend.Text); // + strCounter);
                    udpClient.Send(bytes, (int)bytes.Length, ipServerAddress, portNr);
 
                    loopCounter++;
                    SSNrIteration.Text = loopCounter.ToString();
                    //SSNrIteration.Invalidate();
                    Application.DoEvents();
                    if (ChkLoopInputSentences.Checked)
                    { 
                        Thread.Sleep(1 * 1000);
                    }
                } while (ChkLoopInputSentences.Checked
                    && loopCounter < MaxIterations
                    && !_stopLoop);
                udpClient.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("{0}", ex.Message), string.Format("UDP Send"),
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (loopCounter > 1)
            {
                SSStatus.Text = "Iteration ended.";
            }
            //MessageBox.Show(string.Format("Finito. {0} loop.", loopCounter), string.Format("UDP Send"),
            //                    MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
 
        private void BtnTCPSend_Click(object sender, EventArgs e)
        {
            try
            {
                int portNr = Convert.ToInt32(this.TxtPortServer.Text);
                string ipServerAddress = this.TxtIPServerAddress.Text;
 
                SSStatus.Text = "UPDSend";
 
                TcpClient tcpClient = new TcpClient();
 
                tcpClient.Connect(new IPEndPoint(IPAddress.Parse(ipServerAddress), portNr));
                NetworkStream stream = tcpClient.GetStream();
                ASCIIEncoding aSCIIEncoding = new ASCIIEncoding();
                byte[] bytes = Encoding.ASCII.GetBytes(this.TxtStringToMessageToSend.Text);
                stream.Write(BitConverter.GetBytes((int)bytes.Length), 0, 4);
                stream.Write(bytes, 0, (int)bytes.Length);
                stream.Flush();
                tcpClient.Close();
 
                MessageBox.Show(string.Format("OK."), string.Format("TCP Send"),
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("{0}", ex.Message), string.Format("TCP Send"),
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion
 
        #region Pulsanti controllo Trhead
        private void BtnRestartListner_Click(object sender, EventArgs e)
        {
            try
            {
                StartThreadUDPListner();
            }
            catch (Exception ex)
            {                
                MessageBox.Show(string.Format("{0}", ex.Message), string.Format("UDP Listner restart"),
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void BtnKillAllThreads_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Thread t in _lstAllThreads)
                {
                    if (t.IsAlive)
                    {
                        t.Abort();
                    }
                }
                _isUDPListnerStarted = false;
            }
            catch (Exception ex)
            {                
                MessageBox.Show(string.Format("{0}", ex.Message), string.Format("Kill Threads"),
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void BtnStopSending_Click(object sender, EventArgs e)
        {
            _stopLoop = true;
            SSStatus.Text = "Executing stop request.";
        }
        #endregion
 
        private void BtnLoadData_Click(object sender, EventArgs e)
        {
            string pathFileToLoad = TxtPathFileToLoad.Text;
            try
            {
                StreamReader streamReader = new StreamReader(pathFileToLoad);
                string empty = string.Empty;
                string strLine = streamReader.ReadLine();
                int nrSentences = 0;
                while (strLine != null)
                {
                    if (strLine.StartsWith("##"))
                    {
                        string[] strArrays = new string[] { "##" };
                        string[] strArrays1 = strLine.Split(strArrays, StringSplitOptions.RemoveEmptyEntries);
                        if ((int)strArrays1.Length >= 3)
                        {
                            empty = string.Concat(empty, strArrays1[2], "\r\n");
                            nrSentences++;
                        }
                    }
                    else if (strLine.StartsWith("$AIVDM"))
                    {
                        empty = string.Concat(empty, strLine, "\r\n");
                        nrSentences++;
                    }
                    strLine = streamReader.ReadLine();
                }
                this.TxtStringToMessageToSend.Text = empty;
                MessageBox.Show(string.Format("Numero di sentenze: {0}", nrSentences));
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("{0}", ex.Message), string.Format("Caricando {0}", pathFileToLoad), 
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
        private void ChkLoopInputSentences_CheckedChanged(object sender, EventArgs e)
        {
            BtnStopSending.Enabled = ChkLoopInputSentences.Checked;
            if (ChkLoopInputSentences.Checked)
            {
            }
        }
        #endregion
 
        #region Metodi Privati
        private void StartThreadUDPListner()
        {
            // Avvio il Client Listner UDP
            if (StartUDPListner
                && !_isUDPListnerStarted)
            { 
                ParameterizedThreadStart th = new ParameterizedThreadStart(ThreadUDPListner);
                Thread t = new Thread(th);
                _lstAllThreads.Add(t);
                SSListnerStatus.Text = "Starting....";
                Application.DoEvents();
                t.Start();
                _isUDPListnerStarted = true;
            }
        }
        #endregion
 
        #region Thread
        private void ThreadUDPListner(object data)
        {
            UdpClient listener = null;
            int nrListnedMsg = 0;
 
            try
            {
                int portNr = Convert.ToInt32(this.TxtPortServer.Text);
                int localPortNr = Convert.ToInt32(this.TxtLocalPort.Text);
                string ipServerAddress = this.TxtIPServerAddress.Text;
 
                if (_stopLoop)
                {
                    SSListnerStatus.Text = "Thread Stop.";
                    return;
                }
                SSListnerStatus.Text = "Started.";
 
                #region Listner Code
                listener = new UdpClient(portNr);
                IPAddress ipAddress = IPAddress.Parse(ipServerAddress);
                IPEndPoint groupEP = new IPEndPoint(ipAddress, portNr); // IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, portNr);
                string strReceived_data;
                byte[] receive_byte_array;
 
                while (!_stopLoop)
                {
                    SSListnerStatus.Text = "Listning...";
 
                    receive_byte_array = listener.Receive(ref groupEP);
                    //Console.WriteLine("Received a broadcast from {0}", groupEP.ToString());
 
                    strReceived_data = Encoding.ASCII.GetString(receive_byte_array, 0, receive_byte_array.Length);
                    if (!string.IsNullOrEmpty(strReceived_data))
                    {
                        Application.DoEvents();
                        AppendTxtReceivedUDPMessages(strReceived_data);
                        nrListnedMsg++;
                        SSNrReceivedMsg.Text = nrListnedMsg.ToString();
                        //SSNrReceivedMsg.Invalidate();
                    }
                    Application.DoEvents();
                } //while (!_stopLoop)
                #endregion
 
                if (_stopLoop)
                {
                    SSListnerStatus.Text = "Thread Stop.";
                    return;
                }
            }
            catch (Exception ex)
            {
                SSListnerStatus.Text = "Exception!";
                Application.DoEvents();
                AppendTxtUDPListnerLog("[ThreadUDPListner()] " + ex.Message);
                Application.DoEvents();
            }
            finally
            {
                if (listener != null)
                {
                    listener.Close();
                }
            }
        }
        #endregion
 
        #region UI vs Threds
        private void AppendTxtUDPListnerLog(string text)
        {
            if (this.TxtUDPListnerLog.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(AppendTxtUDPListnerLog);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.TxtUDPListnerLog.Text += text + "\r\n";
            }
        }
        private void AppendTxtReceivedUDPMessages(string text)
        {
            if (this.TxtReceivedUDPMessages.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(AppendTxtReceivedUDPMessages);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.TxtReceivedUDPMessages.Text += text + "\r\n";
            }
        }
 
        //private void SetSSNrReceivedMsg(string text)
        //{
        //    if (((TextBox)(SSMain.Items["SetSSNrReceivedMsg"])).InvokeRequired)
        //    {
        //        SetTextCallback d = new SetTextCallback(SetSSNrReceivedMsg);
        //        this.Invoke(d, new object[] { text });
        //    }
        //    else
        //    {
        //        SetSSNrReceivedMsg.Text += text + "\r\n";
        //    }
        //}
        #endregion
 
        #region Inizializzarione e fine
        protected override void Dispose(bool disposing)
        {
            if ((!disposing ? false : this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        private void InitializeComponent()
        {
            this.BtnUPDSend = new System.Windows.Forms.Button();
            this.TxtStringToMessageToSend = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.TxtPortServer = new System.Windows.Forms.TextBox();
            this.TxtIPServerAddress = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.TxtLocalPort = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.BtnLoadData = new System.Windows.Forms.Button();
            this.BtnTCPSend = new System.Windows.Forms.Button();
            this.TxtPathFileToLoad = new System.Windows.Forms.TextBox();
            this.ChkLoopInputSentences = new System.Windows.Forms.CheckBox();
            this.BtnStopSending = new System.Windows.Forms.Button();
            this.label4 = new System.Windows.Forms.Label();
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.TbUDPTCPTalker = new System.Windows.Forms.TabPage();
            this.TbUDPListner = new System.Windows.Forms.TabPage();
            this.TxtReceivedUDPMessages = new System.Windows.Forms.TextBox();
            this.SSMain = new System.Windows.Forms.StatusStrip();
            this.SSNrIterationLabel = new System.Windows.Forms.ToolStripStatusLabel();
            this.SSNrIteration = new System.Windows.Forms.ToolStripStatusLabel();
            this.SSStatus = new System.Windows.Forms.ToolStripStatusLabel();
            this.SSListnerStatusLabel = new System.Windows.Forms.ToolStripStatusLabel();
            this.SSListnerStatus = new System.Windows.Forms.ToolStripStatusLabel();
            this.SSNrReceivedMsgLabel = new System.Windows.Forms.ToolStripStatusLabel();
            this.SSNrReceivedMsg = new System.Windows.Forms.ToolStripStatusLabel();
            this.BtnRestartListner = new System.Windows.Forms.Button();
            this.label5 = new System.Windows.Forms.Label();
            this.TxtUDPListnerPort = new System.Windows.Forms.TextBox();
            this.label6 = new System.Windows.Forms.Label();
            this.TxtUDPListnerAddress = new System.Windows.Forms.TextBox();
            this.TxtUDPListnerLocalPort = new System.Windows.Forms.TextBox();
            this.label7 = new System.Windows.Forms.Label();
            this.BtnKillAllThreads = new System.Windows.Forms.Button();
            this.TxtUDPListnerLog = new System.Windows.Forms.TextBox();
            this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
            this.tabControl1.SuspendLayout();
            this.TbUDPTCPTalker.SuspendLayout();
            this.TbUDPListner.SuspendLayout();
            this.SSMain.SuspendLayout();
            this.SuspendLayout();
            // 
            // BtnUPDSend
            // 
            this.BtnUPDSend.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.BtnUPDSend.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
            this.BtnUPDSend.Location = new System.Drawing.Point(0, 482);
            this.BtnUPDSend.Name = "BtnUPDSend";
            this.BtnUPDSend.Size = new System.Drawing.Size(75, 23);
            this.BtnUPDSend.TabIndex = 0;
            this.BtnUPDSend.Text = "UDP Send";
            this.BtnUPDSend.UseVisualStyleBackColor = true;
            this.BtnUPDSend.Click += new System.EventHandler(this.BtnUPDSend_Click);
            // 
            // TxtStringToMessageToSend
            // 
            this.TxtStringToMessageToSend.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.TxtStringToMessageToSend.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
            this.TxtStringToMessageToSend.Location = new System.Drawing.Point(6, 1);
            this.TxtStringToMessageToSend.Multiline = true;
            this.TxtStringToMessageToSend.Name = "TxtStringToMessageToSend";
            this.TxtStringToMessageToSend.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.TxtStringToMessageToSend.Size = new System.Drawing.Size(670, 439);
            this.TxtStringToMessageToSend.TabIndex = 1;
            this.TxtStringToMessageToSend.Text = "messaggio di prova";
            // 
            // label1
            // 
            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label1.AutoSize = true;
            this.label1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
            this.label1.Location = new System.Drawing.Point(197, 449);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(26, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "Port";
            // 
            // TxtPortServer
            // 
            this.TxtPortServer.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.TxtPortServer.Location = new System.Drawing.Point(229, 446);
            this.TxtPortServer.Name = "TxtPortServer";
            this.TxtPortServer.Size = new System.Drawing.Size(100, 20);
            this.TxtPortServer.TabIndex = 3;
            this.TxtPortServer.Text = "20000";
            // 
            // TxtIPServerAddress
            // 
            this.TxtIPServerAddress.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.TxtIPServerAddress.Location = new System.Drawing.Point(55, 446);
            this.TxtIPServerAddress.Name = "TxtIPServerAddress";
            this.TxtIPServerAddress.Size = new System.Drawing.Size(137, 20);
            this.TxtIPServerAddress.TabIndex = 5;
            this.TxtIPServerAddress.Text = "10.150.21.179";
            // 
            // label2
            // 
            this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(4, 449);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(45, 13);
            this.label2.TabIndex = 4;
            this.label2.Text = "Address";
            // 
            // TxtLocalPort
            // 
            this.TxtLocalPort.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.TxtLocalPort.Location = new System.Drawing.Point(573, 446);
            this.TxtLocalPort.Name = "TxtLocalPort";
            this.TxtLocalPort.Size = new System.Drawing.Size(100, 20);
            this.TxtLocalPort.TabIndex = 7;
            this.TxtLocalPort.Text = "20002";
            // 
            // label3
            // 
            this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label3.AutoSize = true;
            this.label3.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
            this.label3.Location = new System.Drawing.Point(486, 449);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(81, 13);
            this.label3.TabIndex = 6;
            this.label3.Text = "UDP Local Port";
            // 
            // BtnLoadData
            // 
            this.BtnLoadData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.BtnLoadData.Location = new System.Drawing.Point(598, 510);
            this.BtnLoadData.Name = "BtnLoadData";
            this.BtnLoadData.Size = new System.Drawing.Size(75, 23);
            this.BtnLoadData.TabIndex = 8;
            this.BtnLoadData.Text = "Load Data";
            this.BtnLoadData.UseVisualStyleBackColor = true;
            this.BtnLoadData.Click += new System.EventHandler(this.BtnLoadData_Click);
            // 
            // BtnTCPSend
            // 
            this.BtnTCPSend.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.BtnTCPSend.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
            this.BtnTCPSend.Location = new System.Drawing.Point(404, 482);
            this.BtnTCPSend.Name = "BtnTCPSend";
            this.BtnTCPSend.Size = new System.Drawing.Size(75, 23);
            this.BtnTCPSend.TabIndex = 9;
            this.BtnTCPSend.Text = "TCP Send";
            this.BtnTCPSend.UseVisualStyleBackColor = true;
            this.BtnTCPSend.Click += new System.EventHandler(this.BtnTCPSend_Click);
            // 
            // TxtPathFileToLoad
            // 
            this.TxtPathFileToLoad.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.TxtPathFileToLoad.Location = new System.Drawing.Point(91, 517);
            this.TxtPathFileToLoad.Name = "TxtPathFileToLoad";
            this.TxtPathFileToLoad.Size = new System.Drawing.Size(335, 20);
            this.TxtPathFileToLoad.TabIndex = 10;
            // 
            // ChkLoopInputSentences
            // 
            this.ChkLoopInputSentences.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.ChkLoopInputSentences.AutoSize = true;
            this.ChkLoopInputSentences.Location = new System.Drawing.Point(91, 486);
            this.ChkLoopInputSentences.Name = "ChkLoopInputSentences";
            this.ChkLoopInputSentences.Size = new System.Drawing.Size(137, 17);
            this.ChkLoopInputSentences.TabIndex = 11;
            this.ChkLoopInputSentences.Text = "Loop inserted sentence";
            this.ChkLoopInputSentences.UseVisualStyleBackColor = true;
            this.ChkLoopInputSentences.CheckedChanged += new System.EventHandler(this.ChkLoopInputSentences_CheckedChanged);
            // 
            // BtnStopSending
            // 
            this.BtnStopSending.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.BtnStopSending.Enabled = false;
            this.BtnStopSending.Location = new System.Drawing.Point(269, 482);
            this.BtnStopSending.Name = "BtnStopSending";
            this.BtnStopSending.Size = new System.Drawing.Size(75, 23);
            this.BtnStopSending.TabIndex = 12;
            this.BtnStopSending.Text = "Stop Send";
            this.BtnStopSending.UseVisualStyleBackColor = true;
            this.BtnStopSending.Click += new System.EventHandler(this.BtnStopSending_Click);
            // 
            // label4
            // 
            this.label4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(7, 520);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(80, 13);
            this.label4.TabIndex = 13;
            this.label4.Text = "Path file to load";
            // 
            // tabControl1
            // 
            this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.tabControl1.Controls.Add(this.TbUDPTCPTalker);
            this.tabControl1.Controls.Add(this.TbUDPListner);
            this.tabControl1.Location = new System.Drawing.Point(3, 1);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(687, 569);
            this.tabControl1.TabIndex = 14;
            // 
            // TbUDPTCPTalker
            // 
            this.TbUDPTCPTalker.Controls.Add(this.TxtStringToMessageToSend);
            this.TbUDPTCPTalker.Controls.Add(this.label4);
            this.TbUDPTCPTalker.Controls.Add(this.BtnLoadData);
            this.TbUDPTCPTalker.Controls.Add(this.BtnStopSending);
            this.TbUDPTCPTalker.Controls.Add(this.BtnUPDSend);
            this.TbUDPTCPTalker.Controls.Add(this.ChkLoopInputSentences);
            this.TbUDPTCPTalker.Controls.Add(this.label1);
            this.TbUDPTCPTalker.Controls.Add(this.TxtPathFileToLoad);
            this.TbUDPTCPTalker.Controls.Add(this.TxtPortServer);
            this.TbUDPTCPTalker.Controls.Add(this.BtnTCPSend);
            this.TbUDPTCPTalker.Controls.Add(this.label2);
            this.TbUDPTCPTalker.Controls.Add(this.TxtIPServerAddress);
            this.TbUDPTCPTalker.Controls.Add(this.TxtLocalPort);
            this.TbUDPTCPTalker.Controls.Add(this.label3);
            this.TbUDPTCPTalker.Location = new System.Drawing.Point(4, 22);
            this.TbUDPTCPTalker.Name = "TbUDPTCPTalker";
            this.TbUDPTCPTalker.Padding = new System.Windows.Forms.Padding(3);
            this.TbUDPTCPTalker.Size = new System.Drawing.Size(679, 543);
            this.TbUDPTCPTalker.TabIndex = 0;
            this.TbUDPTCPTalker.Text = "UDP-TCP Talker";
            this.TbUDPTCPTalker.UseVisualStyleBackColor = true;
            // 
            // TbUDPListner
            // 
            this.TbUDPListner.Controls.Add(this.TxtUDPListnerLog);
            this.TbUDPListner.Controls.Add(this.BtnKillAllThreads);
            this.TbUDPListner.Controls.Add(this.label5);
            this.TbUDPListner.Controls.Add(this.TxtUDPListnerPort);
            this.TbUDPListner.Controls.Add(this.label6);
            this.TbUDPListner.Controls.Add(this.TxtUDPListnerAddress);
            this.TbUDPListner.Controls.Add(this.TxtUDPListnerLocalPort);
            this.TbUDPListner.Controls.Add(this.label7);
            this.TbUDPListner.Controls.Add(this.BtnRestartListner);
            this.TbUDPListner.Controls.Add(this.TxtReceivedUDPMessages);
            this.TbUDPListner.Location = new System.Drawing.Point(4, 22);
            this.TbUDPListner.Name = "TbUDPListner";
            this.TbUDPListner.Padding = new System.Windows.Forms.Padding(3);
            this.TbUDPListner.Size = new System.Drawing.Size(679, 543);
            this.TbUDPListner.TabIndex = 1;
            this.TbUDPListner.Text = "UDP Listner";
            this.TbUDPListner.UseVisualStyleBackColor = true;
            // 
            // TxtReceivedUDPMessages
            // 
            this.TxtReceivedUDPMessages.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.TxtReceivedUDPMessages.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
            this.TxtReceivedUDPMessages.Location = new System.Drawing.Point(6, 58);
            this.TxtReceivedUDPMessages.Multiline = true;
            this.TxtReceivedUDPMessages.Name = "TxtReceivedUDPMessages";
            this.TxtReceivedUDPMessages.Size = new System.Drawing.Size(667, 413);
            this.TxtReceivedUDPMessages.TabIndex = 0;
            // 
            // SSMain
            // 
            this.SSMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.SSNrIterationLabel,
            this.SSNrIteration,
            this.SSStatus,
            this.toolStripStatusLabel1,
            this.SSListnerStatusLabel,
            this.SSListnerStatus,
            this.SSNrReceivedMsgLabel,
            this.SSNrReceivedMsg});
            this.SSMain.Location = new System.Drawing.Point(0, 581);
            this.SSMain.Name = "SSMain";
            this.SSMain.Size = new System.Drawing.Size(693, 22);
            this.SSMain.TabIndex = 15;
            this.SSMain.Text = "Stato elaborazioni";
            // 
            // SSNrIterationLabel
            // 
            this.SSNrIterationLabel.Name = "SSNrIterationLabel";
            this.SSNrIterationLabel.Size = new System.Drawing.Size(59, 17);
            this.SSNrIterationLabel.Text = "Iterations:";
            // 
            // SSNrIteration
            // 
            this.SSNrIteration.Name = "SSNrIteration";
            this.SSNrIteration.Size = new System.Drawing.Size(13, 17);
            this.SSNrIteration.Text = "0";
            // 
            // SSStatus
            // 
            this.SSStatus.Name = "SSStatus";
            this.SSStatus.Size = new System.Drawing.Size(77, 17);
            this.SSStatus.Text = "Never started";
            // 
            // SSListnerStatusLabel
            // 
            this.SSListnerStatusLabel.Name = "SSListnerStatusLabel";
            this.SSListnerStatusLabel.Size = new System.Drawing.Size(79, 17);
            this.SSListnerStatusLabel.Text = "Listner status:";
            // 
            // SSListnerStatus
            // 
            this.SSListnerStatus.Name = "SSListnerStatus";
            this.SSListnerStatus.Size = new System.Drawing.Size(57, 17);
            this.SSListnerStatus.Text = "No active";
            // 
            // SSNrReceivedMsgLabel
            // 
            this.SSNrReceivedMsgLabel.Name = "SSNrReceivedMsgLabel";
            this.SSNrReceivedMsgLabel.Size = new System.Drawing.Size(100, 17);
            this.SSNrReceivedMsgLabel.Text = "UDP msg Listned:";
            // 
            // SSNrReceivedMsg
            // 
            this.SSNrReceivedMsg.Name = "SSNrReceivedMsg";
            this.SSNrReceivedMsg.Size = new System.Drawing.Size(13, 17);
            this.SSNrReceivedMsg.Text = "0";
            // 
            // BtnRestartListner
            // 
            this.BtnRestartListner.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.BtnRestartListner.Location = new System.Drawing.Point(598, 20);
            this.BtnRestartListner.Name = "BtnRestartListner";
            this.BtnRestartListner.Size = new System.Drawing.Size(75, 23);
            this.BtnRestartListner.TabIndex = 2;
            this.BtnRestartListner.Text = "Restart";
            this.BtnRestartListner.UseVisualStyleBackColor = true;
            this.BtnRestartListner.Click += new System.EventHandler(this.BtnRestartListner_Click);
            // 
            // label5
            // 
            this.label5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label5.AutoSize = true;
            this.label5.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
            this.label5.Location = new System.Drawing.Point(200, 9);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(26, 13);
            this.label5.TabIndex = 8;
            this.label5.Text = "Port";
            // 
            // TxtUDPListnerPort
            // 
            this.TxtUDPListnerPort.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.TxtUDPListnerPort.Location = new System.Drawing.Point(232, 6);
            this.TxtUDPListnerPort.Name = "TxtUDPListnerPort";
            this.TxtUDPListnerPort.Size = new System.Drawing.Size(100, 20);
            this.TxtUDPListnerPort.TabIndex = 9;
            this.TxtUDPListnerPort.Text = "20000";
            // 
            // label6
            // 
            this.label6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(7, 9);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(45, 13);
            this.label6.TabIndex = 10;
            this.label6.Text = "Address";
            // 
            // TxtUDPListnerAddress
            // 
            this.TxtUDPListnerAddress.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.TxtUDPListnerAddress.Location = new System.Drawing.Point(58, 6);
            this.TxtUDPListnerAddress.Name = "TxtUDPListnerAddress";
            this.TxtUDPListnerAddress.Size = new System.Drawing.Size(137, 20);
            this.TxtUDPListnerAddress.TabIndex = 11;
            this.TxtUDPListnerAddress.Text = "10.150.21.179";
            // 
            // TxtUDPListnerLocalPort
            // 
            this.TxtUDPListnerLocalPort.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.TxtUDPListnerLocalPort.Location = new System.Drawing.Point(95, 32);
            this.TxtUDPListnerLocalPort.Name = "TxtUDPListnerLocalPort";
            this.TxtUDPListnerLocalPort.Size = new System.Drawing.Size(100, 20);
            this.TxtUDPListnerLocalPort.TabIndex = 13;
            this.TxtUDPListnerLocalPort.Text = "20002";
            // 
            // label7
            // 
            this.label7.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label7.AutoSize = true;
            this.label7.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
            this.label7.Location = new System.Drawing.Point(8, 35);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(81, 13);
            this.label7.TabIndex = 12;
            this.label7.Text = "UDP Local Port";
            // 
            // BtnKillAllThreads
            // 
            this.BtnKillAllThreads.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
            this.BtnKillAllThreads.Location = new System.Drawing.Point(418, 20);
            this.BtnKillAllThreads.Name = "BtnKillAllThreads";
            this.BtnKillAllThreads.Size = new System.Drawing.Size(87, 23);
            this.BtnKillAllThreads.TabIndex = 14;
            this.BtnKillAllThreads.Text = "Abort Threads";
            this.BtnKillAllThreads.UseVisualStyleBackColor = true;
            this.BtnKillAllThreads.Click += new System.EventHandler(this.BtnKillAllThreads_Click);
            // 
            // TxtUDPListnerLog
            // 
            this.TxtUDPListnerLog.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.TxtUDPListnerLog.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
            this.TxtUDPListnerLog.Location = new System.Drawing.Point(6, 477);
            this.TxtUDPListnerLog.Multiline = true;
            this.TxtUDPListnerLog.Name = "TxtUDPListnerLog";
            this.TxtUDPListnerLog.Size = new System.Drawing.Size(667, 66);
            this.TxtUDPListnerLog.TabIndex = 15;
            // 
            // toolStripStatusLabel1
            // 
            this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
            this.toolStripStatusLabel1.Size = new System.Drawing.Size(16, 17);
            this.toolStripStatusLabel1.Text = " | ";
            // 
            // FrmMain
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(693, 603);
            this.Controls.Add(this.SSMain);
            this.Controls.Add(this.tabControl1);
            this.Name = "FrmMain";
            this.Text = "UDP / TCP Tester";
            this.tabControl1.ResumeLayout(false);
            this.TbUDPTCPTalker.ResumeLayout(false);
            this.TbUDPTCPTalker.PerformLayout();
            this.TbUDPListner.ResumeLayout(false);
            this.TbUDPListner.PerformLayout();
            this.SSMain.ResumeLayout(false);
            this.SSMain.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
        #endregion
    }
}



C# | Multithrearing | Multithrering Windows Forms | Multithrearing Windows Forms Soluzioni


Visual Studio


Parole chiave:delegate, Thread, UdpClient, TcpClient, Application.DoEvents, DoEvents, Sleep, Thread, Encoding, ASCIIEncoding, Streaming, NetworkStream, BitConverter, ASCII, Flush, Close, IPEndPoint, IPAddress, MessageBox.Show, MessageBox, Show, kill, Abort, IsAlive, GetBytes

Author