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:
Code:
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'.
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);
}
}
23 replies