【问题标题】:C# cannot convert from "Gameserver.Client.TCP" to "Sytem.Net.Sockets.TcpClient"C# 无法从“Game Server.Client.TCP”转换为“System.Net.Sockets.TcpClient”
【发布时间】:2020-07-21 02:54:26
【问题描述】:

我正在尝试为一个简单的游戏设置一个测试服务器,我正在学习一个教程 (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。如果您需要更多源代码,请写评论。

【问题讨论】:

    标签: c# .net tcp


    【解决方案1】:

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

    您需要修复new 后面的类型或字段定义中的类型。根据结构(该类型 TCP 内部包含 TCPClient),您希望字段 tcp 为 TCP 类型。所以把public TCPClient tcp;改成public TCP tcp;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      • 2010-10-15
      • 2019-07-05
      • 2019-04-13
      相关资源
      最近更新 更多