创建一个“监听器”对于将包含 byte[] 数组的传入套接字流
我正在创建一个套接字服务器,它需要不断侦听来自连接的客户端的传入消息。这些消息将以 byte[] 数组的形式发送。我让服务器与 StreamReader 配合得很好,但 StreamReader 只适用于正在发送的数据的文本表示......而不是 byte[] 数组。
这就是我所拥有的:
StreamReader reader = new StreamReader(Client.GetStream());
string line = "";
while (true)
{
line = reader.ReadLine();
if (!string.IsNullOrEmpty(line))
{
parentForm.ApplyText(line + "\r\n");
SocketServer.SendBroadcast(line);
}
}
我现在需要以某种方式将其转换为原始流,从而将流内容转换为 byte[] 数组,但我似乎无法处理它。
我尝试了这个:
while (true)
{
var bytes = default(byte[]);
using (var memstream = new MemoryStream())
{
var buffer = new byte[512];
var bytesRead = default(int);
while ((bytesRead = reader.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
memstream.Write(buffer, 0, bytesRead);
bytes = memstream.ToArray();
}
//parentForm.ApplyText(bytes.Length + "\r\n");
}
但正如您可能猜到的那样, while(true) 循环并不能完全按照我需要的方式工作。任何人都可以帮助我进行一些代码调整,以使这项工作按照我的需要进行。它需要持续侦听传入的消息,然后当收到消息时,它需要对该消息(byte[] 数组)执行某些操作,然后再次返回侦听。
TIA
I'm creating a socket server that needs to continuously listen for incoming messages from the connected clients. Those messages will be sent in a byte[] array. I had the server working great with a StreamReader but StreamReader only works with textual represenations of the data being sent...not byte[] arrays.
Here's what I had:
StreamReader reader = new StreamReader(Client.GetStream());
string line = "";
while (true)
{
line = reader.ReadLine();
if (!string.IsNullOrEmpty(line))
{
parentForm.ApplyText(line + "\r\n");
SocketServer.SendBroadcast(line);
}
}
I need to now convert that into a raw stream somehow that will convert the stream contents into a byte[] array but I can't seem to get a handle on it.
I tried this:
while (true)
{
var bytes = default(byte[]);
using (var memstream = new MemoryStream())
{
var buffer = new byte[512];
var bytesRead = default(int);
while ((bytesRead = reader.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
memstream.Write(buffer, 0, bytesRead);
bytes = memstream.ToArray();
}
//parentForm.ApplyText(bytes.Length + "\r\n");
}
but as you might guess, the while(true) loop doesn't quite work how I need it to. Can anyone help me with some code adjustment to make this work as I need it to. It needs to continuously listen for incoming messages, then when a message is received, it needs to do something with that message (the byte[] array) then go back to listening again.
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想“持续倾听”不是读者的任务,而是听众的任务。当我使用 TcpListener 编写服务器时,我遇到了同样的问题。我不确定你想做什么,但我正在发布你的“连续监听”和读入 byte[] 问题的解决方案。我想这段代码可能对你有帮助:
I guess "listening continuously" is not task of reader its a task of listener. I ran into same problem when i was writing server using TcpListener. I am not sure what you want to do but i am posting solution for your "listening continuous" and reading into byte[] problem. I guess this code might help you:
不需要转换任何东西。
GetStream()
返回一个 NetworkStream。请参阅 Microsoft 在 NetworkStream.Read 方法中包含的示例。您所要做的就是将myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
行替换为适当的存储机制。There's no need to convert anything.
GetStream()
returns a NetworkStream. See the sample Microsoft includes in the NetworkStream.Read Method. All you have to do is replace themyCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
line with an appropriate storage mechanism.