当在C#中收到某个字符时如何运行代码

发布于 2025-01-22 09:20:06 字数 675 浏览 2 评论 0原文

因此,我有AC#程序,当服务器从服务器收到某个字符时,我试图在客户端中运行某个代码。应该发生的事情,如果服务器将G发送到客户端,则应该运行某些代码,然后客户端和服务器确实连接,并且他们确实确认G已发送并接收了G,但IF中的代码不运行。这是客户端代码,在接收G时,应该在客户端上运行代码: 字节[] 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();

            }

从服务器收到G,无问题,但代码不运行。我想知道为什么这可能是?谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后彩虹 2025-01-29 09:20:06

您可以将消息框更改为debug.writeline还是console.writeline并显示输出。

因此,您提供了完整的回调,因为缺少某些代码

,但是

string atk = Encoding.ASCII.GetString(bb);

如果您知道 ascii中的字母“ g”:是“ 103 <)。 /strong>“ hex: 67
直接进行比较而不将其转换为字符串

if(bb == 103)
{
    MessageBox.Show("information received.);
    search.RunWorkerAsync();
}

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文