C#客户端向服务器发送多条消息

C#客户端向服务器发送多条消息,c#,client,tcpclient,streamreader,streamwriter,C#,Client,Tcpclient,Streamreader,Streamwriter,尝试将多条消息发送回服务器时遇到问题。 例如: 客户端发送到服务器 服务器响应 然后,客户端更改响应并将其发送回 然后再次读取服务器的响应 到目前为止,我所拥有的: Stream stream = client.GetStream(); StreamWriter sw = new StreamWriter(client.GetStream(), Encoding.ASCII); StreamReader sr = new StreamReader(client.GetStre

尝试将多条消息发送回服务器时遇到问题。 例如:

客户端发送到服务器

服务器响应

然后,客户端更改响应并将其发送回

然后再次读取服务器的响应

到目前为止,我所拥有的:

 Stream stream = client.GetStream();

    StreamWriter sw = new StreamWriter(client.GetStream(), Encoding.ASCII);
    StreamReader sr = new StreamReader(client.GetStream(), Encoding.ASCII);

 else if (args.Length == 2)
        {
            while (args[1] != response)
            {
                //  response = sr.ReadLine();
                if (args[1] != response)
                {

                    // this is what should be put in sw.write for the updated database entry
                    // <name><space><location><CR><LF>
                    // cssbct +  " " + "is in fenner", so "cssbct location has changed"
                    // args[0] + " " + response


                    // response is now what the arg is
                    response = args[1];
                    // write the new response so that it will be sent to the server
                    sw.WriteLine(args[0] + " " + response);
                    // send it to the server
                      sw.Flush();


                    // write out the args and that the location has changed
                    //  Console.WriteLine(args[0] + " " + " location changed to be" + " " + response);

                    // Just testing what the response should be 
                    Console.WriteLine(args[0] + " " + response);


                    string Response2 = "";
                    // read the response from the server
                    Response2 = sr.ReadLine();



                    // if it equals "OK" do this
                    if (Response2 == "OK")
                    {
                        // drops connection to the server
                        // probably not what is needed but look at later date
                        Console.WriteLine(response);

                        Console.WriteLine("Client connection closed");
                        client.Close();
                    }

                    if (sr.EndOfStream)
                    {
                        Console.WriteLine("End of stream has been reached");
                    }
Stream=client.GetStream();
StreamWriter sw=新的StreamWriter(client.GetStream(),Encoding.ASCII);
StreamReader sr=新的StreamReader(client.GetStream(),Encoding.ASCII);
else if(args.Length==2)
{
while(args[1]!=响应)
{
//响应=sr.ReadLine();
if(args[1]!=响应)
{
//这是更新的数据库条目应放入sw.write中的内容
// 
//cssbct+“+”位于芬纳,因此“cssbct位置已更改”
//args[0]+“”+响应
//现在的响应就是arg
响应=args[1];
//写入新响应,以便将其发送到服务器
sw.WriteLine(args[0]+“”+响应);
//将其发送到服务器
sw.Flush();
//写出参数并确认位置已更改
//Console.WriteLine(args[0]+“”+位置更改为“+“”+响应);
//只是测试响应应该是什么
Console.WriteLine(args[0]+“”+响应);
字符串响应2=“”;
//从服务器读取响应
Response2=sr.ReadLine();
//如果它等于“OK”,那么就这样做
如果(响应2=“确定”)
{
//断开与服务器的连接
//可能不需要什么,但看看以后的日期
控制台写入线(响应);
Console.WriteLine(“客户端连接已关闭”);
client.Close();
}
if(sr.EndOfStream)
{
Console.WriteLine(“已到达流的末尾”);
}
我在sw.flush()上收到一个错误,该错误表示“无法从传输连接读取数据:软件中止了已建立的连接”

感谢您的帮助

如果清除被注释掉,则到达流的末尾

尝试执行读取操作时,在flush()之后收到错误消息


你能解释一下什么是sw吗?它可能是一个StreamWriter。但是如果你想得到答案,你是如何创建它的就变得很重要了。@Vijay抱歉忘了添加这一点,是的,StreamWriter/ReaderClient流在关闭后无法使用。如果response2正常,你在这里就是这样做的。现在完成了注释,我的主要问题是w在刷新之后,它尝试读取响应。由于这个错误:,行Response2=sr.ReadLine();是@Fildor说的。我猜,
while
在连接现在关闭的情况下再次循环。