简体   繁体   中英

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. 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#. Tried searching on google also. 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(); 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(); 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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