发送数据包后接收字节时C#表冻结

时间:2018-05-03 02:32:47

标签: c# sockets

好吧,你们中的一些人可能不会从标题中理解,但我正在努力为C#游戏做点什么。它涉及连接到套接字,以便我可以将数据包发送到服务器并使一切正常。

但问题是,当我在发送数据包后去接收时,程序只是冻结在一起,我甚至无法得到响应以确保其正常工作。在这里,生病通过展示我的代码向您展示我的意思。

private void sendRawPackets(Socket s, string packet)
    {
        byte[] data = Encoding.ASCII.GetBytes(string.Format("{0}\0", packet));
        s.Send(data);
    }

    private void gethash(string cmd, string args)
    {
        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = @"C:\Python27\python.exe";
        start.Arguments = string.Format("{0} {1}", cmd, args);
        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;
        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadLine();
                logkey = "";
                logkey = result;
            }
        }
        connect();
    }

    private void connect()
    {
        byte[] bytes = new byte[2024];
        Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        sock.Connect(ip, port);

        sendRawPackets(sock, "<policy-file-request/>");
        sock.Receive(bytes);
        sendRawPackets(sock, "<msg t='sys'><body action='verChk' r='0'><ver v='154' /></body></msg>");
        sock.Receive(bytes);
        sendRawPackets(sock, "<msg t='sys'><body action='login' r='0'><login z='Slime'><nick><![CDATA["+user.Text+"]]></nick><pword><![CDATA["+logkey+"]]></pword></login></body></msg>");
        sock.Receive(bytes);
        sendRawPackets(sock, "%xt%login%1#2%-2");
        sock.Receive(bytes);
        sendRawPackets(sock, "%xt%login%2#6%-1%");
        sock.Receive(bytes);
        sendRawPackets(sock, "%xt%login%2#7%17610%69%0,-255,190%");
        sock.Receive(bytes);
        sendRawPackets(sock, "%xt%login%2#4%3150%FlumsFountain%52.9941101744771%0%989.1726320236921%-180%0%190%");
        sock.Receive(bytes);
    }

但无论如何,在connect()函数中,这是使用sendRawPackets()函数发送数据包的地方。但由于某种原因,当我在发送数据包后尝试获取字节时,程序再次冻结并且不会继续。

我想知道是否有办法解决这个问题?如果有人知道答案,真的会有所帮助。 :)

编辑基本上尝试使用这个python代码并将其转换为C#代码,但对我来说效果不佳。

self.sendRawPacket(s, "<policy-file-request/>")
            s.recv(2024)
            self.sendRawPacket(s, "<msg t='sys'><body action='verChk' r='0'><ver v='154' /></body></msg>")

2 个答案:

答案 0 :(得分:0)

如果您不想让UI线程冻结

,请尝试此操作
using System.ComponentModel;


private void connect()
{
    using(BackgroundWorker bw = new BackgroundWorker())
    {
        Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        sock.Connect(ip, port);
        bw.DoWork += (o, e) => {
            sendRawPackets(sock, "<policy-file-request/>");
            Thread.Sleep(600);
            sendRawPackets(sock, "<msg t='sys'><body action='verChk' r='0'><ver v='154' /></body></msg>");
            Thread.Sleep(600);
            sendRawPackets(sock, "<msg t='sys'><body action='login' r='0'><login z='Slime'><nick><![CDATA[" + user.Text + "]]></nick><pword><![CDATA[" + logkey + "]]></pword></login></body></msg>");
            Thread.Sleep(600);
            sendRawPackets(sock, "%xt%login%1#2%-2");
            Thread.Sleep(600);
            sendRawPackets(sock, "%xt%login%2#6%-1%");
            Thread.Sleep(600);
            sendRawPackets(sock, "%xt%login%2#7%17610%69%0,-255,190%");
            Thread.Sleep(600);
            sendRawPackets(sock, "%xt%login%2#4%3150%FlumsFountain%52.9941101744771%0%989.1726320236921%-180%0%190%");
            Thread.Sleep(600);
        }

        bw.RunWorkerCompleted += (o, e) => {
            //When all the work is finished
        }

        //Runs the background worker
        bw.RunWorkerAsync();
    }

}

答案 1 :(得分:0)

弄明白了这个问题。当我试图发送数据包时,我正在使用UDP和Dgram进行套接字类型。

但我真正需要做的是将协议类型更改为Tcp,将SocketType更改为流

抱歉一切。