'How to run code when a certain character is recieved in c#
so I have a c# program in which I am trying to run a certain code in a client when a certain character is recieved from the server. What is supposed to happen, If the server sends g to the client it's supposed to run certain code, and while then client and server do connect and they do acknowledge g was sent and received, the code in the if then statement does not run. Here is the client code where when receiving g it is supposed to run code on the client: byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
for (int i = 0; i < k; i++)
Console.Write(Convert.ToString(bb));
string atk = Encoding.ASCII.GetString(bb);
MessageBox.Show("Recieved Command " + atk);
if (atk == "g")
{
MessageBox.Show("information received.);
search.RunWorkerAsync();
}
The g is recieved from the server no problem, but the code does not run. I am wondering why this might be? Thanks.
Solution 1:[1]
can you change the message box to Debug.WriteLine or Console.WriteLine and show the output please.
All so your provide your full Callback as some code is missing
But maybe don't convert
string atk = Encoding.ASCII.GetString(bb);
if you know the letter "g" in ASCII: is "103" in HEX: 67 do a direct compare without convert it to string
if(bb == 103)
{
MessageBox.Show("information received.);
search.RunWorkerAsync();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kamil |
