views:

25

answers:

2

Hello. I have written a TCP server. Then I am trying to connect to my server used Telnet (telnet localhost 2200). Problem: telnet write bad text - like this: ? ?????...

static void Main(string[] args)
    {
        ASCIIEncoding encoding = new ASCIIEncoding();
        //UnicodeEncoding encoding = new UnicodeEncoding();
        Byte[] message = encoding.GetBytes("Я занят...");

        try
        {
            IPAddress localAddress = IPAddress.Parse("127.0.0.1");
            TcpListener listener = new TcpListener(localAddress,2200);

            listener.Start(1);

            while (true)
            {
                Console.WriteLine("Сервер ожидает {0}", listener.LocalEndpoint);
                TcpClient client = listener.AcceptTcpClient();

                NetworkStream io = client.GetStream();
                Console.WriteLine("Принято соединение от {0}", client.Client.RemoteEndPoint);

                Console.WriteLine("Отправляем сообщение...");
                io.Write(message,0,message.Length);

                Console.WriteLine("Закрытие соединения");
                client.Close();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Произошла ошибка {0}", e.Message);
        }
    }

The text on Russian language.If text on English then OK. What is the problem, may be codepage? Thanks and sorry for my bad English.

+2  A: 

ASCIIEncoding wont work with Russian. Use UTF8Encoding or UTF32Encoding (I'm unsure of which one that works with Russian).

jgauffin
I'm trying, not work.
What's the new error? Update the code with your changes.
jgauffin
I have change ASCIIEncoding on UTF32Encoding, but text is bad again: /♦ 7♦0♦=♦O♦B♦...
Where are you outputting the text? You need to specify encoding there too.
jgauffin
I have output in standard windows telnet client fire up via command promt.
Then the standard telentclient doesn't support UTF. Putty (http://www.putty.org/) supports UTF-8
jgauffin
A: 

Maybe netcat gives better results. It does less interpreting of the returned data than telnet.

Sjoerd