2
我正在处理一个C#应用程序,它在网络实例之间传递字符串。从套接字缓冲字符串流
该代码当前正在使用异步套接字,并且到目前为止一切正常(localhost)。
但是,我期待缓冲问题,当数据包将被拆分和合并时,我会经历真正的接口。
客户:
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);
}
位于此处的msdn示例(http://msdn.microsoft.com/en-us/library/fx6588te%28v=vs.90%29.aspx)看起来好像可能有所帮助 - 检查ReadCallbackMethod – Johnv2020
@ Johnv2020这让我意识到Encoding.GetBytes不会附加或预先加载字符串终止数据,所以没有办法通过该协议来完成。 –