CSON全球攻势的RCON客户端

时间:2014-08-26 12:25:58

标签: .net vb.net tcp tcpclient

大家好,

我正在尝试使用VisualStudio.NET在Basic中编写自己的小RCON-Tool。 程序正确发送数据,所以我假设“发送” - 和“连接” - 部分正常工作。游戏服务器正确执行命令,但我没有收到任何消息。

我的函数buildTcpPacket只是根据source-rcon-protocol构建一个字节数组。

参数是实际命令(或第一次连接时的密码),请求的类型(命令为2,认证为3)和数据包的ID,根据我的研究,这些参数不重要,除非多个连接是开放的。

到目前为止,我尝试了两种非常相似的方法:

方法1:

Public Function connect() As String
    Try
        ' First, build the authentictation request.
        Dim authPacket As Byte() = buildTcpPacket(rcon_password, 3, 1)
        ' Create a TCP-client.*
        client = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        ' Connect the client to the server.
        client.Connect(ip, rcon_port)
        ' Try to autheticate.
        client.Send(authPacket)
        ' Create the buffer to read to. Max. package size is 4096 byte.
        Dim buffer(4095) As Byte
        ' Listen for the answer.
        client.Receive(buffer)
        Return System.Text.ASCII.Encoding.getString(buffer)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
        Return "-1"
    End Try
End Function

Public Function sendCommand(ByVal rconCommand As String) As String
    Try
        ' Build the package with Type = 2 (= Input to server).
        Dim cmdPacket As Byte() = buildTcpPacket(rconCommand, 2, 1)
        ' Send the command.
        client.Send(cmdPacket, SocketFlags.None)
        ' Create the buffer to read to. Max. package size is 4096 byte.
        Dim buffer(4095) As Byte
        ' Listen for the answer.
        client.Receive(buffer)
        Return Encoding.ASCII.GetString(buffer)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Function

方法2:

Private Sub connect()
    Try
        Dim client As New TcpClient(server, port)
        Dim data As Byte() = buildTcpPacket(rcon_password, 3, id)
        Dim stream As NetworkStream = client.GetStream()
        stream.Write(data, 0, data.Length)

        data = New Byte(4095) {}
        Dim bytes As Int32 = stream.Read(data, 0, data.Length)
        Dim response As String = Encoding.ASCII.GetString(data, 0, bytes)
        MsgBox("received: " & response)

        data = buildTcpPacket(rcon_command, 2, id)
        stream.Write(data, 0, data.Length)

        data = New Byte(4095) {}
        bytes = stream.Read(data, 0, data.Length
        response = Encoding.ASCII.GetString(data, 0, bytes)
        MsgBox("received: " & response)

        stream.Close()
        client.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

我只创建了方法2,以确保方法1没有RCON部分之外的其他问题,因为它嵌入在更大,更复杂的程序中。

如果我的英语很糟糕,我很抱歉,所有人都很感激。

1 个答案:

答案 0 :(得分:0)

找到解决方案:Encoding.ASCII.GetString()一旦找到终结符号,就会对流/数组进行编码。出于某种奇怪的原因,RCON服务器似乎在实际回答之前发送了乱码数据。

很抱歉,我无法提供任何代码,在迁移到新计算机时丢失了我的项目。如果有人遇到同样的问题,仍然想提供答案。