本文整理汇总了C#中SslStream.Read方法的典型用法代码示例。如果您正苦于以下问题:C# SslStream.Read方法的具体用法?C# SslStream.Read怎么用?C# SslStream.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SslStream
的用法示例。
在下文中一共展示了SslStream.Read方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SslStream_StreamToStream_Successive_ClientWrite_Sync_Success
public void SslStream_StreamToStream_Successive_ClientWrite_Sync_Success()
{
byte[] recvBuf = new byte[_sampleMsg.Length];
VirtualNetwork network = new VirtualNetwork();
using (var clientStream = new VirtualNetworkStream(network, isServer: false))
using (var serverStream = new VirtualNetworkStream(network, isServer: true))
using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
using (var serverSslStream = new SslStream(serverStream))
{
bool result = DoHandshake(clientSslStream, serverSslStream);
Assert.True(result, "Handshake completed.");
clientSslStream.Write(_sampleMsg);
serverSslStream.Read(recvBuf, 0, _sampleMsg.Length);
Assert.True(VerifyOutput(recvBuf, _sampleMsg), "verify first read data is as expected.");
clientSslStream.Write(_sampleMsg);
serverSslStream.Read(recvBuf, 0, _sampleMsg.Length);
Assert.True(VerifyOutput(recvBuf, _sampleMsg), "verify second read data is as expected.");
}
}
示例2: SslStream_StreamToStream_Successive_ClientWrite_Success
public void SslStream_StreamToStream_Successive_ClientWrite_Success()
{
byte[] recvBuf = new byte[sampleMsg.Length];
MockNetwork network = new MockNetwork();
using (var clientStream = new FakeNetworkStream(false, network))
using (var serverStream = new FakeNetworkStream(true, network))
using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
using (var serverSslStream = new SslStream(serverStream))
{
bool result = DoHandshake(clientSslStream, serverSslStream);
Assert.True(result, "Handshake completed.");
clientSslStream.Write(sampleMsg);
serverSslStream.Read(recvBuf, 0, sampleMsg.Length);
clientSslStream.Write(sampleMsg);
// TODO Test Issue #3802
// The condition on which read method (UpdateReadStream) in FakeNetworkStream does a network read is flawed.
// That works fine in single read/write but fails in multi read write as stream size can be more, but real data can be < stream size.
// So I am doing an explicit read here. This issue is specific to test only & irrespective of xplat.
serverStream.DoNetworkRead();
serverSslStream.Read(recvBuf, 0, sampleMsg.Length);
Assert.True(VerifyOutput(recvBuf, sampleMsg), "verify second read data is as expected.");
}
}
示例3: RoundDownToNearest
public Double RoundDownToNearest()
{
string username = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes("utrnclin11"));
string password = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes("Po1Da7si"));
TcpClient tcpclient = new TcpClient();
tcpclient.Connect("mail.marvin-soft.de", 587);
System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());
//sslstream.AuthenticateAsClient("mail.marvin-soft.de");
// sslstream.LocalCertificate.Import(
bool flag = sslstream.IsAuthenticated;
byte[] read = new byte[10024];
// sslstream.Write(System.Text.Encoding.ASCII.GetBytes("yourYahooID"), 0, System.Text.Encoding.ASCII.GetBytes("satalaj").Length);
System.IO.StreamWriter sw = new StreamWriter(sslstream);
sw.WriteLine("EHLO mail.marvin-soft.de");
sw.WriteLine("AUTH LOGIN");
sw.WriteLine("utrnclin11");
//sw.Flush();
//
sw.WriteLine("Po1Da7si");
sw.Flush();
System.Threading.Thread.Sleep(2000);
sslstream.Read(read, 0, 1024);
System.Threading.Thread.Sleep(3000);
string str = System.Text.Encoding.ASCII.GetString(read);
// System.Threading.Thread.Sleep(2000);
sw.WriteLine("Mail From:<[email protected]>");
//sw.Flush();
sw.WriteLine("RCPT TO:<[email protected]>");
//sw.Flush();
sw.WriteLine("DATA ");
//sw.Flush();
sw.WriteLine("This is test message from marvin-soft");
sw.WriteLine(".");
//sw.WriteLine("quit");
sw.Flush();
sslstream.Read(read, 0, 10024);
System.Threading.Thread.Sleep(3000);
str = System.Text.Encoding.ASCII.GetString(read);
return 2.5;
}
示例4: ReadMessage
static string ReadMessage(SslStream sslStream, NetworkStream client)
{
byte[] buffer = new byte[2048];
StringBuilder messageData = new StringBuilder();
//Debug.Log(client.DataAvailable);
if (!client.DataAvailable)
return null;
//if (!client.CanRead)
// return null;
int bytes = sslStream.Read(buffer, 0, buffer.Length);
//Decoder d = Encoding.UTF8.GetDecoder();
//char[] cs = new char[d.GetCharCount(buffer, 0, bytes)];
//d.GetChars(buffer, 0, bytes, cs, 0);
//string test = "";
//for (int i = 0; i < cs.Length; i++)
//{
// test += cs[i];
//}
string data = "";
Decoder decoder = Encoding.UTF8.GetDecoder();
char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
decoder.GetChars(buffer, 0, bytes, chars, 0);
for (int i = 0; i < chars.Length; i++)
{
data += chars[i];
}
//if (messageData.ToString().IndexOf("<EOF>") != -1)
//{
// break;
//}
if (data.Trim() == "")
return null;
return data.Trim();
}
示例5: ReadMessage
static string ReadMessage(SslStream sslStream, NetworkStream client)
{
byte[] buffer = new byte[2048];
StringBuilder messageData = new StringBuilder();
if (!client.DataAvailable)
return null;
if (!client.CanRead)
return null;
int bytes = sslStream.Read(buffer, 0, buffer.Length);
//if(bytes == 0)
//{
// return null;
//}
while (client.DataAvailable)
{
Decoder decoder = Encoding.UTF8.GetDecoder();
char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
decoder.GetChars(buffer, 0, bytes, chars, 0);
messageData.Append(chars);
//if (messageData.ToString().IndexOf("<EOF>") != -1)
//{
// break;
//}
if (client.CanRead)
{
bytes = sslStream.Read(buffer, 0, buffer.Length);
}
else
{
bytes = 0;
}
}
return messageData.ToString();
}
示例6: SslStream_StreamToStream_LargeWrites_Sync_Success
public void SslStream_StreamToStream_LargeWrites_Sync_Success(bool randomizedData)
{
VirtualNetwork network = new VirtualNetwork();
using (var clientStream = new VirtualNetworkStream(network, isServer:false))
using (var serverStream = new VirtualNetworkStream(network, isServer:true))
using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
using (var serverSslStream = new SslStream(serverStream))
{
Assert.True(DoHandshake(clientSslStream, serverSslStream), "Handshake complete");
byte[] largeMsg = new byte[4096 * 5]; // length longer than max read chunk size (16K + headers)
if (randomizedData)
{
new Random().NextBytes(largeMsg); // not very compressible
}
else
{
for (int i = 0; i < largeMsg.Length; i++)
{
largeMsg[i] = (byte)i; // very compressible
}
}
byte[] receivedLargeMsg = new byte[largeMsg.Length];
// First do a large write and read blocks at a time
clientSslStream.Write(largeMsg);
int bytesRead = 0, totalRead = 0;
while (totalRead < largeMsg.Length &&
(bytesRead = serverSslStream.Read(receivedLargeMsg, totalRead, receivedLargeMsg.Length - totalRead)) != 0)
{
totalRead += bytesRead;
}
Assert.Equal(receivedLargeMsg.Length, totalRead);
Assert.Equal(largeMsg, receivedLargeMsg);
// Then write again and read bytes at a time
clientSslStream.Write(largeMsg);
foreach (byte b in largeMsg)
{
Assert.Equal(b, serverSslStream.ReadByte());
}
}
}
示例7: SslStream_StreamToStream_LargeWrites_Sync_Success
public void SslStream_StreamToStream_LargeWrites_Sync_Success()
{
MockNetwork network = new MockNetwork();
using (var clientStream = new FakeNetworkStream(false, network))
using (var serverStream = new FakeNetworkStream(true, network))
using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
using (var serverSslStream = new SslStream(serverStream))
{
Assert.True(DoHandshake(clientSslStream, serverSslStream), "Handshake complete");
byte[] largeMsg = Enumerable.Range(0, 4096 * 5).Select(i => (byte)i).ToArray();
byte[] receivedLargeMsg = new byte[largeMsg.Length];
// First do a large write and read blocks at a time
clientSslStream.Write(largeMsg);
int bytesRead = 0, totalRead = 0;
while (totalRead < largeMsg.Length &&
(bytesRead = serverSslStream.Read(receivedLargeMsg, totalRead, receivedLargeMsg.Length - totalRead)) != 0)
{
totalRead += bytesRead;
}
Assert.Equal(receivedLargeMsg.Length, totalRead);
Assert.Equal(largeMsg, receivedLargeMsg);
// Then write again and read bytes at a time
clientSslStream.Write(largeMsg);
foreach (byte b in largeMsg)
{
Assert.Equal(b, serverSslStream.ReadByte());
}
}
}
示例8: SslStream_StreamToStream_Successive_ClientWrite_WithZeroBytes_Success
public void SslStream_StreamToStream_Successive_ClientWrite_WithZeroBytes_Success()
{
byte[] recvBuf = new byte[_sampleMsg.Length];
VirtualNetwork network = new VirtualNetwork();
using (var clientStream = new VirtualNetworkStream(network, isServer: false))
using (var serverStream = new VirtualNetworkStream(network, isServer: true))
using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
using (var serverSslStream = new SslStream(serverStream))
{
bool result = DoHandshake(clientSslStream, serverSslStream);
Assert.True(result, "Handshake completed.");
clientSslStream.Write(Array.Empty<byte>());
clientSslStream.WriteAsync(Array.Empty<byte>(), 0, 0).Wait();
clientSslStream.Write(_sampleMsg);
int bytesRead = 0;
while (bytesRead < _sampleMsg.Length)
{
bytesRead += serverSslStream.Read(recvBuf, bytesRead, _sampleMsg.Length - bytesRead);
}
Assert.True(VerifyOutput(recvBuf, _sampleMsg), "verify first read data is as expected.");
clientSslStream.Write(_sampleMsg);
clientSslStream.WriteAsync(Array.Empty<byte>(), 0, 0).Wait();
clientSslStream.Write(Array.Empty<byte>());
bytesRead = 0;
while (bytesRead < _sampleMsg.Length)
{
bytesRead += serverSslStream.Read(recvBuf, bytesRead, _sampleMsg.Length - bytesRead);
}
Assert.True(VerifyOutput(recvBuf, _sampleMsg), "verify second read data is as expected.");
}
}
示例9: TestSyncAdvanced
public void TestSyncAdvanced()
{
IPEndPoint ep = null;
var evtReady = new AutoResetEvent(false);
var serverTask = Task.Factory.StartNew(() =>
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start(5);
ep = (IPEndPoint)listener.LocalEndpoint;
evtReady.Set();
Console.WriteLine("Server> waiting for accept");
using (var tcp = listener.AcceptTcpClient())
using (var sslStream = new SslStream(tcp.GetStream(), false, ValidateRemoteCert))
{
Console.WriteLine("Server> authenticate");
sslStream.AuthenticateAsServer(
_ctx.ServerCertificate,
true,
_ctx.CAChain,
SslProtocols.Tls,
SslStrength.All,
true
);
Console.WriteLine("Server> CurrentCipher: {0}", sslStream.Ssl.CurrentCipher.Name);
Assert.AreEqual("AES256-GCM-SHA384", sslStream.Ssl.CurrentCipher.Name);
Assert.IsTrue(sslStream.IsMutuallyAuthenticated);
Console.WriteLine("Server> rx msg");
var buf = new byte[256];
sslStream.Read(buf, 0, buf.Length);
Assert.AreEqual(clientMessage.ToString(), buf.ToString());
Console.WriteLine("Server> tx msg");
sslStream.Write(serverMessage, 0, serverMessage.Length);
Console.WriteLine("Server> done");
}
listener.Stop();
});
var clientTask = Task.Factory.StartNew(() =>
{
evtReady.WaitOne();
Console.WriteLine("Client> Connecting to: {0}:{1}", ep.Address, ep.Port);
using (var tcp = new TcpClient(ep.Address.ToString(), ep.Port))
using (var sslStream = new SslStream(
tcp.GetStream(),
false,
ValidateRemoteCert,
SelectClientCertificate))
{
Console.WriteLine("Client> authenticate");
sslStream.AuthenticateAsClient(
"localhost",
_ctx.ClientCertificateList,
_ctx.CAChain,
SslProtocols.Tls,
SslStrength.All,
true
);
Console.WriteLine("Client> CurrentCipher: {0}", sslStream.Ssl.CurrentCipher.Name);
Assert.AreEqual("AES256-GCM-SHA384", sslStream.Ssl.CurrentCipher.Name);
Assert.IsTrue(sslStream.IsMutuallyAuthenticated);
Console.WriteLine("Client> tx msg");
sslStream.Write(clientMessage, 0, clientMessage.Length);
Console.WriteLine("Client> rx msg");
var buf = new byte[256];
sslStream.Read(buf, 0, buf.Length);
Assert.AreEqual(serverMessage.ToString(), buf.ToString());
Console.WriteLine("Client> done");
}
});
Task.WaitAll(clientTask, serverTask);
}
示例10: TestSyncIntermediate
public void TestSyncIntermediate()
{
IPEndPoint ep = null;
var evtReady = new AutoResetEvent(false);
var serverTask = Task.Factory.StartNew(() =>
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start(5);
ep = (IPEndPoint)listener.LocalEndpoint;
evtReady.Set();
Console.WriteLine("Server> waiting for accept");
using (var tcp = listener.AcceptTcpClient())
using (var sslStream = new SslStream(tcp.GetStream()))
{
Console.WriteLine("Server> authenticate");
sslStream.AuthenticateAsServer(
_ctx.ServerCertificate,
false,
null,
SslProtocols.Default,
SslStrength.Low,
false
);
Console.WriteLine("Server> CurrentCipher: {0}", sslStream.Ssl.CurrentCipher.Name);
Assert.AreEqual("DES-CBC-SHA", sslStream.Ssl.CurrentCipher.Name);
Console.WriteLine("Server> rx msg");
var buf = new byte[256];
sslStream.Read(buf, 0, buf.Length);
Assert.AreEqual(clientMessage.ToString(), buf.ToString());
Console.WriteLine("Server> tx msg");
sslStream.Write(serverMessage, 0, serverMessage.Length);
Console.WriteLine("Server> done");
}
listener.Stop();
});
var clientTask = Task.Factory.StartNew(() =>
{
evtReady.WaitOne();
Console.WriteLine("Client> Connecting to: {0}:{1}", ep.Address, ep.Port);
using (var tcp = new TcpClient(ep.Address.ToString(), ep.Port))
using (var sslStream = new SslStream(tcp.GetStream()))
{
Console.WriteLine("Client> authenticate");
sslStream.AuthenticateAsClient(
"localhost",
null,
null,
SslProtocols.Default,
SslStrength.Low,
false
);
Console.WriteLine("Client> CurrentCipher: {0}", sslStream.Ssl.CurrentCipher.Name);
Assert.AreEqual("DES-CBC-SHA", sslStream.Ssl.CurrentCipher.Name);
Console.WriteLine("Client> tx msg");
sslStream.Write(clientMessage, 0, clientMessage.Length);
Console.WriteLine("Client> rx msg");
var buf = new byte[256];
sslStream.Read(buf, 0, buf.Length);
Assert.AreEqual(serverMessage.ToString(), buf.ToString());
Console.WriteLine("Client> done");
}
});
serverTask.Wait();
clientTask.Wait();
}