C#客户端/服务器:使用Streamreader / writer [英] C# Client/Server: using Streamreader/writer
问题描述
我对C#来说还比较陌生,但这里有:
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.
主要问题之一(甚至认为它看起来似乎很简单)是使用streamreader / streamwriter从服务器向客户端返回一个字符串。
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.
应用程序用户是一个命令行界面,带有用于执行操作的选项(来自switch语句)。即键入1并输入enter将执行代码,以将字符串从服务器发送到客户端。
The application user a command line interface with options (from a switch statement) to execute actions. I.e. typing 1 and enter would execute the code to send the string from the server to the client.
下面是客户端的示例代码:
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.
这仅仅是
感谢您提供的任何帮助/建议。它不是我正在寻找的代码示例(尽管有一些帮助),但是关于流阅读器/写入器的更多解释是因为我似乎无法理解很多在线内容。
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.
谢谢。
推荐答案
我认为您只是想念一个 wr.flush();。
,但本文应涵盖您需要的所有内容:
I think you're just missing a wr.flush();
but this article should cover everything you need:
http://thuruinhttp.wordpress.com/2012/01/07/simple-clientserver-in-c/
这篇关于C#客户端/服务器:使用Streamreader / writer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!