本文整理汇总了C#中NetworkStream.?.Close方法的典型用法代码示例。如果您正苦于以下问题:C# NetworkStream.?.Close方法的具体用法?C# NetworkStream.?.Close怎么用?C# NetworkStream.?.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NetworkStream
的用法示例。
在下文中一共展示了NetworkStream.?.Close方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: _sockConnection
//------------------------------------------------------------------------------------------------------------------------
SimpleActionResult _sockConnection(string RemoteHost, int RemotePort)
{
//declares
bool isSecured = false;
string sslProtocol = "";
var result = new SimpleActionResult()
{
IsSuccessful = false,
Message = "",
};
lock (this)
{
//create socket
#if NETFX
_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
#elif UNIVERSAL
_sock = new StreamSocket();
#endif
//Try to connect
try
{
//attemp connection
#if NETFX
_sock.Connect(RemoteHost, RemotePort);
#elif UNIVERSAL
SocketSetup?.Invoke(this, _sock);
_sock.Control.KeepAlive = true;
_sock.ConnectAsync(new Windows.Networking.HostName(RemoteHost), RemotePort.ToStringInvariant(), SocketProtectionLevel.PlainSocket).AsTask().Wait();
#endif
}
catch (Exception ex)
{
DebugEx.TraceError(ex, "Connection Error");
result.Message = ex.Message;
try { Close("Connection Error"); } catch { }
return result;
}
//create network stream
#if NETFX
//Stream _netstream = new BufferedStream(new NetworkStream(_sock, true));
Stream _netstream = new NetworkStream(_sock, true);
#endif
//Wrap with a secure stream?
if (Secure)
{
#if NETFX
//create ssl stream
var sslstream = new SslStream(_netstream, false);
#endif
//decide on certifacte server name
var remCertHostName = !string.IsNullOrWhiteSpace(CertificateServerName) ? CertificateServerName : RemoteHost;
try
{
#if NETFX
//collect certificates
var certs = Yodiwo.Tools.Certificates.CollectCertificates();
if (CustomCertificates != null)
foreach (var c in CustomCertificates)
certs.Add(c);
//try authenticate
sslstream.AuthenticateAsClient(remCertHostName,
certs,
System.Security.Authentication.SslProtocols.Tls | System.Security.Authentication.SslProtocols.Tls11 | System.Security.Authentication.SslProtocols.Tls12,
true
);
//checks
if (!sslstream.IsAuthenticated)
{
DebugEx.Assert("Not authenticated");
throw new Exception("Not authenticated");
}
if (!sslstream.IsEncrypted)
{
DebugEx.Assert("No encryption");
throw new Exception("Not encryption");
}
//get info
isSecured = true;
sslProtocol = sslstream.SslProtocol.ToStringInvariant();
//use this stream from now on
_netstream = sslstream;
#elif UNIVERSAL
_sock.UpgradeToSslAsync(SocketProtectionLevel.Tls12, new Windows.Networking.HostName(remCertHostName)).AsTask().Wait();
var _isSecured = _sock.Information.ProtectionLevel == SocketProtectionLevel.Tls10 ||
_sock.Information.ProtectionLevel == SocketProtectionLevel.Tls11 ||
_sock.Information.ProtectionLevel == SocketProtectionLevel.Tls12;
if (!_isSecured)
throw new Exception("Connection not secured (" + _sock.Information.ProtectionLevel + ")");
//.........这里部分代码省略.........
示例2: SetupServerSocket
public virtual bool SetupServerSocket()
#endif
{
try
{
//keep
bool isSecured = false;
string sslProtocol = "";
//check packer
if (SupportedChannelSerializationModes.HasFlag(ChannelSerializationMode.MessagePack))
DebugEx.Assert(MsgPack != null, "MessagePack serializer not provided");
//create network stream
#if NETFX
//Stream _netstream = new BufferedStream(new NetworkStream(base._sock, true));
Stream _netstream = new NetworkStream(base._sock, true);
//Wrap with a secure stream?
if (Server.Certificate != null)
{
var sslstream = new SslStream(_netstream, false);
try
{
//try authenticate
sslstream.AuthenticateAsServer(Server.Certificate, false, SslProtocols.Tls | SslProtocols.Tls12 | SslProtocols.Tls11, true);
//checks
if (!sslstream.IsAuthenticated)
{
DebugEx.Assert("Not authenticated");
throw new Exception("Not authenticated");
}
if (!sslstream.IsEncrypted)
{
DebugEx.Assert("No encryption");
throw new Exception("Not encryption");
}
//get info
isSecured = true;
sslProtocol = sslstream.SslProtocol.ToStringInvariant();
//use this stream from now on
_netstream = sslstream;
}
catch (Exception ex)
{
var msg = ex.Message;
if (ex.InnerException != null && ex.InnerException.Message != ex.Message)
msg += " (inner msg=" + ex.InnerException.Message + ")";
DebugEx.TraceError("Certificate not accepted, " + msg);
try { Close("Certificate not accepted, " + msg); } catch { }
try { sslstream.Close(); base._sock.Dispose(); } catch { }
try { _netstream.Close(); _netstream.Dispose(); } catch { }
try { _sock.Close(); _sock.Dispose(); } catch { }
return false; //failed
}
}
#endif
//read clients packers
var clientPackers = ChannelSerializationMode.Unkown;
var clientPreferredPackers = ChannelSerializationMode.Unkown;
#if NETFX
clientPackers = (ChannelSerializationMode)_netstream.ReadByte();
clientPreferredPackers = (ChannelSerializationMode)_netstream.ReadByte();
#elif UNIVERSAL
clientPackers = (ChannelSerializationMode)_sock.InputStream.AsStreamForRead().ReadByte();
clientPreferredPackers = (ChannelSerializationMode)_sock.InputStream.AsStreamForRead().ReadByte();
#endif
//filter packers
clientPackers = clientPackers & SupportedChannelSerializationModes;
clientPreferredPackers = clientPackers & clientPreferredPackers;
var serverPreferredPackers = clientPackers & PreferredChannelSerializationModes;
var commonPreferredPackers = clientPreferredPackers & serverPreferredPackers;
//choose packer
if ((_ChannelSerializationMode = _choosePacker(commonPreferredPackers)) == ChannelSerializationMode.Unkown &&
(_ChannelSerializationMode = _choosePacker(clientPreferredPackers)) == ChannelSerializationMode.Unkown &&
(_ChannelSerializationMode = _choosePacker(serverPreferredPackers)) == ChannelSerializationMode.Unkown &&
(_ChannelSerializationMode = _choosePacker(clientPackers)) == ChannelSerializationMode.Unkown)
{
DebugEx.TraceError("Could not decide on packer.");
try { Close("Could not decide on packer."); } catch { }
#if NETFX
try { _netstream?.Close(); _netstream?.Dispose(); } catch { }
try { _sock?.Close(); _sock?.Dispose(); } catch { }
#elif UNIVERSAL
try { _sock?.Dispose(); } catch { }
#endif
return false; //failed
}
//write packer
#if NETFX
var _nodelay = _sock.NoDelay;
_sock.NoDelay = true; //Disable the Nagle Algorithm
_netstream.WriteByte((byte)_ChannelSerializationMode);
//.........这里部分代码省略.........