Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to implement an a sever that uses an SSL to authenticate a users.It require to authenticate a user and making more no. of requests through a ssl.



this is my function that accepts a new client and authenticat.but it is not receiving a next request from that client.

private void ProcessAccept(SocketAsyncEventArgs e)
        {

            if (!StopAcceptingUsers)
            {
                TClientConnectionToITS Client;
                Interlocked.Increment(ref m_numConnectedSockets);
                Interlocked.Increment(ref LoginCnt);

                SocketAsyncEventArgs readEventArgs = m_readWritePool.Pop();
                Client = (TClientConnectionToITS)readEventArgs.UserToken;
                Client.ITSClientSock = e.AcceptSocket;
                Client.e = e;

                IPEndPoint remoteIpEndPoint = Client.ITSClientSock.RemoteEndPoint as IPEndPoint;
                IPEndPoint localIpEndPoint = Client.ITSClientSock.RemoteEndPoint as IPEndPoint;

                Client.HashEntryId = LoginCnt;
                Client.reqBuffer = new byte[1024];
                readEventArgs.SetBuffer(Client.reqBuffer, 0, 1024);

                Client.ClearSendBuffer();
                Client.SockCreateTime = DateTime.Now;
                Client.ClientIP = ((IPEndPoint)Client.ITSClientSock.RemoteEndPoint).Address;
                Client.ServerIP = remoteIpEndPoint.Address;

                //SSLserver.EOL[ConnectionId] := 'ZZZ' ;
                Client.FEUserID = " ";
                Client.TraderID = " ";
                Client.FEUserType = 0;
                Client.FEUserPassword = " ";
                Client.FEUserPassword2 = " ";
                Client.ClientCnt = 0;  //for Non Trader Type only
                Client.ClientArray = null;  //for Non Trader Type only
                Client.ClientIP = localIpEndPoint.Address;  //null;
                Client.ServerIP = remoteIpEndPoint.Address;
                Client.ITS_IP = ITSForm.LocalMachineIP;
                Client.MachineID = "";
                Client.ConnTime = DateTime.Now;
               
                m_hashtable.Add(LoginCnt, Client);

                Client.ns = new NetworkStream(Client.ITSClientSock);
                Client.sslStream = new SslStream(Client.ns, false);

                Client.sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls, true);

                Client.sslStream.ReadTimeout = 500000;
                Client.sslStream.WriteTimeout = 500000;     
          
                byte[] buff = new byte[2048];
                
                buff = ReadMessage(Client);
                Client.Write(buff, buff.Length);

                ProcessReceive(readEventArgs);
            }
            StartAccept(e);
        }




C#
private void ProcessReceive(SocketAsyncEventArgs e)
        {
            TClientConnectionToITS token = (TClientConnectionToITS)e.UserToken;
            byte[] buff=new byte[2048];

            if (token.First)
            {
                token.First = false;
                Interlocked.Add(ref m_totalBytesRead, e.BytesTransferred);

                processData = () => MainForm.ClientDataAvailable(token, e);
                try
                {
                    MainForm.Invoke(processData);
                    ProcessSend(e);
                }
                catch (Exception ex)
                {
                    ITSForm.ErrorLog(ex);
                }
            }
            else
            {
                try
                {
                    if (!token.sslStream.IsAuthenticated)
                    {
                        token.sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls, true);
                    }


                    //buff = ReadMessage(token);
                    //token.Write(buff, buff.Length);

                   // token.sslStream.Read(buff, 0, 2048);
                    token.WrCount=e.BytesTransferred;

                    processData = () => MainForm.ClientDataAvailable(token, e);
                    try
                    {
                        MainForm.Invoke(processData); ProcessSend(e);
                    }
                    catch (Exception ex)
                    {
                        ITSForm.ErrorLog(ex);
                    }
                }
                catch (Exception ex) { }


            }
        }


public static byte[] ReadMessage(TClientConnectionToITS Client)
        {

            byte[] buffer = new byte[2048];
            StringBuilder messageData = new StringBuilder();
            int bytes = -1;   

            bytes = Client.sslStream.Read(buffer, 0, buffer.Length);        

            Decoder decoder = Encoding.UTF8.GetDecoder();
            char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
            decoder.GetChars(buffer, 0, bytes, chars, 0);
            messageData.Append(chars);           
            return buffer;
        }
Posted
Updated 21-May-12 0:57am
v3

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