c# tcp server with multithreading

1.2k views Asked by At

i working on banking application. i want to create multithreaded tcp iso8583 server. which can handle passbook printing request simultaneously. multiple device are connected to server from different locations.
i am going to use below code in my application.
is this thread safe ? can i face any problem in future if i use this code.? kindly suggest me.

class Program
    {

        static void Main(string[] args)
        {

            TcpListener serverSocket = new TcpListener(8888);

            TcpClient clientSocket = default(TcpClient);

            int counter = 0;



            serverSocket.Start();

            Console.WriteLine(" >> " + "Server Started");



            counter = 0;

            while (true)
            {

                counter += 1;

                clientSocket = serverSocket.AcceptTcpClient();

                Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");

                handleClinet client = new handleClinet();

                client.startClient(clientSocket, Convert.ToString(counter));

            }



            clientSocket.Close();

            serverSocket.Stop();

         //   Console.WriteLine(" >> " + "exit");

           Console.ReadLine();

        }

    }



    //Class to handle each client request separatly

    public class handleClinet
    {

        TcpClient clientSocket;

        string clNo;

        public void startClient(TcpClient inClientSocket, string clineNo)
        {

            this.clientSocket = inClientSocket;

            this.clNo = clineNo;

            Thread ctThread = new Thread(doChat);

            ctThread.Start();

        }

        private void doChat()
        {

            int requestCount = 0;

            byte[] bytesFrom = new byte[10025];

            string dataFromClient = null;

            Byte[] sendBytes = null;

            string serverResponse = null;

            string rCount = null;

            requestCount = 0;



            while ((true))
            {

                try
                {
                    var respose = "";
                    requestCount = requestCount + 1;

                    NetworkStream networkStream = clientSocket.GetStream();

                    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

                    dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);

                  //  dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

                    Console.WriteLine(" >> " + "From client-" + clNo + dataFromClient);

                    try
                    {
                        var isoPassbookRequestMessage = System.Text.Encoding.ASCII.GetString(bytesFrom);
                        WebClient wc = new WebClient();
                        NameValueCollection input = new NameValueCollection();
                        input.Add("isoPassbookRequest", Convert.ToBase64String(bytesFrom));
                        respose = Encoding.ASCII.GetString(wc.UploadValues("http://localhost:52835/Transaction/PassbookTransactionRequest", input));


                        try
                        {
                          //  CommonMethods.AddtoLogFile("PassbookTransactionResponse = Clientid-" + clientID + " ProcessingCode -930000 Message -" + respose);
                          //  atmServer.Send(clientID, Encoding.ASCII.GetBytes(respose));
                        }
                        catch (SocketException se)
                        {

                            //could not complete transaction
                            //Send reversal to CBS

                        }
                    }
                    catch (Exception e)
                    {

                    }

                    rCount = Convert.ToString(requestCount);

                   serverResponse = "Server to clinet(" + clNo + ") " + rCount;


                    sendBytes = Encoding.ASCII.GetBytes(respose);

                    networkStream.Write(sendBytes, 0, sendBytes.Length);

                    networkStream.Flush();

                    Console.WriteLine(" >> " + serverResponse);

                }

                catch (Exception ex)
                {

                    Console.WriteLine(" >> " + ex.ToString());

                }

            }

        }

    }
0

There are 0 answers