I can not share the server code as it is hosted inside an encryption device which I do not have access too nor is it documented.
In this scenario, I am and sending a command to the encryption device by socket inside a loop. The encryption device processes the socket command and upon success, forwards the command to a printer.
Scenario 1
for (int i = 1; i <= 10; i++)
{
// Step 1. Initialize TcpClient stream
m_client = new TcpClient();
m_client.Client.ExclusiveAddressUse = false;
m_client.ReceiveTimeout = 20000;
m_client.SendTimeout = 20000;
m_client.Connect(SrmIp, Port);
m_stream = new SslStream(m_client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
m_stream.AuthenticateAsClient("MEV", null, SslProtocols.Tls, false);
// Step 2. Write data command
m_stream.Write(aTaille, 0, aTaille.Length);
m_stream.Write(SrmCheckData, 0, SrmCheckData.Length);
// Step 3. Receive the status response from the encryption device
int status = ReceiveReturnCode(true);
if (status != 2)
throw new Exception("Encryption Error");
// Step 4.Close the stream
m_stream.Close();
}
In the above Scenario 1, I receive no stream errors. It is documented that the device will send back a 2 if the command was processed successfully. In Step 3 I check for this and I never receive an exception, indicating the device has receieved all 10 of my commands.
My problem is that it seems that the device only fully processes 5 of the 10 commands. Only half of them get forwarded to the printer.
If I move everything outside the loop, and don't close and open the socket every time than the device fully processes all 10 commands. It will also work as expected if I put in an unacceptably long sleep after each iteration in the loop. This leads me to believe that it's something to do with my lack of knowledge, and not their device.
I am fairly new to socket programming, is there something I am not understanding correctly on my client that is causing this behavior?