Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NetworkStream BeginRead只读取一次?_C#_Networkstream - Fatal编程技术网

C# NetworkStream BeginRead只读取一次?

C# NetworkStream BeginRead只读取一次?,c#,networkstream,C#,Networkstream,我有一些非常简单的代码,可以从它所连接的网络流中读取行。在代码示例中,每次读取只有一行,并且不会继续从服务器获取更多信息 怎么了 byte[] readBuffer = new byte[1024]; byte[] tempBuff = new byte[1024]; int tempBuffSize = 0; private void btnConnect_Click(object sender, EventArgs e)

我有一些非常简单的代码,可以从它所连接的网络流中读取行。在代码示例中,每次读取只有一行,并且不会继续从服务器获取更多信息

怎么了

        byte[] readBuffer = new byte[1024];
        byte[] tempBuff = new byte[1024];
        int tempBuffSize = 0;    


private void btnConnect_Click(object sender, EventArgs e)
            {

            TcpClient tcpClient = new TcpClient("192.168.1.151", 5505);
            NetworkStream stream = tcpClient.GetStream();
            stream.BeginRead(readBuffer, 0, 1024, readHandler, tcpClient); 
        }

void readHandler(IAsyncResult result)
        {
            TcpClient tcpClient = (TcpClient)result.AsyncState;
            int dataLen = tcpClient.GetStream().EndRead(result);

            int currStart = 0;
            int currEnd = -1;

            for (int i = 0; i < dataLen; i++)
            {
                if (readBuffer[i] == '\r' && i < (readBuffer.Length - 1) &&
                    readBuffer[i + 1] == '\n')
                {
                    // Set the end of the data 
                    currEnd = i - 1;

                    // If we have left overs from previous runs: 
                    if (tempBuffSize != 0)
                    {

                        byte[] joinedData = new byte[tempBuffSize + (currEnd - currStart + 1)];
                        Array.Copy(tempBuff, 0, joinedData, 0, tempBuffSize);
                        Array.Copy(readBuffer, currStart, joinedData, tempBuffSize, (currEnd - currStart + 1));

                        System.Text.Encoding enc = System.Text.Encoding.ASCII;
                        string myString = enc.GetString(joinedData);
                        System.Diagnostics.Debug.Write(myString);

                        tempBuffSize = 0;
                    }
                    else
                    {
                        System.Text.Encoding enc = System.Text.Encoding.ASCII;
                        string myString = enc.GetString(readBuffer);
                        System.Diagnostics.Debug.Write(myString);

                        // HandleData(readBuffer, currStart, currEnd);
                    }

                    // Set the new start - after our delimiter 
                    currStart = i + 2;
                }

            }

            // See if we still have any leftovers 
            if (currStart < dataLen)
            {
                Array.Copy(readBuffer, currStart, tempBuff, 0, dataLen - currStart);
                tempBuffSize = dataLen - currStart;
            }
        }  
byte[]readBuffer=新字节[1024];
字节[]tempBuff=新字节[1024];
int tempBuffSize=0;
私有void btnConnect_单击(对象发送者,事件参数e)
{
TcpClient TcpClient=新的TcpClient(“192.168.1.151”,5505);
NetworkStream=tcpClient.GetStream();
BeginRead(readBuffer,0,1024,readHandler,tcpClient);
}
void readHandler(IAsyncResult结果)
{
TcpClient TcpClient=(TcpClient)result.AsyncState;
int dataLen=tcpClient.GetStream().EndRead(结果);
int currStart=0;
int currEnd=-1;
对于(int i=0;i
可能您的流对象在超出作用域时,在再次调用readHandler之前被处理掉了。尝试将tcpClient和stream提升到类作用域而不是方法作用域,或者将读取移动到一个单独的线程,该线程在操作完成后退出。

可能是流对象在超出作用域后,在可以再次调用readHandler之前被释放。尝试将tcpClient和stream提升到类作用域而不是方法作用域,或者将读取移动到操作完成后退出的单独线程。

我在为嵌入式设备开发tcp应用程序时遇到过类似问题。在我的例子中,问题是设备在延迟时间内发出数据,因此在其余数据进入控件之前,控件将移动到程序中的下一行,仅从服务器获取初始数据。我通过引入延迟来解决这个问题

就在从服务器读取数据的那一行之后引入了延迟,因此最好在单独的线程上运行

thread.sleep(3000)

这很可能是你的问题。

我在为嵌入式设备开发tcp应用程序时也遇到过类似的问题。在我的例子中,问题是设备在延迟时间内发出数据,因此在其余数据进入控件之前,控件将移动到程序中的下一行,仅从服务器获取初始数据。我通过引入延迟来解决这个问题

就在从服务器读取数据的那一行之后引入了延迟,因此最好在单独的线程上运行

thread.sleep(3000)

这很可能是您的问题。

为什么您希望它首先读取全部信息?我不是专家,但在我看来,同步或异步方法都不能保证读取所有数据(不管这意味着什么,因为只要套接字打开,就会有更多的数据到达)。在EndRead方法中的代码之后,如果需要更多数据,应该再次调用Read或BeginRead。您应该知道,根据与客户端建立的协议,是否需要更多数据。

为什么您希望它首先读取全部信息?我不是专家,但在我看来,同步或异步方法都不能保证读取所有数据(不管这意味着什么,因为只要套接字打开,就会有更多的数据到达)。在EndRead方法中的代码之后,如果需要更多数据,应该再次调用Read或BeginRead。您应该知道,根据与客户端建立的协议,是否需要更多数据。

否,我只是将流和TCP客户端移动到类范围,没有更改:-(否,我只是将流和TCP客户端移动到类范围,没有更改:-(您应该仔细阅读如何从网络获取数据。网络编程的问题在于,您无法保证当服务器发送1000字节时,您将以1000字节的形式接收数据,您可能会以100字节的存储桶的形式接收数据,或者先接收300字节,然后接收600字节,然后再接收100字节。这都是“随机的”。因此,您应该继续从流中读取数据,直到获得所需的所有数据。通常,这是通过再次调用BeginRead来结束readHandler来完成的(从而进行异步排序循环)。上述注释应该是一个答案(除非它是pra)