gpt4 book ai didi

c# - 缓冲来自套接字的字符串流

转载 作者:太空宇宙 更新时间:2023-11-03 16:23:30 24 4
gpt4 key购买 nike

我正在开发一个在网络实例之间传递字符串的 C# 应用。

代码目前正在使用异步套接字,到目前为止一切正常(本地主机)。

但是,当我通过真实接口(interface)时,数据包将被拆分和合并时,我预计会出现缓冲问题。

客户:

Socket sock;
// Snip init and connect

string msg1 = "Hello\nWorld";
byte[] data1 = System.Text.Encoding.UTF8.GetBytes(msg1);
sock.Send(data1);

string msg2 = "Foo\nBar\nBaz";
byte[] data2 = System.Text.Encoding.UTF8.GetBytes(msg2);
sock.Send(data2);

我会选择这样的东西,但我想不出一个优雅的解决方案来解决缺失的问题:

服务器:

Socket sock;
MemoryStream ms = new MemoryStream();
Queue<string> strings = new Queue<string>();
// Snip init and receive connection
sock.BeginReceive(buffer, 0, MaxSize, SocketFlags.None, new AsyncCallback(OnReceived), null);

void OnReceived(IAsyncResult result) {
// Snip sanity stuff
int bytesReceived = sock.EndReceive(result);

// Here is where I'd need some help...
ms.Write(buffer, 0, bytesReceived);
ms.Flush();
for (;;) {
StreamReader sr = new StreamReader(ms);
if (sr.HasAStringTerminationCharacter) { // <-- How?
string currentString = sr.ReadUntilTermination(); // <-- How?
strings.Enqueue(currentString);
}
else
break;
}
sock.BeginReceive(buffer, 0, MaxSize, SocketFlags.None, new AsyncCallback(OnReceived), null);
}

最佳答案

由于 Encoding.GetBytes() 不会预先添加或附加字符串终止数据,因此我改用 BinaryReader/BinaryWriter,它在字符串内容之前对字符串长度进行编码。

这是修改后的客户端:

Socket sock;
// Snip init and connect
sock.Send(ToBinary("Hello\nWorld"));
sock.Send(ToBinary("Foo\nBar\nBaz"));

byte[] ToBinary(string s) {
var ms = new MemoryStream();
var bw = new BinaryWriter(ms);
bw.Write(s);
bw.Flush();
ms.Flush();
return ms.ToArray();
}

服务器:

Socket sock;
MemoryStream ms = new MemoryStream();
Queue<string> strings = new Queue<string>();
// Snip init and receive connection
sock.BeginReceive(buffer, 0, MaxSize, SocketFlags.None, new AsyncCallback(OnReceived), null);

void OnReceived(IAsyncResult result) {
// Snip sanity stuff
int bytesReceived = sock.EndReceive(result);

ms.Write(buffer, 0, bytesReceived);
ms.Flush();
long endPos = ms.Position;

ms.Seek(0, SeekOrigin.Begin);
long readPos = ms.Position;
var bw = new BinaryReader(ms);

for (;;) {
try {
string currentString = bw.ReadString();
strings.Enqueue(currentString);
readPos = stream.Position;
}
catch (EndOfStreamException) {
long unusedBytes = endPos - readPos;
var remaining = new MemoryStream();
remaining.Write(stream.GetBuffer(), (int) readPos, (int) unusedBytes);
ms = remaining;
break;
}
}
sock.BeginReceive(buffer, 0, MaxSize, SocketFlags.None, new AsyncCallback(OnReceived), null);
}

我相信它会正确处理拆分和合并的数据。

关于c# - 缓冲来自套接字的字符串流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13277617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com