Skip to main content
edited body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I am going to use below code in my application.

  Is this thread safe  ? Can I face any problem in the future if iI use this code? All suggestions welcome.

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? All suggestions welcome.

I am going to use below code in my application. Is this thread safe? Can I face any problem in the future if I use this code? All suggestions welcome.

Title, text.
Source Link
rolfl
  • 98.1k
  • 17
  • 220
  • 419

c# tcp server TCP Server with multithreading

iI am working on a banking application. iI want to create a multithreaded tcp iso8583TCP Payment Card (iso8583) server. which that can handle passbook printing requestrequests simultaneously. multiple deviceMultiple devices are connected to the server from different locations.
i

I am going to use below code in my application.
is

Is this thread safe ? can iCan I face any problem in future if i use this code.? kindly suggest meAll suggestions welcome.

c# tcp server with multithreading

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.

TCP Server with multithreading

I am working on a banking application. I want to create a multithreaded TCP Payment Card (iso8583) server that can handle passbook printing requests simultaneously. Multiple devices are connected to the 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? All suggestions welcome.

Source Link

c# tcp server with multithreading

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());

                }

            }

        }

    }