C# System.InvalidOperationException:&x27;不允许在未连接的插座上进行操作;

C# System.InvalidOperationException:&x27;不允许在未连接的插座上进行操作;,c#,C#,这是我的表格 这是我的密码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.

这是我的表格

这是我的密码

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

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

    private void Form1_Load(object sender, EventArgs e)
    {
        msg("Client Started");
        clientSocket.Connect("192.168.0.167", 5000);
        label1.Text = "Client Socket Program - Server Connected ...";
    }



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

        byte[] inStream = new byte[10025];
        serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
        string returndata = System.Text.Encoding.ASCII.GetString(inStream);
        msg(returndata);
        textBox2.Text = "";
        textBox2.Focus();

    }


    public void msg(string mesg)
    {
        textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
    }


}
}

当我点击代码中的开始按钮时,我在连接到服务器时遇到了这个错误。请指导我如何摆脱此错误并改为连接到服务器


form\u load中的异常不显示是正常的-因此很明显它没有连接。我应该如何删除此错误。1.我不认为它连接了。2.将连接移动到以后,并检查它是否工作。它没有工作。NetworkStream serverStream=clientSocket.GetStream()//这就是我得到这个错误的地方。