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;
Client.FEUserID = " ";
Client.TraderID = " ";
Client.FEUserType = 0;
Client.FEUserPassword = " ";
Client.FEUserPassword2 = " ";
Client.ClientCnt = 0;
Client.ClientArray = null;
Client.ClientIP = localIpEndPoint.Address;
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);
}
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);
}
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;
}