2
I have a connection socket that works well with TCP/UDP, the problem is that use Tcp1.Connect
and it bars more than one connection. In an internet search I verified that the right one would use the Tcp1.BeginConnect
which makes it possible to use more than one thread.
In my scenario I have foreach
running along the lines of Grid catching the ip and equipment door. My doubt is how to mount several threads, or how to use the asynchronous connection within this loop.
Someone would have some example?
The code I’m using to send message to the equipment:
TcpClient tcp1 = new TcpClient();
UdpClient udp1 = new UdpClient();
#region Menssagem Rápida
private void cmd_msg_Click(object sender, EventArgs e)
{
string command = "";
string preCommand = "";
byte chkSum = 0;
Random rnd = new Random();
chaveAes[0] = Convert.ToByte(rnd.Next(1, 256));
chaveAes[1] = Convert.ToByte(rnd.Next(1, 256));
chaveAes[2] = Convert.ToByte(rnd.Next(1, 256));
chaveAes[3] = Convert.ToByte(rnd.Next(1, 256));
chaveAes[4] = Convert.ToByte(rnd.Next(1, 256));
chaveAes[5] = Convert.ToByte(rnd.Next(1, 256));
chaveAes[6] = Convert.ToByte(rnd.Next(1, 256));
chaveAes[7] = Convert.ToByte(rnd.Next(1, 256));
chaveAes[8] = Convert.ToByte(rnd.Next(1, 256));
chaveAes[9] = Convert.ToByte(rnd.Next(1, 256));
chaveAes[10] = Convert.ToByte(rnd.Next(1, 256));
chaveAes[11] = Convert.ToByte(rnd.Next(1, 256));
chaveAes[12] = Convert.ToByte(rnd.Next(1, 256));
chaveAes[13] = Convert.ToByte(rnd.Next(1, 256));
chaveAes[14] = Convert.ToByte(rnd.Next(1, 256));
chaveAes[15] = Convert.ToByte(rnd.Next(1, 256));
string comandoCatraca = "01+REON+00+20]5]" + mensagem_rapida + "]";
command = "";
command = command + (char)(2);
preCommand = preCommand + (char)((comandoCatraca).ToString().Length);
preCommand = preCommand + (char)(0);
preCommand = preCommand + comandoCatraca;
chkSum = calcCheckSumString(preCommand);
command = command + preCommand;
command = command + Convert.ToChar(chkSum);
command = command + (char)(3);
byte[] array = Encoding.ASCII.GetBytes(command);
tcp1.Client.Send(array);
}
#endregion
Does anyone know how I can get the data from the equipment in this method down past our friend Rick.
Thanks in advance.
Here you find a good example.
– lsalamon