Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# I';我正在尝试关闭udp连接_C# - Fatal编程技术网

C# I';我正在尝试关闭udp连接

C# I';我正在尝试关闭udp连接,c#,C#,我在Visual Studio 2019中遇到了一个问题,正在编程服务器-客户端连接。 当我调用关闭连接时,程序抛出一个break null异常。我尝试了不同的方法来注销UDP连接,但没有一种有效 以下是我的代码,它会让客户端出错: this.clientSocket.SendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer); 当程序在此之后退出时,我的服务器在重新连接时抛出套接字异常 客户无效 public vo

我在Visual Studio 2019中遇到了一个问题,正在编程服务器-客户端连接。 当我调用关闭连接时,程序抛出一个break null异常。我尝试了不同的方法来注销UDP连接,但没有一种有效

以下是我的代码,它会让客户端出错:

this.clientSocket.SendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer);
当程序在此之后退出时,我的服务器在重新连接时抛出套接字异常

客户无效

    public void Client_Closing()
    {
        Console.WriteLine("CLOSING SOCKET");

        try
        {
            Console.WriteLine("CLOSING SOCKET");
             
            // Initialise a packet object to store the data to be sent
            Packet sendData = new Packet();
            sendData.ChatDataIdentifier = DataIdentifier.LogOut;
            sendData.ChatName = this.name;
            sendData.ChatMessage = null;
                
            // Get packet as byte array
            byte[] byteData = sendData.GetDataStream();
               
            // Send packet to the server
            this.clientSocket.SendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer);   
              
            // Close the socket
            this.clientSocket.Close();
        }
        catch (Exception ex)
        {
            // MessageBox.Show("Closing Error: " + ex.Message, "UDP Client", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Console.WriteLine("Closing Error: " + ex.Message);
        }
    }

我知道了谢谢你的帮助

我从一个事件处理程序调用了我的关闭void,该事件处理程序在客户端关闭时运行。 我把它设置在一个类的不同实例上

我将事件处理程序实例化到套接字所在的类的同一个对象上,它在这里工作,这是固定的代码

                AsynchronousClient AsynchronousClient = new AsynchronousClient();
            AsynchronousClient.StartClient();



            // Some boilerplate to react to close window event, CTRL-C, kill, etc
            //AsynchronousClient h = new AsynchronousClient(); //REMOVED THIS
            _handler += new EventHandler(AsynchronousClient.Handler); //FIXED HERE
            SetConsoleCtrlHandler(_handler, true);

我知道了谢谢你的帮助

我从一个事件处理程序调用了我的关闭void,该事件处理程序在客户端关闭时运行。 我把它设置在一个类的不同实例上

我将事件处理程序实例化到套接字所在的类的同一个对象上,它在这里工作,这是固定的代码

                AsynchronousClient AsynchronousClient = new AsynchronousClient();
            AsynchronousClient.StartClient();



            // Some boilerplate to react to close window event, CTRL-C, kill, etc
            //AsynchronousClient h = new AsynchronousClient(); //REMOVED THIS
            _handler += new EventHandler(AsynchronousClient.Handler); //FIXED HERE
            SetConsoleCtrlHandler(_handler, true);

检查epServer是否有值?检查epServer是否有值?