在线聊天室C#错误

时间:2018-01-18 00:22:40

标签: c# server client

所以我在C#中建立了一个在线聊天室,我在第71行收到了这个错误。

  

System.ArgumentOutOfRangeException:'指定的参数超出了有效值的范围。'

我有一个客户端程序和一个服务器程序。这是客户端程序的代码。给我错误的行是星号。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net.Sockets;
    using System.Threading;

    namespace ChatProgram
    {
    public partial class Form1 : Form
    {
    System.Net.Sockets.TcpClient clientSocket = new 
    System.Net.Sockets.TcpClient();
    NetworkStream serverStream = default(NetworkStream);
    string readData = null;

    static int maxUsage = 0;
    string noName = "Anonymous";
    int tick = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void enterButton_Click(object sender, EventArgs e)
    {
        byte[] outStream = System.Text.Encoding.ASCII.GetBytes(readRichTxt.Text + "$");
        serverStream.Write(outStream, 0, outStream.Length);
        serverStream.Flush();

        readData = "Connected to server...";
        msg();
        clientSocket.Connect("127.0.0.1", 8888);
        serverStream = clientSocket.GetStream();

        Thread ctThread = new Thread(getMessage);
        ctThread.Start();

        string nameTxt = nameTextBox.Text;
        if (nameTxt == String.Empty)
        {
            nameTxt = noName;
            tick = 1;
        }
        else
        {
            tick = 0;
        }
        readRichTxt.SelectionColor = Color.Red;
        readRichTxt.AppendText("\n" + nameTxt + " has joined." + "\n");
        ++maxUsage;
        sendButton.Enabled = true;

        if (maxUsage == 3)
        {
            enterButton.Enabled = false;
        }
    }

    private void getMessage()
    {
        while(true)
        {
            serverStream = clientSocket.GetStream();
            int buffSize = 0;
            byte[] inStream = new byte[10025];
            buffSize = clientSocket.ReceiveBufferSize;
            ***serverStream.Read(inStream, 0, buffSize);***
            string returndata = System.Text.Encoding.ASCII.GetString(inStream);
            readData = "" + returndata;
            msg();
        }
    }

    private void msg()
    {
        if (this.InvokeRequired)
            this.Invoke(new MethodInvoker(msg));
        else
            readRichTxt.Text = readRichTxt.Text + Environment.NewLine + " >> " + readData;
    }

    private void sendButton_Click(object sender, EventArgs e)
    {
        string nameTxt = nameTextBox.Text;
        string userTyped = userTypeBox.Text;

        if (userTyped == String.Empty)
        {
            return;
        }
        if (tick == 1)
        {
            readRichTxt.AppendText(noName + ": " + userTyped);
        }
        else
        {
            readRichTxt.AppendText(nameTxt + ": " + userTyped);
        }
        userTypeBox.Clear();
    }
}
}

请帮助,我完全被这一点困扰。

1 个答案:

答案 0 :(得分:0)

大小是多少:

buffSize = clientSocket.ReceiveBufferSize;

这可能比你在这里声明的要大:

byte[] inStream = new byte[10025];

因此错误大致告诉你:

buffSize > inStream.Length

导致超出界限,如错误。