开发者

Problem with own TCPServer

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);

           开发者_如何学JAVA 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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜