【问题标题】:C# Client/Server: using Streamreader/writerC# 客户端/服务器:使用 Streamreader/writer
【发布时间】:2014-05-05 17:21:38
【问题描述】:

我对 C# 比较陌生,但这里是:

我正在用 C# 开发一个远程文件服务客户端/服务器控制台应用程序,它应该使用同步套接字交换消息。

其中一个主要问题(即使它看起来很简单)是使用 streamreader/streamwriter 从服务器返回一个字符串到客户端。

应用程序用户使用带有选项(来自 switch 语句)的命令行界面来执行操作。 IE。键入 1 并回车将执行代码以将字符串从服务器发送到客户端。

以下是来自客户端的示例代码:

        try
        {
            using (TcpClient client = (TcpClient)clientObject)
            using (NetworkStream stream = client.GetStream())
            using (StreamReader rd = new StreamReader(stream))
            using (StreamWriter wr = new StreamWriter(stream))
            {

                string menuOption = rd.ReadLine();


                switch (menuOption)
                    {
                        case "1":
                        case "one":
                            string passToClient = "Test Message!";
                            wr.WriteLine(passToClient);
                            break;
                   } 
                      while (menuOption != "4");
               }
         }

我知道我发布的代码只是程序的一个 sn-p,但它会占用相当多的空间,希望你能从中收集到我的意思,如果没有,我会发布更多。

这只是为了大致了解我的目标,

感谢您提供的任何帮助/建议。我正在寻找的代码示例并不多(尽管有一点帮助),而是更多关于流读取器/写入器的解释,因为我似乎无法理解在线的大部分内容。

谢谢。

【问题讨论】:

    标签: c# sockets tcp streamreader streamwriter


    【解决方案1】:

    我认为您只是缺少wr.flush();,但本文应该涵盖您需要的所有内容:

    http://thuruinhttp.wordpress.com/2012/01/07/simple-clientserver-in-c/

    【讨论】:

    • 啊,我错过了Flush()
    【解决方案2】:

    每当您使用StreamWriter 时,您都需要Flush() 流的内容。我将引用 MSDN,因为原因很清楚:

    清除当前写入器的所有缓冲区,并将所有缓冲数据写入底层流。

    你可以这么简单地称呼它:

    wr.flush();
    

    【讨论】:

      【解决方案3】:

      解决方案可以更简单:

      StreamWriter wr = new StreamWriter(stream) { AutoFlush = true }
      

      【讨论】:

        【解决方案4】:

        我刚刚使用您的代码进行了测试,它运行良好,我可以直接进入“一个”案例语句。

        我猜你要么没有在发送的字符串中包含换行符,要么只是 TcpClient 或 TcpListener 配置错误。

        这是我的测试的客户端代码:

                TcpClient client = new TcpClient("127.0.0.1", 13579);
                string message = "one" + Environment.NewLine;
                Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
                NetworkStream stream = client.GetStream();
                stream.Write(data, 0, data.Length);
        

        这里是服务器端:

                IPAddress localAddr = IPAddress.Parse("127.0.0.1");
                TcpListener server = new TcpListener(localAddr, 13579);
                server.Start();
        
                TcpClient client = server.AcceptTcpClient();
                using (client)
                using (NetworkStream stream = client.GetStream())
                using (StreamReader rd = new StreamReader(stream))
                using (StreamWriter wr = new StreamWriter(stream))
                {
                    string menuOption = rd.ReadLine();
                    switch (menuOption)
                    {
                        case "1":
                        case "one":
                            string passToClient = "Test Message!";
                            wr.WriteLine(passToClient);
                            break;
                    }
                    while (menuOption != "4") ;
                }
        

        只需先运行服务器端代码,它会在等待连接时阻塞,然后在等待数据时阻塞。然后运行客户端代码。您应该能够在 switch() 上捕获断点。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-26
          • 2013-06-10
          • 2014-02-19
          • 2010-10-31
          相关资源
          最近更新 更多