© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
22 replies
Outspending

TcpClient being Disposed

I am pretty new to C#'s Dispose system, i came from JVM stuff so i'm pretty familiar with OOP.
Error:
Client Error: Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.
Client Error: Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.

Code:
public class ServerClient : IPacketable
{
    public readonly TcpClient Client;
    public readonly NetworkStream Stream;
    
    public ServerClient(TcpClient client)
    {
        Client = client;
        Stream = client.GetStream();
        
        Thread clientThread = new Thread(Init);
        clientThread.Start();
    }

    private void Init()
    {
        try
        {
            string clientAddress = ((IPEndPoint) Client.Client.RemoteEndPoint).Address.ToString();
            Console.WriteLine("Handling client from: " + clientAddress);

            new PacketListener(this);
            SendHandshakePacket(HandshakePacket.ConnectionState.Login);
        }
        catch (Exception e)
        {
            Console.WriteLine("Client Error: " + e.Message);
        }
    }

    public void SendHandshakePacket(HandshakePacket.ConnectionState state)
    {
        HandshakePacket packet = new HandshakePacket(
            protocolVersion: 754,
            serverAddress: "localhost",
            serverPort: 25565,
            nextState: state
        );
        
        SendPacket(packet);
    }
    
    public void SendPacket(IPacket packet)
    {
        byte[] packetBytes = packet.ToByteArray();
        Stream.Write(packetBytes, 0, packetBytes.Length);   
    }
}
public class ServerClient : IPacketable
{
    public readonly TcpClient Client;
    public readonly NetworkStream Stream;
    
    public ServerClient(TcpClient client)
    {
        Client = client;
        Stream = client.GetStream();
        
        Thread clientThread = new Thread(Init);
        clientThread.Start();
    }

    private void Init()
    {
        try
        {
            string clientAddress = ((IPEndPoint) Client.Client.RemoteEndPoint).Address.ToString();
            Console.WriteLine("Handling client from: " + clientAddress);

            new PacketListener(this);
            SendHandshakePacket(HandshakePacket.ConnectionState.Login);
        }
        catch (Exception e)
        {
            Console.WriteLine("Client Error: " + e.Message);
        }
    }

    public void SendHandshakePacket(HandshakePacket.ConnectionState state)
    {
        HandshakePacket packet = new HandshakePacket(
            protocolVersion: 754,
            serverAddress: "localhost",
            serverPort: 25565,
            nextState: state
        );
        
        SendPacket(packet);
    }
    
    public void SendPacket(IPacket packet)
    {
        byte[] packetBytes = packet.ToByteArray();
        Stream.Write(packetBytes, 0, packetBytes.Length);   
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

TcpClient connection
C#CC# / help
4y ago
SocketException, TCPListener, TCPClient reason?
C#CC# / help
2y ago
SqlConnection Connection Disposed
C#CC# / help
15mo ago