- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一个通过 SslStream 进行通信的 TcpClient 和服务器。通信有效,但是当我从客户端向服务器发送消息时,服务器首先读取 1 个字节,然后在下一步中读取其余部分。例子:我想通过Client发送“test”,Server先收到“t”,再收到“est”
这里是客户端发送的代码
public void Send(string text) {
byte[] message = Encoding.UTF8.GetBytes(text);
SecureStream.BeginWrite(message, 0, message.Length, new AsyncCallback(WriteCallback), null);
}
private void WriteCallback(IAsyncResult AR) {
}
这里是服务器用来读取的代码
private SslStream CryptedStream = ...;
private byte[] buffer = new byte[1024];
public void BeginReadCallback(IAsyncResult AsyncCall) {
// initialize variables
int bytesRead = 0;
try {
// retrieve packet
bytesRead = CryptedStream.EndRead(AsyncCall);
// check if client has disconnected
if (bytesRead > 0) {
// copy buffer to a temporary one
var temporaryBuffer = buffer;
Array.Resize(ref temporaryBuffer, bytesRead);
string read = Encoding.ASCII.GetString(temporaryBuffer);
SetText(read);
// read more data
CryptedStream.BeginRead(buffer, 0, 1024, new AsyncCallback(BeginReadCallback), null);
// client is still connected, read data from buffer
//ProcessPacket(temporaryBuffer, temporaryBuffer.Length, helper);
} else {
// client disconnected, do everything to disconnect the client
//DisconnectClient(helper);
}
} catch (Exception e) {
// encountered an error, closing connection
// Program.log.Add(e.ToString(), Logger.LogLevel.Error);
// DisconnectClient(helper);
}
}
我错过了什么吗?感谢您的帮助
最佳答案
正如 Lasse 所解释的,流式 API 不 promise 您每次读取返回特定数量的字节。
最好的解决方法是不使用套接字。使用更高级别的 API,例如 WCF、SignalR、HTTP...
如果您坚持,您可能应该使用 BinaryReader/Writer
来发送您的数据。这使得它很容易。例如,它内置了字符串发送功能。您还可以使用这些类轻松地手动添加长度前缀。
您可能不需要异步 IO,也不应该使用它。如果您坚持,您至少可以通过使用 await
来摆脱回调。
关于c# - SslStream EndRead 获取第一个 1 字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36982469/
当远程客户端未发送任何内容时,我遇到了 SSLStream 返回一些数据的问题。当服务器正在监听新命令时,我遇到了这个问题。如果服务器没有收到新的请求,ReadMessage() 函数应该捕获一个 I
我最近在将套接字通信转换为使用 System.Net.Security.SslStream 而不是 NetworkStream 时遇到了一个问题。此转换针对 .Net Framework 4.8 项目
我正在尝试使用 SslStream 和 TLS 1.2 协议(protocol)与远程服务器建立 TCP 连接。代码如下: _tcpClient.Connect(endPoint); var cert
我正在尝试使用 TLS (openssl) 实现客户端-服务器应用程序。我按照 rust doc 中给出的示例作为我的代码结构:example 服务器代码 fn handle_client(mut s
当我尝试从 SslStream 函数读取时,如果我没有设置连接超时,Read() 永远不会结束,但如果我设置了连接超时异常。这家伙有同样的问题http://msdn.microsoft.com/en-
我正在尝试通过 C# .Net 应用程序中的 SslStream 连接到服务器。 当我尝试向服务器进行身份验证时,我在客户端收到一条错误消息,显示从传输流中收到了意外的 EOF 或 0 字节。 在服务
我用 C# 制作了一个简单的 ftp 客户端,它可以满足我的需要(连接到 ftp,可以选择使用代理),但我也希望能够使用 AUTH SSL。 因此,我查看了 SslStream 而不是 Network
我有一个多线程异步System.Net.Socket,它监听一个端口。如果我收到来自 HTTP 的请求,我没有任何问题,但最近我不得不向我的应用程序添加 https 支持。 客户给了我一个.arm认证
我正在尝试编写一个客户端-服务器库,让我的 LAN 计算机相互通信。由于这在很大程度上是一种自学尝试,因此我试图通过 SSL 建立这些连接。我的计划是维护一台主机,并让每个客户端维护一个到主机的相互验
各位,我正在尝试写一些关于 SSL 的文章,这里是问题: 我在下面构建了一些东西: CA 证书(自制 CA) Server pfx, Server cert, Server key(由自制的CA签发到
我正在尝试对存在证书交换的客户端进行身份验证。我遇到的问题是 openssl 客户端拒绝连接尝试,因为身份验证握手中没有包含任何证书颁发机构。 这对于在 Windows Server 2008 R2
我正在尝试连接到 SSL TCP 服务器,它可以在我的浏览器上运行,并且我可以收到硬编码的响应。在应用程序上,当使用 ReadLine 函数时抛出异常。 Error: System.ArgumentE
伙计们,我正在使用 SslStream 作为服务器来测试我的应用程序,但我在从流中读取时遇到了问题。我正在使用以下代码: while (true) {
我正在设计一个基于客户端服务器架构的系统 - 基于 TCP。要求服务器和客户端之间的所有消息都应该加密。所以我正在考虑在 .NET Framework 中使用 SslStream 类。 从 SslSt
我有这个代码: string certificateFilePath = @"C:\Users\Administrator\Documents\Certificate.pfx"; string cer
我看了很长时间的 MSDN 信息文章,但我仍然无法理解。 基于不需要客户端身份验证的假设: 1.当我调用SslStream.AuthenticateAsServer(...)时,我是在服务器端还是在客
我对处理套接字时“最好”使用什么感到困惑。 Socket 对象提供 Send/Receive 方法(和异步等效方法),但也允许创建 NetworkStream。我对使用 Socket.Send 感到高
我正在开发 C# SSL 客户端。 这是单向证书验证。我的客户需要进行服务器证书验证。 我已将 RemoteCertificateValidationCallback 添加到 SslStream。 目
我目前正在尝试使用 TcpClient 连接到 FTP 服务器。 为了使用 FTPS 作为协议(protocol),我通过 NetworkStream 处理一个单独的方法,该方法使用该方法创建一个 S
当使用 SSLStream 向(已经过身份验证的)客户端发送“大”数据 block (1 兆)时,我看到的数据包碎片/分解是 远大于使用普通 NetworkStream 时。 在客户端使用异步读取(即
我是一名优秀的程序员,十分优秀!