Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Monday, April 4, 2022

C# Asynchronous Tcp Serve

 This is the model of an Asynchronous TCP server, that can have multiple clients connected, each one on it’s own thread - all of this is done using a relatively short and easy-to-remember code.

The code

As always, the code first and after, the comments:

  • functions used for sending/receiving data are not included - you should know these, from the basic server stuff.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;      
using System.Net.Sockets;  

using System.Threading;

namespace ServerTest
{
    class Program
    {

        static void Main(string[] args)
        {
            Program main = new Program();
            main.server_start();  //starting the server

            Console.ReadLine();  
        }

        TcpListener server = new TcpListener(IPAddress.Any, 9999);   

        private void server_start()
        {
            server.Start();    
            accept_connection();  //accepts incoming connections
        }

        private void accept_connection()
        {
            server.BeginAcceptTcpClient(handle_connection, server);  //this is called asynchronously and will run in a different thread
        }

        private void handle_connection(IAsyncResult result)  //the parameter is a delegate, used to communicate between threads
        {
            accept_connection();  //once again, checking for any other incoming connections
            TcpClient client = server.EndAcceptTcpClient(result);  //creates the TcpClient

            NetworkStream ns = client.GetStream();

            /* here you can add the code to send/receive data */

        }

    }
}

Comments

This is the server, containing almost anything required to work properly.

Now, to understand how it works, take a look at the methods below:


        private void server_start()
        {
            ...  
            accept_connection();  
        }

        private void accept_connection()
        {
            server.BeginAcceptTcpClient(handle_connection, server);  
        }

        private void handle_connection(IAsyncResult result)
        {
            accept_connection();
            TcpClient client = server.EndAcceptTcpClient(result);
            ...
        }

  • first, the program calls the function accept_connection() - used to accept a client’s connection to the server. This function will invoke through BeginAcceptTcpClient another function called handle_connection() which will run on a different thread chosen from the threadpool - so you don’t have to manually create/release threads.

  • when handle_connection() is called, it also receives an IAsyncResult argument - this argument maintains the connection between the 2 threads. This method will then call again accept_connection() - so the program will constantly change the threads.

  • if the connection is closed, the thread used is released automatically and can later be used for a new client; however this threadpool is limited.

In the end…

This is basically everything you might need to know about the asynchronous tcp servers - without involving any unnecessary code. If there are any questions, feel free to leave a comment :)

 

No comments:

Post a Comment