简体   繁体   中英

What is the proper way to close a C# Socket in .NET Core 3.1?

The Issue

I am trying to handle disconnection for my app and whatever approach I have tried so far has failed, I tried to disconnect from server side, I tried to disconnect from client side but receiving issues on both ends, What I am trying to achieve here is to disconnect the app through a QUIT command without having to close it from the close icon as it throws an exception

client.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace MessengerConsole
{
class Client
{
    static string username;
    static int port = 8888;
    static IPAddress clientIP;
    static Socket serverSocket;
    static Thread processThread;
    static bool connected = false;

    static string GetIp()
    {
        Console.WriteLine("Type the server ip:");
        String clientIP = Console.ReadLine();
        Console.WriteLine("Client IP: " + clientIP);
        //return clientIP;
        //temp solution
        return "192.168.0.106";
    }

    static void printSession()
    {
        Console.Clear();
        Console.WriteLine("//=====================================================");
        Console.WriteLine("//                       Session Details               ");
        Console.WriteLine("//                    =====================");
        Console.WriteLine("//                    IP: " + clientIP + "\n//                    Time: " + DateTime.Now);
        Console.WriteLine("//=====================================================");
    }
    static void clientReceiver()
    {
        while (true)
        {
            Thread.Sleep(500);
            byte[] buffer = new byte[300];
            int rece = serverSocket.Receive(buffer, 0, buffer.Length, 0);
            Array.Resize(ref buffer, rece);
            if (connected == false)
            {
                Console.WriteLine("[" + DateTime.Now.ToString() + "] " + Encoding.Default.GetString(buffer) + " Connected!");
                connected = true;
            }

            else
            {

                if (Encoding.Default.GetString(buffer) == "QUIT")
                {
                    //Quit
                    Console.WriteLine("Server Shutdown");
                    serverSocket.Shutdown(SocketShutdown.Both);
                    serverSocket.Close();
                   
                }
                else
                {
                    Console.WriteLine("[" + DateTime.Now.ToString() + "] " + Encoding.Default.GetString(buffer));
                }

            }
        }
    }



    public static void StartClient()
    {
        processThread = new Thread(clientReceiver);
        Console.WriteLine("Please enter your name");
        username = Console.ReadLine();
        clientIP = IPAddress.Parse(GetIp());  //Returns IP from GetIP()
        Console.WriteLine("Please enter HostPort");
        //string portString = Console.ReadLine();
        //temp port
        string portString = "80";
        try
        {
            port = Convert.ToInt32(portString);
        }
        catch
        {
            port = 8888;
        }
        try
        {

            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Connect(new IPEndPoint(clientIP, port));
            processThread.Start();
            byte[] name = Encoding.Default.GetBytes(username);
            
            //Send Name
            serverSocket.Send(name);
           // byte[] data = Encoding.Default.GetBytes("<" + username + "> Connected");
            //serverSocket.Send(data, 0, data.Length, 0);
            printSession();
            while (serverSocket.Connected)
            {
                //byte[] sdata = Encoding.Default.GetBytes("<" + username + ">" + Console.ReadLine());
                byte[] sdata = Encoding.Default.GetBytes(Console.ReadLine());
                if(Encoding.Default.GetString(sdata) == "QUIT")
                {
                    serverSocket.Send(sdata, sdata.Length, 0);
                    serverSocket.Shutdown(SocketShutdown.Both);
                    serverSocket.Disconnect(true);
                    serverSocket.Close();
                }

                else
                {
                    serverSocket.Send(sdata, 0, sdata.Length, 0);
                }

            }
        }
        catch (Exception e)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(e.Message);
            Console.ForegroundColor = ConsoleColor.White;
        }

    }
}
}

server.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace MessengerConsole
{
class Server
{

    //Server Socket
    static Socket serverSocket;
    //Client Socket
    static Socket clientSocket;

    //Other Variables
    static int port = 8888;
    static IPAddress serverIP;
    static Thread processThread;
    static string username;
    static bool connected = false;
    //Function returns IP Address
    static string GetIp()
    {
        //Computer Name
        string hostname = Dns.GetHostName();

        /*The IPHostEntry class associates a Domain Name System (DNS) host name with an array of aliases and 
         * an array of matching IP addresses.
         */
        IPHostEntry ipentry = Dns.GetHostEntry(hostname);

        //The Address
        IPAddress[] ipAddress = ipentry.AddressList;

        return ipAddress[ipAddress.Length - 1].ToString();
    }

    static void printSession()
    {
        Console.Clear();
        Console.WriteLine("//=====================================================");
        Console.WriteLine("//                       Session Details               ");
        Console.WriteLine("//                    =====================");
        Console.WriteLine("//                    You are hosting the server");
        Console.WriteLine("//                    Time: " + DateTime.Now);
        Console.WriteLine("//=====================================================");
    }
    //Receive
    static void serverReciever()
    {
        while (true)
        {
            Thread.Sleep(500);
            byte[] buffer = new byte[300];
            int rece = clientSocket.Receive(buffer, 0, buffer.Length, 0);
            Array.Resize(ref buffer, rece);

            if (connected == false)
            {
                Console.WriteLine("[" + DateTime.Now.ToString() + "] " + Encoding.Default.GetString(buffer) + " Connected!");
                connected = true;
            }

            else
            {
                if(Encoding.Default.GetString(buffer) == "QUIT")
                {
                    //Quit
                    Console.WriteLine("Client disconnected from the chat");
                    clientSocket.Shutdown(SocketShutdown.Both);
                    clientSocket.Close();
                }
                else
                {
                    Console.WriteLine("[" + DateTime.Now.ToString() + "] " + Encoding.Default.GetString(buffer));
                }

            }
        }
    }



    public static void StartServer()
    {
        //Thread
        processThread = new Thread(serverReciever);

        //Display
        Console.WriteLine("Your Local Ip is " + GetIp());
        Console.WriteLine("Please enter your name");
        username = Console.ReadLine();
        Console.WriteLine("Please enter HostPort");
        //  string portString = Console.ReadLine();
        //temp solution
        string portString = "80";
        try
        {
            port = Convert.ToInt32(portString);
        }
        catch
        {
            port = 8888;
        }

        try
        {
            //GetIp returns string
            serverIP = IPAddress.Parse(GetIp());
            //TCP Socket
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(new IPEndPoint(serverIP, port));
            serverSocket.Listen(0);

            //Server Socket listening for client requests
            clientSocket = serverSocket.Accept();
            printSession();
            processThread.Start();
            byte[] name = Encoding.Default.GetBytes(username);
            clientSocket.Send(name);
            while (true)
            {
                byte[] sdata = Encoding.Default.GetBytes(Console.ReadLine());
                clientSocket.Send(sdata, 0, sdata.Length, 0);
            }
        }
        catch
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Server already open!");
            Console.ForegroundColor = ConsoleColor.White;
        }
    }
}
}

This is the error I have received

Unhandled exception. System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.Sockets.Socket'. at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, SocketError& errorCode) at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at MessengerConsole.Client.clientReceiver() in C:\Users\MessengerConsoleAppV2\MessengerConsole\Client.cs:line 42 at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) --- End of stack trace from previous location where exception was thrown --- at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

I tried using Socket.Disconnect(), Socket.Close(), Socket.Dispose() and all of them yield same result

EDIT:

adding return in the

if(Encoding.Default.GetString(sdata) == "QUIT")
            {
                serverSocket.Send(sdata, sdata.Length, 0);
                serverSocket.Shutdown(SocketShutdown.Both);
                serverSocket.Close();
                return;
            }

Results in no errors on the server side but the client still throws the exception of the disposed object being accessed

Unhandled exception. System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.Sockets.Socket'. at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, SocketError& errorCode) at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at MessengerConsole.Client.clientReceiver() in C:\Users\MessengerConsoleAppV2\MessengerConsole\Client.cs:line 44 at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) --- End of stack trace from previous location where exception was thrown --- at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

In all places of code related to QUIT you only closing and disposing socket, but not returning from infinite loop serving this socket. I think this stack is thrown on next iteration when Connected property is evaluated on disposed socket, try adding return statement after you close socket.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM