gpt4 book ai didi

c# - AcceptTcpClient() 和基于 TcpClient 的类

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:38 24 4
gpt4 key购买 nike

我需要一个类来包含 TcpClient 的所有内容 + 几个额外的字段,所以我创建了一个基于 TcpClient 的类:

public class MyClient: TcpClient
{
public string winUser;
public bool needHelp;
public bool needSignOff;

public MyClient() : base() {
this.needHelp = false;
this.needSignOff = false;
this.winUser = "empty";
}
}

现在我需要将所有传入连接创建为此类的对象:

MyClient clientConnection = (MyClient)this.helpServer.AcceptTcpClient();

问题是 AcceptTcpClient() 生成了一个 TcpClient 类对象,当我尝试连接到服务器时,我一直收到这个异常:

listenServer():无法将“System.Net.Sockets.TcpClient”类型的对象转换为类型“Server.MyClient”。

如何将 AcceptTcpClient() 给我的 TcpClient 对象转换为 MyClient 对象?

最佳答案

无法将类型 (TcpClient) 向下转换为更派生的类型 (MyClient)。您必须更新 AcceptTcpClient 以返回一个 MyClient 对象。

关于c# - AcceptTcpClient() 和基于 TcpClient 的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8251860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com