@@ -255,7 +255,14 @@ public void UnRegisterSession()
255255 encapsulation . Length = 0 ;
256256 encapsulation . SessionHandle = sessionHandle ;
257257
258- stream . Write ( encapsulation . toBytes ( ) , 0 , encapsulation . toBytes ( ) . Length ) ;
258+ try
259+ {
260+ stream . Write ( encapsulation . toBytes ( ) , 0 , encapsulation . toBytes ( ) . Length ) ;
261+ }
262+ catch ( Exception )
263+ {
264+ //Handle Exception to allow to Close the Stream if the connection was closed by Remote Device
265+ }
259266 byte [ ] data = new Byte [ 256 ] ;
260267 client . Close ( ) ;
261268 stream . Close ( ) ;
@@ -727,11 +734,25 @@ public void ForwardClose()
727734 System . Buffer . BlockCopy ( encapsulation . toBytes ( ) , 0 , dataToWrite , 0 , encapsulation . toBytes ( ) . Length ) ;
728735 System . Buffer . BlockCopy ( commonPacketFormat . toBytes ( ) , 0 , dataToWrite , encapsulation . toBytes ( ) . Length , commonPacketFormat . toBytes ( ) . Length ) ;
729736 encapsulation . toBytes ( ) ;
730-
731- stream . Write ( dataToWrite , 0 , dataToWrite . Length ) ;
737+ try
738+ {
739+ stream . Write ( dataToWrite , 0 , dataToWrite . Length ) ;
740+ }
741+ catch ( Exception e )
742+ {
743+ //Handle Exception to allow Forward close if the connection was closed by the Remote Device before
744+ }
732745 byte [ ] data = new Byte [ 564 ] ;
733746
734- Int32 bytes = stream . Read ( data , 0 , data . Length ) ;
747+ try
748+ {
749+ Int32 bytes = stream . Read ( data , 0 , data . Length ) ;
750+ }
751+ catch ( Exception e )
752+ {
753+ //Handle Exception to allow Forward close if the connection was closed by the Remote Device before
754+ }
755+
735756
736757 //--------------------------BEGIN Error?
737758 if ( data [ 42 ] != 0 ) //Exception codes see "Table B-1.1 CIP General Status Codes"
0 commit comments