Search code examples
c#.nettcp

C# cannot convert from "Gameserver.Client.TCP" to "Sytem.Net.Sockets.TcpClient"


I am trying to set up a test server for a simple Game and I am following a tutorial (https://www.youtube.com/watch?v=uh8XaC0Y5MA). The problematic bit of code is:

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;
            }
        }
    }
}

Whenever I try to run the program I get the error from the title because of this block of code(precisely the "new TCP(id)" Part):

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

From my understanding the problem is that I cant convert from the Self written code to the standard library but I don't see where that would be a problem. So my question is: is there a conversion I can use or some other solution to my problem. Thank you in advance. PS. If you need more of the source code please write a Comment.


Solution

  • Looks like your field tcp is defined as the type TCPClient, not as type TCP. There's a type mismatch. Which type did you want it to be?

    You need to fix either the type behind new, or the type in the definition of your field. Based on the structure (that type TCP contains a TCPClient inside), you wanted the field tcp to be of type TCP. So change public TCPClient tcp; to public TCP tcp;.