简体   繁体   English

为什么从我的代码中删除TextBox.AppendText()会更改此处的套接字通信?

[英]Why removig TextBox.AppendText() from my code changes socket communication here?

I have a simple client/server communication between C++ and C# where the C# program sends a string to the C++. 我在C ++和C#之间进行了简单的客户端/服务器通信,其中C#程序将字符串发送到C ++。 Sending of a string is done on 3 stages. 字符串的发送分三个阶段进行。 Send the length of the length of the string--> Send the length of string --> Send the String. 发送字符串长度的长度->发送字符串长度->发送字符串。 For debugging purposes I have a TextBox called textbox1 on my C# program to print the sent values using textbox1.AppendText() three times for the three values sent. 出于调试目的,我在C#程序上有一个名为textbox1的TextBox,用于对发送的三个值使用textbox1.AppendText()三次打印发送的值。 Everything was sent and received correctly, When I remove two of the three AppendText() lines from my code it still works but the strange thing is when I remove the third one (Commented as //<--This Line , The C++ server receives 0! 一切都已正确发送和接收,当我从代码中删除三个AppendText()行中的两个时,它仍然有效,但是奇怪的是,当我删除了第三行时(注释为//<--This Line -This //<--This Line ,C ++服务器接收到0!

C# Client (Code Snippet): C#客户端(代码段):

 private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MemoryStream ms;
                NetworkStream ns;
                TcpClient client;
                BinaryWriter br;
                byte[] tosend;
                string AndroidId = "2468101214161820";
                string len = AndroidId.Length.ToString();
                string lol = len.Length.ToString();

                ms = new MemoryStream();
                client = new TcpClient("127.0.0.1", 8888);                
                ns = client.GetStream();
                br = new BinaryWriter(ns);
                //****************Send Length Of Length***************
                tosend = System.Text.Encoding.ASCII.GetBytes(lol);
                br.Write(tosend);
                textBox1.AppendText(Encoding.ASCII.GetString(tosend));//<---THIS LINE
                //****************Send Length***************
                tosend = System.Text.Encoding.ASCII.GetBytes(len);
                br.Write(tosend);
                //****************Send Length Of Length***************
                tosend = System.Text.Encoding.ASCII.GetBytes(AndroidId);
                br.Write(tosend);

                ns.Close();
                client.Close();
            }

C++ Server Code Snippet: C ++服务器代码段:

//***********Recieve Length Of Length*****************
                char* lol_buff0 = new char[1];
                int nullpoint=  recv(s, lol_buff0, strlen(lol_buff0), 0);
                lol_buff0[nullpoint] = '\0';
                int lengthoflength = atoi(lol_buff0);
                //***********Recieve Length*****************
                char* l_buff0 = new char[lengthoflength];
                int nullpoint2=recv(s, l_buff0, strlen(l_buff0), 0);
                l_buff0[nullpoint2] = '\0';
                int length = atoi(l_buff0);
                //***********Recieve AndroidID*****************
                char* AndroidID = new char[length];
                valread0 = recv(s, AndroidID, strlen(AndroidID), 0);

                if (valread0 == SOCKET_ERROR)
                {
                    int error_code = WSAGetLastError();
                    if (error_code == WSAECONNRESET)
                    {
                        //Somebody disconnected , get his details and print
                        printf("Host disconnected unexpectedly , ip %s , port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));

                        //Close the socket and mark as 0 in list for reuse
                        closesocket(s);
                        client_socket[i] = 0;
                    }
                    else
                    {
                        printf("recv failed with error code : %d", error_code);
                    }
                }
                if (valread0 == 0)
                {
                    //Somebody disconnected , get his details and print
                    printf("Host disconnected , ip %s , port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));

                    //Close the socket and mark as 0 in list for reuse
                    closesocket(s);
                    client_socket[i] = 0;
                }
                else
                {
                    //add null character, if you want to use with printf/puts or other string handling functions
                    AndroidID[valread0] = '\0';
                    printf("%s:%d  Your Android ID is - %s \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port), AndroidID);
                }

I know I can accommodate the TextBox as long as it works but It is so weird and I'd like to know what is the explanation for that. 我知道只要它可以工作,我就可以容纳它,但是它太奇怪了,我想知道对此的解释是什么。 Thanks. 谢谢。

You're assuming that the data will be received in one recv call (or alternatively, that one send corresponds to one receive). 您假设将在一个recv调用中接收到数据(或者,一个发送对应一个接收)。 That is a false assumption. 这是一个错误的假设。 You need to keep reading until you read length of bytes of data. 您需要继续读取,直到读取数据字节的长度 TCP doesn't have any messaging built in, it only deals with streams. TCP没有内置的任何消息传递,它仅处理流。

Adding the line may mean that some small delay is added which makes the receive happen in a single call - it's hard to tell, since you're dealing with something that isn't quite deterministic. 添加线路可能意味着添加了一些小的延迟,这使得接收在单个调用中发生-很难说,因为您要处理的不是确定性的东西。 Handle TCP properly, and see if the problem persists. 正确处理TCP,然后查看问题是否仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TextBox.AppendText()无法自动滚动 - TextBox.AppendText() not autoscrolling 使用\\ n作为行终止符时,为什么c#textbox.AppendText()换行符会消失? - Why do c# textbox.AppendText() newlines disappear when using \n as line terminator? TextBox.Text +=“字符串”; vs TextBox.AppendText(“字符串”); - TextBox.Text += “string”; vs TextBox.AppendText(“string”); 跨类共享TextBox.AppendText()方法的最干净方法是什么? - What is the cleanest way to share a TextBox.AppendText() method across classes? 调用 TextBox.AppendText() 后显示自动完成列表 - Show autocomplete list after calling TextBox.AppendText() 试图在textbox.AppendText中退格显示一个奇怪的符号 - Trying to do a backspace in textbox.AppendText shows a weird symbol 从另一个不起作用的线程C#AppendText到TextBox - C# AppendText to TextBox from another thread not working 当我使用textBox1.AppendText时,为什么在文本框中不出现换行符 - Why don't new line characters appear in the textbox when I use textBox1.AppendText TextBox和.AppendText()中带有行的奇怪行为-C# - Odd Behaviour with Lines in TextBox and .AppendText() - C# 有没有比AppendText更快的方法来打印到TextBox? - Is there a faster way to print to a TextBox than AppendText?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM