此流不支持搜索c#

时间:2017-08-24 09:23:22

标签: c# networkstream

我对NetworkStream很新,我无法弄清楚为什么会出现以下错误:

  • length - 此流不支持搜索
  • 位置 - 此流不支持搜索

我已经阅读了一些文章和文档,但是,这有点模糊。

我目前的代码是:

        public static string ReturnedMessage;

        public static void Connect(String server, String message, Int32 port)
        {
            try
            {
                TcpClient client = new TcpClient(server, port);

                Byte[] messageData = System.Text.Encoding.ASCII.GetBytes(message);

                NetworkStream stream = client.GetStream();             

                stream.Write(messageData, 0, messageData.Length);

                // MessageBox.Show("Message Sent");

                messageData = new Byte[1024];

                String responseData = String.Empty;

                Int32 bytes = stream.Read(messageData, 0, messageData.Length);
                responseData = System.Text.Encoding.ASCII.GetString(messageData, 0, bytes);
                ReturnedMessage = "Received: " + responseData;

                stream.Close();
                client.Close();
            }
            catch (ArgumentNullException e)
            {
                ReturnedMessage = "ArgumentNullException: {0}" + e.ToString();
            }
            catch (SocketException e)
            {
                ReturnedMessage = "SocketException: {0}" + e.ToString();
            }
        }

我看过一些关于MemoryStream的帖子,但这似乎主要用于HTTP。任何帮助,将不胜感激。

消息的堆栈如下:

-       Length  'stream.Length' threw an exception of type 'System.NotSupportedException'   long {System.NotSupportedException}

'stream.Length' threw an exception of type 'System.NotSupportedException'
    "at System.Net.Sockets.NetworkStream.get_Length()"

0 个答案:

没有答案