none
Disable pending connections queue on aTCPListener server RRS feed

  • Question

  • I'm developing a TCPListerner server with C# and .NET Framework 4.6.1.

    I want to create a synchronous server and refuse any incoming connection. In other words, if the server receives a connection request it has to reject it.

    To do it, I have written this code:
       
    class Program
        {
            static void Main(string[] args)
            {
                TcpListener server = null;
                try
                {
                    // Set the TcpListener on port 13000.
                    Int32 port = 13000;
                    IPAddress localAddr = IPAddress.Parse("127.0.0.1");
        
                    // TcpListener server = new TcpListener(port);
                    server = new TcpListener(localAddr, port);
        
                    // Start listening for client requests.
                    server.Start(0);

    But it seems it doesn't work because I can connect to Telnet sessions using Putty. The first one can send and receive data, but on the second one I can write data to send, but the server doesn't receive anything from the second one. If I close the first putty session, the server receives everything that the I wrote on the second one.

    How can I refuse any incoming connection if there is a client connected to the server?

    By the way, I have modified this example changing server.Start(); to server.Start(0);. 0 should mean that the server wont have incoming connection queue.

    I have used 1 instead of 0, and I have the same result that 0, it allows one pending connection. And I have also changed to 2, and it allows two pending connections. So, I think this number works, but I don't understand why it doesn't allow zero pending connections.
    Thursday, October 6, 2016 6:11 AM

Answers

  • Hi VansFannel,

    To achieve your requirement, I think you could stop TCPListener after you accept a TcpClient, and then restart TCPLister after you close the TcpClient. But now, if you send a request during stopping the TCPLister, you will get an error “SocketException: System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it 127.0.0.1:13000” which seems to be your expected result. You will need to handle this error at your client side.

    Here is a simple code for TCPListener.

            static void Main(string[] args)
            {
                TcpListener server = null;
                try
                {
                    Int32 port = 13000;
                    IPAddress localAddr = IPAddress.Parse("127.0.0.1");
                    server = new TcpListener(localAddr,port);
                    server.Start(0);
                    Byte[] bytes = new Byte[256];
                    string data = null;
                    while (true)
                    {
                        Console.WriteLine("Wairing for a connection...");
                        TcpClient client = server.AcceptTcpClient();
                        Console.WriteLine("Connected!");
                        server.Stop();
                        Console.WriteLine("stop listener!");
                        data = null;
                        NetworkStream stream = client.GetStream();
                        int i;
                        while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            data = System.Text.Encoding.ASCII.GetString(bytes,0,i);
                            Console.WriteLine("Received: {0}",data);
                            data = data.ToUpper();
                            byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
                            // Send back a response.
                            stream.Write(msg, 0, msg.Length);
                            Console.WriteLine("Sent: {0}", data);            
                        }
                        System.Threading.Thread.Sleep(10000);
                        client.Close();
                        server.Start();
                        Console.WriteLine("start listener!");
    
                    }
                    
    
                }
                catch (SocketException e)
                {
                    Console.WriteLine("SocketException: {0}", e);
                }
                finally
                {
                    // Stop listening for new clients.
                    server.Stop();
                }
    
    
                Console.WriteLine("\nHit enter to continue...");
                Console.Read();
    
            }
    

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by VansFannel Friday, October 7, 2016 7:43 AM
    Friday, October 7, 2016 2:14 AM