How to set up TcpListener to always listen and accept multiple connections?

Kehlan Krumme

This is my Server App:

public static void Main()
{
    try
    {
        IPAddress ipAddress = IPAddress.Parse("127.0.0.1");

        Console.WriteLine("Starting TCP listener...");

        TcpListener listener = new TcpListener(ipAddress, 500);

        listener.Start();

        while (true)
        {
            Console.WriteLine("Server is listening on " + listener.LocalEndpoint);

            Console.WriteLine("Waiting for a connection...");

            Socket client = listener.AcceptSocket();

            Console.WriteLine("Connection accepted.");

            Console.WriteLine("Reading data...");

            byte[] data = new byte[100];
            int size = client.Receive(data);
            Console.WriteLine("Recieved data: ");
            for (int i = 0; i < size; i++)
                Console.Write(Convert.ToChar(data[i]));

            Console.WriteLine();

            client.Close();
        }

        listener.Stop();
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: " + e.StackTrace);
        Console.ReadLine();
    }
}

As you can see , it always listens while working , but I would like to specify that I want the app be able to listen and to have multiple connections support in the same time.

How could I modify this to constantly listen while also accepting the multiple connections?

User 12345678
  1. The socket on which you want to listen for incoming connections is commonly referred to as the listening socket.

  2. When the listening socket acknowledges an incoming connection, a socket that commonly referred to as a child socket is created that effectively represents the remote endpoint.

  3. In order to handle multiple client connections simultaneously, you will need to spawn a new thread for each child socket on which the server will receive and handle data.
    Doing so will allow for the listening socket to accept and handle multiple connections as the thread on which you are listening will no longer be blocking or waiting while you wait for the incoming data.

while (true)
{
   Socket client = listener.AcceptSocket();
   Console.WriteLine("Connection accepted.");
    
   var childSocketThread = new Thread(() =>
   {
       byte[] data = new byte[100];
       int size = client.Receive(data);
       Console.WriteLine("Recieved data: ");
       
       for (int i = 0; i < size; i++)
       {
           Console.Write(Convert.ToChar(data[i]));
       }

       Console.WriteLine();
    
       client.Close();
    });

    childSocketThread.Start();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set TCPListener to always listen and when new connection discard current

From Dev

How do I listen to multiple IPs with TcpListener?

From Dev

Set up Socket Client always listen data

From Dev

Nginx: how to set up multiple servers with accept_filter=httpready

From Dev

Nginx: how to set up multiple servers with accept_filter=httpready

From Dev

How to break from the blocking TcpListener::accept call?

From Dev

Symfony3: How to set multiple connections?

From Dev

TCPListener blocks new connections

From Dev

Limit amount and time of connections TcpListener. How correct close\stop TcpListener and TcpClient?

From Dev

How can i interrupt a goroutine executing (*TCPListener) Accept?

From Dev

How to set up snmpd to listen on an alternative port (other than 161)?

From Dev

How to always listen to SMS android

From Dev

How to accept secure websocket connections in HornetQ

From Dev

TcpListener doesn't accept TcpClient

From Dev

AngularJS - how to set up $watch on multiple values

From Java

How to set up Travis CI with multiple languages

From Dev

How to set up multiple tabs navs in Bootstrap

From Dev

How to set up the relationships in Rails for multiple Collections

From Dev

AngularJS - how to set up $watch on multiple values

From Dev

How to set up and react to multiple display options?

From Dev

How to set up multiple GPUs (12.04)?

From Dev

How to set up multiple Tomcat instances?

From Dev

How to set up multiple IP addresses?

From Dev

I'm using Socket to listen for new connections. How do I break out of synchronous Socket.Accept() method? How do I terminate this call?

From Dev

Set some firewall ports to only accept local network connections?

From Dev

How to set up simple VPN for secure Internet connections over unencrypted Wi-Fi?

From Dev

How to set up EC2 with public IP for connections from itself?

From Dev

How to set up stateless NAT for two UDP connections from a global network to special network namespace?

From Dev

Ubuntu 19 Make X11 listen and accept connections on port 6000

Related Related

  1. 1

    How to set TCPListener to always listen and when new connection discard current

  2. 2

    How do I listen to multiple IPs with TcpListener?

  3. 3

    Set up Socket Client always listen data

  4. 4

    Nginx: how to set up multiple servers with accept_filter=httpready

  5. 5

    Nginx: how to set up multiple servers with accept_filter=httpready

  6. 6

    How to break from the blocking TcpListener::accept call?

  7. 7

    Symfony3: How to set multiple connections?

  8. 8

    TCPListener blocks new connections

  9. 9

    Limit amount and time of connections TcpListener. How correct close\stop TcpListener and TcpClient?

  10. 10

    How can i interrupt a goroutine executing (*TCPListener) Accept?

  11. 11

    How to set up snmpd to listen on an alternative port (other than 161)?

  12. 12

    How to always listen to SMS android

  13. 13

    How to accept secure websocket connections in HornetQ

  14. 14

    TcpListener doesn't accept TcpClient

  15. 15

    AngularJS - how to set up $watch on multiple values

  16. 16

    How to set up Travis CI with multiple languages

  17. 17

    How to set up multiple tabs navs in Bootstrap

  18. 18

    How to set up the relationships in Rails for multiple Collections

  19. 19

    AngularJS - how to set up $watch on multiple values

  20. 20

    How to set up and react to multiple display options?

  21. 21

    How to set up multiple GPUs (12.04)?

  22. 22

    How to set up multiple Tomcat instances?

  23. 23

    How to set up multiple IP addresses?

  24. 24

    I'm using Socket to listen for new connections. How do I break out of synchronous Socket.Accept() method? How do I terminate this call?

  25. 25

    Set some firewall ports to only accept local network connections?

  26. 26

    How to set up simple VPN for secure Internet connections over unencrypted Wi-Fi?

  27. 27

    How to set up EC2 with public IP for connections from itself?

  28. 28

    How to set up stateless NAT for two UDP connections from a global network to special network namespace?

  29. 29

    Ubuntu 19 Make X11 listen and accept connections on port 6000

HotTag

Archive