简体   繁体   English

C# 客户端/服务器:使用 Streamreader/writer

[英]C# Client/Server: using Streamreader/writer

I'm relatively new to C# but here goes:我对 C# 比较陌生,但这里有:

I am developing a remote file service client/server console application in C# which is supposed to exchange messages using synchronous sockets.我正在用 C# 开发一个远程文件服务客户端/服务器控制台应用程序,它应该使用同步套接字交换消息。

One of the main problems (even thought it might seem simple) is to return a string from the server, to the client using streamreader/streamwriter.主要问题之一(甚至认为它可能看起来很简单)是使用流读取器/流写入器将字符串从服务器返回到客户端。

The application user a command line interface with options (from a switch statement) to execute actions.应用程序用户使用带有选项(来自 switch 语句)的命令行界面来执行操作。 Ie typing 1 and enter would execute the code to send the string from the server to the client.即输入 1 并回车将执行代码以将字符串从服务器发送到客户端。

Below is a sample code from the client:以下是来自客户端的示例代码:

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

I understand the code I posted is just a snippet of the program, but it would take up a fair amount of space and was hoping you can gather what I mean from this, if not I will post more.我知道我发布的代码只是程序的一个片段,但它会占用相当多的空间,希望你能从中了解我的意思,如果不是,我会发布更多。

This is just to give a general idea of what I am going for,这只是为了大致了解我要做什么,

I appreciate any help / advice you can give.感谢您提供的任何帮助/建议。 Its not so much code examples I'm looking for (although a little would help) but more some explanation on streamreader/writer as I cant seem to understand much of what is online.它不是我正在寻找的代码示例(尽管有一点会有所帮助),而是更多关于 streamreader/writer 的解释,因为我似乎无法理解在线内容。

Thanks.谢谢。

I think you're just missing a wr.flush();我认为你只是缺少一个wr.flush(); but this article should cover everything you need:但这篇文章应该涵盖你需要的一切:

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

Whenever you use StreamWriter you need to Flush() the contents of the stream.每当您使用StreamWriter您都需要Flush()流的内容。 I'll quote MSDN as the reason becomes quite clear:我将引用 MSDN,因为原因变得很清楚:

Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.清除当前写入器的所有缓冲区,并使所有缓冲数据写入底层流。

You can call it quite simply like:你可以很简单地称它为:

wr.flush();

解决方案可以更简单:

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

I just ran a test using your code, and it works fine, I can step right into the "one" case statement.我刚刚使用您的代码运行了一个测试,它运行良好,我可以直接进入“one”case 语句。

I am guessing you are either not including the line-break in the string you are sending, or you just have the TcpClient or TcpListener configured wrong.我猜您要么没有在发送的字符串中包含换行符,要么只是将 TcpClient 或 TcpListener 配置错误。

Here is the Client-Side code for my test:这是我的测试的客户端代码:

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

Here is the Server-Side:这是服务器端:

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

Just run the server-side code first, which will block while waiting for connection and then while waiting for data.只需先运行服务器端代码,它会在等待连接时阻塞,然后在等待数据时阻塞。 Then run the client-side code.然后运行客户端代码。 You should be able to catch a breakpoint on the switch().您应该能够在 switch() 上捕获断点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM