C# 无法从"Gameserver.Client.TCP"转换为"Sytem.Net.Sockets.TcpClient"



我正在尝试为一个简单的游戏设置一个测试服务器,我正在遵循一个教程(https://www.youtube.com/watch?v=uh8XaC0Y5MA)。有问题的代码是:

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

namespace GameServer
{
class Client
{
public static int dataBufferSize = 4096;
public int id;
public TcpClient tcp;
public Client(int _clientId)
{
id = _clientId;
tcp = new TCP(id);
}
public class TCP
{
public TcpClient socket;
private readonly int id;
private NetworkStream stream;
private byte[] receiveBuffer;
public TCP(int _id)
{
id = _id;
}
}
}
}

每当我试图运行程序时,我都会从标题中得到错误,因为这段代码(确切地说是"新TCP(id("部分(:

public Client(int _clientId)
{
id = _clientId;
tcp = new TCP(id);
}

根据我的理解,问题是我无法从自写代码转换到标准库,但我不认为这会是一个问题。所以我的问题是:我是否可以使用转换或其他解决方案来解决我的问题。提前谢谢。PS。如果你需要更多的源代码,请写一篇评论。

看起来您的字段tcp被定义为TCPClient类型,而不是TCP类型。类型不匹配。你希望它是哪种类型的?

您需要修复new后面的类型或字段定义中的类型。根据结构(TCP类型包含TCPClient(,您希望字段tcp为TCP类型。因此,将public TCPClient tcp;更改为public TCP tcp;