I try to connect to my TeamSpeak 3 server using telnet in a C# application.
By the way, im not very experienced using telnet ^^', so I showed up the telnet Code at the Site https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient(VS.80).aspx
The following code should:
send the command "help" and read out the help Message
string command = "help";
// creates new TCP client
TcpClient client = new TcpClient(adress, port);
// get client stream
NetworkStream stream = client.GetStream();
// send Password
Byte[] data = System.Text.Encoding.ASCII.GetBytes(password);
stream.Write(data, 0, data.Length);
data = new Byte[256];
Thread.Sleep(200);
Int32 bytes = stream.Read(data, 0, data.Length);
String responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine(responseData);
// send the given command
Byte[] data2 = System.Text.Encoding.ASCII.GetBytes(command);
stream.Write(data2, 0, data2.Length);
data2 = new Byte[2560];
Thread.Sleep(200);
Int32 bytes2 = stream.Read(data2, 0, data2.Length);
String responseData2 = System.Text.Encoding.ASCII.GetString(data2, 0, bytes2);
Console.WriteLine(responseData2);
// end stream and client
stream.Close();
client.Close();
The first query works as it should and writes the welcome message into the Console. But at Int32 bytes2 = stream.Read(data2, 0, data2.Length);
in the seccond query the application stops without giving back any exeption.
Can anyone explain why the I cant read out the Help Message?
The reason the application appears to stop is because NetworkStream.Read()
will block if there is no data available to read and the connection is still open. Note that before calling stream.Read(data2, 0, data2.Length)
, we can see that the stream.DataAvailable
property is set to false
.
Now, as to why there is no data available: you need to terminate your commands with a line feed so that TeamSpeak knows that the command is complete:
string command = "help\n";
...
// send the given command
byte[] data2 = Encoding.ASCII.GetBytes(command);
stream.Write(data2, 0, data2.Length);
Your first query actually didn't succeed, for the same reason. The welcome message is sent by the server on connect; it is not a response to your command. Additionally, I can't see what the value of password
is, but if you intend to log in the full command is login <username> <password>
, like so:
TS3
Welcome to the TeamSpeak 3 ServerQuery interface, type "help" for a list of commands and "help <command>" for information on a specific command.
login serveradmin hunter2
error id=0 msg=ok