Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
hi,
I have written for connectivity using tcpclient.
but after every successful transaction it creates new session for each request.
please do help..thanks in advance.
my code is as..


C#
namespace CBS
{
    class TCPListner
    {
        private static string hostName = "";
        private static string output = "";
        private static int portNo;
        public static NetworkStream stream = null;
        private static void Main()
        {
            ArrayList doneEvents = new ArrayList();

            TcpClient client;

            int iVal = 0;
            ClsDataRequestResponse response = new ClsDataRequestResponse();
            DataSet hostName = response.GetHostName();
            if (hostName.Tables[0].Rows.Count > 0)
            {
                TCPListner.hostName = hostName.Tables[0].Rows[0]["host_ip"].ToString();
                portNo = Convert.ToInt32(hostName.Tables[0].Rows[0]["server_port"].ToString());
            }
            else
            {
                TCPListner.hostName = "localhost";
                portNo = 80;
            }

            TcpListener listener = null;
            IPAddress localaddr = IPAddress.Parse(TCPListner.hostName);//Dns.GetHostEntry(Program.hostName).AddressList[0];
            try
            {
                listener = new TcpListener(localaddr, portNo);
                listener.Start();
                output = "Waiting for a connection...";
                clslog.WriteLog(output);
            }
            catch (Exception exception)
            {
                output = "Error: " + exception.ToString();
                Console.WriteLine(output);
                clslog.WriteLog(output);
            }

       
            while (true)
            {
                iVal++;
                client.NoDelay = true;
                client.Client.NoDelay = true;
                Console.WriteLine("Waiting For Request No......" + iVal.ToString());
                client = listener.AcceptTcpClient();
                clsReqHandle objReqHandle = new clsReqHandle();
                //Read request string
                object[] Req = readReq(client, iVal, stream);
                //Process request
                objReqHandle.StartReq(client, response, Req[0], Req[1], iVal);
            }
        }
        public static object[] readReq(TcpClient _client, int reqID,NetworkStream _stream)
        {
            object[] strReqParam = new object[2];
            byte[] buffer = new byte[384];
           
            _stream = _client.GetStream();
            _stream.Read(buffer, 0, buffer.Length);
            string str = Encoding.ASCII.GetString(buffer, 0, buffer.Length);
            str= str.Replace('\0',' ');            
            if (str.TrimEnd().Length>5 )
            {
                int iStrLength = Convert.ToInt32(str.Substring(0, 4));
                str = str.Substring(0, iStrLength + 4);
            }
            
           
                       strReqParam[0] = str;
            strReqParam[1] = _stream;
            //stream.Flush();
            return strReqParam;
        }
    }

    class clsReqHandle
    {
        TcpClient client;
        ClsDataRequestResponse response;
        string str;
        NetworkStream stream;
        int reqID = 0;
        public void StartReq(TcpClient _client, ClsDataRequestResponse _response, object strReq, object _stream, int _iVal)
        {
            this.client = (TcpClient)_client;
            this.response = _response;
            this.str = (string)strReq;
            this.stream = (NetworkStream)_stream;
            reqID = _iVal;
            Thread objThread = new Thread(Process);
            objThread.Start();
        }

        public void Process()
        {
            int batchNo = response.GetBatchNo();
            DateTime currentDate = response.GetCurrentDate();
            string str2 = response.InsertRequest(str, currentDate, batchNo);
            byte[] bytes = new byte[str2.Length];
            bytes = Encoding.ASCII.GetBytes(str2);
            stream.Write(bytes, 0, bytes.Length);            
                   
            stream.Flush();
            stream.Close();
           //stream.Dispose();
          
        }
    }
}
Posted
Updated 11-Apr-14 1:33am
v2
Comments
laxman ghadage 7-Apr-14 7:27am    
please note
I m using C#
Herman<T>.Instance 11-Apr-14 9:20am    
stream.Close(); ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900