创建一个“监听器”对于将包含 byte[] 数组的传入套接字流

发布于 2025-01-02 07:28:41 字数 1300 浏览 1 评论 0原文

我正在创建一个套接字服务器,它需要不断侦听来自连接的客户端的传入消息。这些消息将以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

<逆流佳人身旁 2025-01-09 07:28:41

我想“持续倾听”不是读者的任务,而是听众的任务。当我使用 TcpListener 编写服务器时,我遇到了同样的问题。我不确定你想做什么,但我正在发布你的“连续监听”和读入 byte[] 问题的解决方案。我想这段代码可能对你有帮助:

TcpListener t = new TcpListener(IPAddress.Loopback, _port);
        t.Start();

        Console.WriteLine("Server is started and waiting for client\n\n");

        byte[] buff = new byte[255];
        NetworkStream stream;
        TcpClient client;
        while(true)
        {
            client = t.AcceptTcpClient();
            if (!client.Connected)
               return;

            stream = client.GetStream();
            while ((stream.Read(buff, 0, buff.Length)) != 0)
            {
                break;
            }

            if (0 != buff.Length)
                break;
        }  

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:

TcpListener t = new TcpListener(IPAddress.Loopback, _port);
        t.Start();

        Console.WriteLine("Server is started and waiting for client\n\n");

        byte[] buff = new byte[255];
        NetworkStream stream;
        TcpClient client;
        while(true)
        {
            client = t.AcceptTcpClient();
            if (!client.Connected)
               return;

            stream = client.GetStream();
            while ((stream.Read(buff, 0, buff.Length)) != 0)
            {
                break;
            }

            if (0 != buff.Length)
                break;
        }  
白云不回头 2025-01-09 07:28:41

不需要转换任何东西。 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 the myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead)); line with an appropriate storage mechanism.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文