Search code examples
.netvb.nettcptcpclient

RCON Client for CS Global Offensive


Hello everybody,

I'm trying to write my own little RCON-Tool in Basic using VisualStudio.NET. The program sends the data correctly so I assume the "send"- and "connect"-part work properly. The gameserver performs the commands correctly, but i don't receive any message back.

My function buildTcpPacket just builds a byte-array according to the source-rcon-protocol.

The parameters are the actual command (or password when conencting the first time), the type of request (2 for commands, 3 for authentification) and the ID of the packet, which, according to my research, is not important unless multiple connections are open.

Up until now, i have tried two very similar methods:

Method 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

And Method 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

I only created method 2 to make sure that method 1 has no other problem than the RCON-part, since it is embedded in a larger, more complicated program.

I'm sorry if my English is terrible, all help appreciated.


Solution

  • Found the solution: Encoding.ASCII.GetString() ends encoding the stream/array once it found a terminator symbol. For some strange reason the RCON server seems to send gibberish data before actually answering.

    Sorry I can't provide any code, lost my project while migrating to a new machine. Still wanted to provide an answer though, if anyone ever runs into the same problem.