本文介绍了错误出现“对象引用未设置为对象的实例”在NetworkStrem对象中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用socket编程的Windows窗体应用程序。我在这里很少混淆,当我根据handleClient.cs类中的连接客户端(只是一个简单的类)初始化NetworkStream对象时,它工作得很好但是当我在其他表单类中初始化相同时说IndentMaster.cs它给出了上面的错误全部代码与handleClient类相同。并且连接是从Form.sc类完成的。



我给我的代码供参考,看看并纠正我,谢谢。



IndentMaster.cs



I am working on windows form application with socket programming . I am little confuse here that when i initialize "NetworkStream" object according to connected client in handleClient.cs class(just a simple class) it works well but when i initialize the same in other form class says IndentMaster.cs it gives above error all code is same like handleClient class. and connection is done from Form.sc class.

I giving my code for reference, look out and correct me, thanks.

IndentMaster.cs

TcpClient clientSocket;
string clNo;

  public void startClient(TcpClient inClientSocket, string clineNo)
        {
            this.clientSocket = inClientSocket;
            inClientSocket.NoDelay = true;
            this.clNo = clineNo;
            new Thread(GetData).Start();
        }


        public void GetData()
        {
            NetworkStream networkStream = clientSocket.GetStream();

            for (int p = 0; p >= 0; p++)
            {
                byte[] b = new byte[100];

                int k = networkStream.Read(b, 0, b.Length);

                for (int i = 0; i < k; i++)
                {
                    Convert.ToChar(b[i]);
                }
            }
        }





handleClient.cs





handleClient.cs

TcpClient clientSocket;
       IndentMaster im = new IndentMaster();
       string clNo;

       public void startClient(TcpClient inClientSocket, string clineNo)
       {
           this.clientSocket = inClientSocket;
           inClientSocket.NoDelay = true;
           this.clNo = clineNo;
           new Thread(GetData).Start();
       }


       public void GetData()
       {

           NetworkStream networkStream = clientSocket.GetStream();

           for (int p = 0; p >= 0; p++)
           {
               byte[] b = new byte[100];

               int k = networkStream.Read(b, 0, b.Length);

               for (int i = 0; i < k; i++)
               {
                   Convert.ToChar(b[i]);
               }
            }
         }

推荐答案


这篇关于错误出现“对象引用未设置为对象的实例”在NetworkStrem对象中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 23:41
查看更多