1

ポート 2628 で dict.org サーバーに接続しようとしていますが、サーバーから完全な応答を取得できません。コードは次のようになります。

TcpClient client = new TcpClient("216.18.20.172", 2628);
            try
            {
                Stream s = client.GetStream();
                StreamReader sr = new StreamReader(s);
                StreamWriter sw = new StreamWriter(s);
                sw.AutoFlush = true;
                Console.WriteLine(sr.ReadLine());
                while (true)
                {
                    Console.Write("Word: ");
                    string msg = Console.ReadLine();
                    sw.WriteLine("D wn {0}", msg);
                    if (msg == "") break;

                    Console.WriteLine(sr.ReadLine());
                }
                s.Close();
            }
            finally
            {
                client.Close();
                Console.ReadLine();
            }

単語に「こんにちは」と入力すると、1行の応答が返されます。何かを入力してEnterキーを押すと、次の行が表示されます。完全な応答を一度に表示する方法は?

4

1 に答える 1

1

これは私が思いついたものです:

        static void Main(string[] args)
    {
        TcpClient client = new TcpClient("216.18.20.172", 2628);
        try
        {
            Stream s = client.GetStream();
            StreamReader sr = new StreamReader(s);
            StreamWriter sw = new StreamWriter(s);
            sw.AutoFlush = true;
            Console.WriteLine(sr.ReadLine());
            while (true)
            {
                Console.Write("Word: ");
                string msg = Console.ReadLine();
                sw.WriteLine("D wn {0}", msg);
                if (msg == "") break;

                var line = sr.ReadLine();
                while (line != ".") // The dot character is used as an indication that no more words are found
                {
                    Console.WriteLine(line);
                    line = sr.ReadLine();
                }
                sr.ReadLine();
            }
            s.Close();
        }
        finally
        {
            client.Close();
            Console.ReadLine();
        }
    }

他の応答タイプについても解決する必要があります。単語が見つからない場合、私のソリューションはハングしますが、ドット文字の代わりに特定の応答タイプの数字に注意することで簡単に修正できます。

ハッピーコーディング!

編集:これは決してエレガントなソリューションではありません。原理を説明したかっただけです。

于 2012-11-25T15:17:28.583 に答える