简体   繁体   English

在C#套接字中收到消息后,将客户端与服务器断开连接

[英]disconnecting a client from server after receiving message in c# socket

I have made a small client program which will receive a message from server. 我做了一个小的客户端程序,它将接收来自服务器的消息。 I want to disconnect the client from the server safely after receiving the message.(Another case may also arise such that, I want to compare the received value with another value and if the don't match, the client will be disconnected) My sample code is: 我想在收到消息后安全地断开客户端与服务器的连接。(另一种情况是,我想将接收到的值与另一个值进行比较,如果不匹配,客户端将被断开连接)代码是:

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
class Program
{

    static void Main(string[] args)
    {
        try
        {                
            TcpClient tcpClient = new TcpClient("127.0.0.1", 1234);

            NetworkStream ns = tcpClient.GetStream();
            StreamReader sr = new StreamReader(ns);
            StreamWriter sw = new StreamWriter(ns);
            string data;

            //receiving message
            data = sr.ReadLine();
            Console.WriteLine(data);

            //I want to disconnect here     
         }
        catch (Exception e)
        {
            Console.Write(e.Message);
        }

        Console.Read();
    }
}

What additional things will I need to add? 我还需要添加什么?

Just wrap the usage of the TcpClient instance and NetworkStream in a using block: 只需将TcpClient实例和NetworkStream的用法包装在using块中:

using(TcpClient tcpClient = new TcpClient("127.0.0.1", 1234))
using(NetworkStream ns = tcpClient.GetStream())
{
  // Do read here
}

I would structure your code more like: 我会更像您的代码结构:

using (var tcpClient = new TcpClient("127.0.0.1", 1234))
{
    using(var ns = tcpClient.GetStream())
    {
        StreamReader sr = new StreamReader(ns);
        StreamWriter sw = new StreamWriter(ns);
        string data;

        //receiving message
        data = sr.ReadLine();

        if(data == somePreDeterminedValue)
        {
            Console.WriteLine(data);
        }
    }
}

This will mean that when you hit the closing brace it'll automatically close the TCPClient and NetworkStream for you and dispose of their instances. 这意味着当您点击右括号时,它将自动为您关闭TCPClient和NetworkStream并处置它们的实例。

In this case if the value you get from the stream is correct then it won't disconnect, simple. 在这种情况下,如果您从流中获取的值是正确的,那么它将不会断开连接,很简单。 I think you'll need to provide what you want to do overall for a complete answer to your question as in this simple terms that is what I'd do, but in this example the TCPClient is closed regardless. 我认为您需要提供总体上想要做的问题的完整答案,就像我要做的这个简单术语一样,但是在此示例中,无论如何,TCPClient都是关闭的。

In a real world application you'd essentially want to keep a field of the TCPClient that you open and close throughout the application and only dispose of when your messaging object is disposed. 在实际的应用程序中,您实际上希望保留在整个应用程序中打开和关闭的TCPClient字段,并且仅在处理消息传递对象时才进行处理。

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

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