大家好,
我正试图用visualstudio.net在basic中编写自己的小rcon工具。
程序正确地发送数据,所以我假设“发送”和“连接”部分工作正常。游戏服务器正确地执行命令,但我没有收到任何消息。
我的函数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部分没有其他问题,因为它嵌入了一个更大、更复杂的程序中。
如果我的英语不好,我很抱歉,谢谢大家的帮助。
最佳答案
找到了解决方案:Encoding.ASCII.GetString()
一旦找到终止符符号,就结束对流/数组的编码。出于某种奇怪的原因,RCON服务器在实际应答之前似乎发送了一些乱七八糟的数据。
抱歉,我无法提供任何代码,在迁移到新计算机时丢失了我的项目。不过,如果有人遇到同样的问题,我还是想提供一个答案。