简体   繁体   English

使用C#停止客户端服务器中的通信

[英]Stopping communication in client server using c#

I am trying to make a client server communication using c# .I have made the client and the server to communicate with each other. 我正在尝试使用c#进行客户端服务器通信。我已经使客户端和服务器相互通信。 But, the problem I am facing is that how do I stop the communication? 但是,我面临的问题是如何停止通信?

Condition: The server should always be listening (when multiple clients are present.) (presently, I am using single client). 条件:服务器应始终在侦听(如果存在多个客户端)。(目前,我正在使用单个客户端)。 The client should have a stop or exit condition (ie; when the client sends "exit" the client should terminate, the server should still be listening to other clients) when it finishes transmitting the data. 客户端应具有停止或退出条件(即,当客户端发送“退出”时,客户端应终止,服务器应仍在侦听其他客户端),当它完成数据传输时。

I am new to c#. 我是C#的新手。 Tried searching on google also. 也尝试过在Google上搜索。 but didn't find what I need. 但没有找到我所需要的。

Part of my code: 我的部分代码:

Server: 服务器:

        try
        {
            IPAddress addr = IPAddress.Parse("127.0.0.1");
            string a = "recieved by server";
            TcpListener Receiver = new TcpListener(addr, 1234);
            Receiver.Start();
            Console.WriteLine("trying to connect to " + addr);
            Socket s = Receiver.AcceptSocket();
            Console.WriteLine("Connected");
            while (true)
            {
                byte[] b = new byte[100];
                int k = s.Receive(b);
                char[] chars = new char[b.Length / sizeof(char)];
                System.Buffer.BlockCopy(b, 0, chars, 0, b.Length);
                string dataReceived = new string(chars);
                Console.WriteLine(dataReceived);
                byte[] bw = new byte[a.Length * sizeof(char)];
                System.Buffer.BlockCopy(a.ToCharArray(), 0, bw, 0, bw.Length);
                Console.WriteLine("sending Acknowledgement to client");
                s.Send(bw);
            }

            //Receiver.Stop();
        }

Client: 客户:

        try
        {
            TcpClient TcpC = new TcpClient();
            TcpC.Connect("127.0.0.1", 1234);
            while (true)
            {
                Console.WriteLine("enter somethiong to send");
                String s = Console.ReadLine();
                NetworkStream CStream = TcpC.GetStream();
                byte[] bw = new byte[s.Length * sizeof(char)];
                System.Buffer.BlockCopy(s.ToCharArray(), 0, bw, 0, bw.Length);
                Console.WriteLine("Transmit");
                CStream.Write(bw, 0, bw.Length);
                if (s == "exit")
                {
                    CStream.Flush();
                    CStream.Close();
                    TcpC.Close();
                    break;
                }
                byte[] br = new byte[100];
                int k = CStream.Read(br, 0, 100);
                char[] chars = new char[br.Length / sizeof(char)];
                System.Buffer.BlockCopy(br, 0, chars, 0, br.Length);
                string ackReceived = new string(chars);
                Console.WriteLine(ackReceived);
            }

            //TcpC.Close();
        }

Once you accepted socket connection from the client using Socket s = Receiver.AcceptSocket(); 一旦您使用Socket s = Receiver.AcceptSocket();接受来自客户端的套接字连接, Socket s = Receiver.AcceptSocket(); you need to put your commands processing logic in your while (true) { } block in some another thread (using a Task or Thread whatever), and then you should start accepting new socket connection using Receiver.AcceptSocket(); 您需要将命令处理逻辑放在另一个线程(同时使用TaskThread )的while (true) { }块中,然后应该使用Receiver.AcceptSocket();开始接受新的套接字连接Receiver.AcceptSocket(); again. 再次。

The problem is you do a 问题是你做一个

Receiver.Stop();

when it should be the socket you are closing. 当它应该是您要关闭的套接字时。

The idea is you create a new socket to handle each time a client connects - and that's the connection one you terminate, not the main listening socket. 这个想法是您创建一个新的套接字来处理每次客户端连接的情况-这是您终止的连接,而不是主监听套接字。

There is good sample code here: 这里有很好的示例代码:

https://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener(v=vs.110).aspx https://msdn.microsoft.com/zh-cn/library/system.net.sockets.tcplistener(v=vs.110).aspx

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

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