devxlogo

Closing an SSLEngine Connection

Closing an SSLEngine Connection

This tip shows you how to correctly close a SSLEngine connection. Notice that this is not a simple process, like closing a simple socket!

SSLContext context ...;SSLEngine engine ...;ByteBuffer dummy ...;ByteBuffer OutputNet ...;engine.closeOutbound();while(!engine.isOutboundDone())       {       dummy.flip();       OutputNet.clear();              result=engine.wrap(dummy,OutputNet);       status=result.getStatus();       System.out.println("Status:"+status);                                          if(status==SSLEngineResult.Status.BUFFER_OVERFLOW)            {            System.out.println("BUFFER_OVERFLOW");                                                                                          OutputNet=ByteBuffer.allocate(SOutputNet.capacity()+100);            }             if(status==SSLEngineResult.Status.OK)            {                                                System.out.println("OK");            while(OutputNet.hasRemaining())                  {                  try{                     int W=channel.write(OutputNet);                     if(W==-1)                        {                        //the channel has been closed                        }                                             if(W==0)                        {                        //nothing has been read                        }                         }catch(IOException e)                        {                        ?                        }                     }           OutputNet.compact();           }                   //BUFFER_UNDERFLOW, CLOSE                                                       }   //closing the transport mechanismchannel.close();                                                                           
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist