NetworkStream.Read()不起作用,并引发ArgumentOutOfRangeException [英] NetworkStream.Read() doesn't work and throws ArgumentOutOfRangeException

查看:73
本文介绍了NetworkStream.Read()不起作用,并引发ArgumentOutOfRangeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需尝试创建聊天服务器-客户端

Just trying to create a chat server-client


  1. 这是等待客户端连接的服务器(可选)*

  1. This is the server waiting for a client connection (optional)*

    TcpListener serverSocket = new TcpListener(8888);
    int requestCount = 0;
    TcpClient clientSocket = default(TcpClient);
    serverSocket.Start();
    Console.WriteLine(" >> Server Started");
    clientSocket = serverSocket.AcceptTcpClient();
    Console.WriteLine(" >> Accept connection from client");


  • 然后,客户端连接到服务器(可选)*

  • Then, the client connects to the server (optional)*

        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        clientSocket.Connect("127.0.0.1", 8888);
    


  • 然后,在客户端,我从Windows表单发送消息,然后按钮单击事件会执行以下操作:

  • Then, on the client side, I send the message from a windows form, and the button click event does this:

    NetworkStream serverStream = clientSocket.GetStream();
    byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox2.Text + "$");
    serverStream.Write(outStream, 0, outStream.Length);
    serverStream.Flush();
    //this goes to the server  ------> to the part (4)
    
    //returning from the server <------
    byte[] inStream = new byte[10025];
    serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
    string returndata = System.Text.Encoding.ASCII.GetString(inStream);
    textBox2.Text = "";
    textBox2.Focus();
    


  • 最后,在服务器端,可以无限扩展客户端请求。这是我在networkStream.Read()

  • Finally, on the server side, an infinite bucle for client requests. And here is where I got the problem on the networkStream.Read()

    while ((true))
        {
            try
            {
                requestCount = requestCount + 1;
                NetworkStream networkStream = clientSocket.GetStream();
                byte[] bytesFrom = new byte[10025];
                if (networkStream.DataAvailable)
                {
                    **networkStream.Read(bytesFrom, 0, (int) clientSocket.ReceiveBufferSize);**
                    string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                    Console.WriteLine(" >> Data from client - " + dataFromClient);
                    string serverResponse = "Last Message from client" + dataFromClient;
                    Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
                    networkStream.Write(sendBytes, 0, sendBytes.Length);
                    networkStream.Flush();
                    Console.WriteLine(" >> " + serverResponse);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    


  • 我一步一步地调试了(在服务器和客户端上),直到我进入networkStream.Read()方法并抛出ArgumentOutOfRangeException ...,一切都还好。请任何人帮助我或告诉我错误在哪里。

    I debugged step by step (on server and client), and it's all fine until I get to the networkStream.Read() method, and throws ArgumentOutOfRangeException... Please, anybody help me or tell me where the error is.

    *我之所以说是可选的,是因为1.和2.步骤很好(至少,我认为)

    *I say optional because the 1. and 2. steps are fine (at least, I think)

    推荐答案

    来自 MSDN ,ArgumentOutOfRangeException表示 offset count 为负(第二个和第三个参数),或者 size(count) 参数大于缓冲区长度减去 offset参数的值。

    From the MSDN, ArgumentOutOfRangeException means that offset or count is negative(2nd and 3rd parameters), OR the "size(count)" parameter is greater than the length of buffer minus the value of the "offset" parameter.

    我尝试传递 bytesFrom.Length 而不是 clientSocket.ReceiveBufferSize

    这篇关于NetworkStream.Read()不起作用,并引发ArgumentOutOfRangeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆