c# - 为什么发送数据包没有发送任何数据(与minecraft服务器握手)?

标签 c# sockets packet minecraft

我正在尝试使用数据包在 C# 中制作一个 Minecraft 假客户端,又名 Minecraft 聊天机器人。 我已经尝试了很多不同的方法来完成此任务,但没有成功。

每次我发送数据包时,它都不会发送任何数据(使用数据包嗅探器)。 虽然packetsniffers说数据包的总大小是:190字节。 大小为:17 字节。

这是我的代码:

 static TcpClient client = new TcpClient();
    static void Main(string[] args)
    {
        Console.WriteLine("Start GATHERING INFO.....");

        Console.Write("Write a ip: ");
        IPAddress ip = IPAddress.Parse("192.168.178.11");
        try
        {
            ip = IPAddress.Parse(Console.ReadLine());
        }
        catch
        {
            Console.Write("\nUnknown/Wrong ip entered redirecting to :  127.0.0.1 (AKA Localhost)");
            ip = IPAddress.Parse("192.168.178.11");
        }
        Console.Write("\nWrite a port: ");
        int port = int.Parse(Console.ReadLine());

        Console.WriteLine("Connecting.....");

        try
        {
            client.Connect(ip, port);
            client.NoDelay = false;
            Console.WriteLine("Connection succesfull!");
        }catch
        {
            Console.WriteLine("--== ERROR WHILE TRYING TO CONNECT PLEASE RESTART PROGRAM ==--");
            Console.ReadKey();
            client.Close();
            Main(args);
        }

        Stream stream = client.GetStream();

        Console.Write("Please enter a username: ");
        string usrn = Console.ReadLine();
        Console.Write("\n");

        byte[] data = new byte[3 + usrn.Length*2];
        data[0] = (byte)2;
        data[1] = (byte)29;
        gb(usrn).CopyTo(data, 2);

        stream.Write(data, 0, data.Length);

        Console.ReadKey();

    }

    public static byte[] gb(String str)
    {
        return System.Text.ASCIIEncoding.ASCII.GetBytes(str);
    }

数据包应如下所示:

http://www.wiki.vg/Protocol#Handshake_.280x02.29

我忽略了服务器主机和服务器端口,因为其他机器人没有使用它。 (尽管他们没有做到:/

以下是原始客户端数据包的内容:

'显示奇怪的转到:https://dl.dropbox.com/u/32828727/packetsocketsminecraft.txt '

timboiscool9(我的用户名) 192.168.178.1(服务器IP)

之后还有更多,但这就是我需要的。

我对套接字和 tcpclients 相当陌生

最佳答案

我稍微清理了一下你的代码:

    static void Main(string[] args)
    {
        bool keepTrying = true;
        while (keepTrying)
        {
            Console.Write("Enter server IP Address: ");
            IPAddress ip;
            if(!IPAddress.TryParse(Console.ReadLine(), out ip))
            {
                Console.WriteLine("Invalid ip entered, defaulting to 192.168.178.11");
                ip = IPAddress.Parse("192.168.178.11");
            }

            Console.Write("Enter server port: ");
            Int16 port;
            if(!Int16.TryParse(Console.ReadLine(), out port))
            {
                Console.WriteLine("Invalid port entered, defaulting to 1234");
                port = 1234;
            }

            Console.WriteLine("Connecting.....");

            try
            {
                TcpClient client = new TcpClient();
                client.Connect(new IPEndPoint(ip, port));
                client.NoDelay = false;
                Console.WriteLine("Connection succesfull!");

                List<byte> data = new List<byte>() { 2, 29 };
                Console.Write("Please enter a username: ");
                byte[] userName = ASCIIEncoding.ASCII.GetBytes(Console.ReadLine());
                data.AddRange(userName);

                using (var stream = client.GetStream())
                {
                    stream.Write(data.ToArray(), 0, data.Count);
                    Console.Write("Data sent!");
                }
                keepTrying = false;
            }
            catch
            {
                Console.WriteLine("--== ERROR CONNECTING ==--");
                Console.WriteLine();
            }
        }
    }

关于您原来的问题,我们需要更多信息。你说数据包嗅探器没有显示数据,但你说数据有大小。那么你看到数据了吗?你确定服务器已经启动了吗?我发布的代码对我有用,这意味着它连接到我本地系统上的服务器并发送字节。

关于c# - 为什么发送数据包没有发送任何数据(与minecraft服务器握手)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12709049/

相关文章:

c# - S.O.L.I.D Essentials 缺少点?

C++ - 如何在 vector 中使用 pcap_next_ex 存储离线读取数据包?

python - argv 的 ctypes 错误

c - Windows 和 Linux 之间通过套接字进行通信的问题

linux - 如何制作数据包的副本?

c# - 读取拆分的 TCP 数据包

c# - 如何使用 VS2010 安装项目将我的程序添加到用户开始菜单?

c# - 一个列表中具有更多不同类型的对象

c# - orderby 无法使用 LINQ 获取子查询中的前 1 个结果

java - 当我使用套接字通过本地网络传输文件时,如何防止文件损坏?