使用套接字传输二进制数据?

发布于 2024-11-09 02:59:29 字数 688 浏览 0 评论 0原文

我正在考虑制作一个服务器/客户端应用程序来执行此操作:客户端将连接到服务器,服务器将发回客户端能够流式传输或下载的文件夹列表(可能是音乐或视频) 。我将为此制作一个 GUI,这样它就不会是一个纯文本界面。我希望这样当客户端连接到服务器并获取有关可以流式传输/下载的媒体文件的信息时,客户端将在类似资源管理器的界面中显示文件夹/文件(文件夹将显示一个文件夹图标、视频将显示视频图标等。)

这对我来说似乎真的很大,但我真的很想了解更多有关使用 C# 进行套接字编程的信息,并且觉得亲自动手是最好的方法。正如你可以想象的那样,我对如何开始这项工作以及我需要做什么才能使这项工作顺利进行感到很困惑。

我的问题的第一部分是:如果我想让客户端接收文件/文件夹列表并且还能够下载/流式传输它们,我是否需要使用套接字传输二进制数据做一些事情,或者我完全错了关于那个?如果我错了,我是否需要使用其他东西(例如文件读取器)并以某种方式通过套接字使用它们?

我的问题的第二部分是:当我将此信息传输给客户端时,如何使客户端显示视频、文件夹、mp3 等的适当图标?我在想,如果我必须传输二进制数据,我将不得不使用该数据以某种方式用正确的图标/数据填充客户端 GUI。我完全不知道如何处理这个问题。是否有某些方法/类可以用来解析该数据并执行我想要执行的操作?再说一次,我不知道我是否需要传输二进制数据是错误的,但无论哪种方式,我都对处理这个问题所必需的内容感到困惑。

如果我的术语有误,我深表歉意。我显然还不是 C# 专家;)

I am thinking of making a server/client application that does this: the client(s) will connect to the server and the server will send back a list of folders (probably music or videos) that the client will be able to stream or download. I am going to make a GUI for this so that it won't be a text-only interface. I want it so that when the client connects to the server and gets the information about the media files that can be streamed/downloaded, the client will show the folders/files in an explorer-like interface (folders will show up with a folder icon, videos will show up with a video icon, etc.)

This seems really big to me but I really want to learn more about socket programming with C# and feel that getting my hands really dirty is the best way. As you can imagine, I have a lot of confusion about how to start this and what it is that I need to do to make this work.

The first part of my question is: If I want to let a client receive a list of files/folders and also have the ability to download/stream them, do I need to do something with transferring binary data using sockets or am I completely wrong on that? If I'm wrong, do I need to use something else like file readers and somehow use them over sockets?

The second part to my question is: when I transfer this information over to a client, how can I make the client show the appropriate icons for videos, folders, mp3s, etc.? I'm thinking that if I have to transfer binary data I will have to use that data to somehow populate the client GUI with the correct icons/data. I am stuck on exactly how to handle that. Are there certain methods/classes that I can use to parse that data and do what I want to do with it? Again, I don't know if I'm wrong about needing to transfer binary data but either way I am confused on what it is that's necessary to handle this.

I apologize if any of my terminology is wrong. I am obviously not an expert in C# yet ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你丑哭了我 2024-11-16 02:59:29

每当您在机器之间(或进程之间)发送数据时,您就已经在谈论二进制数据(您可能会说一切都是二进制数据,但是当将其作为面向对象模型使用时,它并不感觉像这样)。这里的要点是,您需要做的就是序列化您的消息。有无数的序列化框架 - 有些旨在方便,有些旨在功能范围,有些可以针对性能(最小带宽等)。

即使内置的也可以工作 - 例如,您可以使用 XmlSerializer 等写入 Stream (例如您通常使用的 NetworkStream一个插座)。但请不要使用 BinaryFormatter (老实说,这会伤害你)。就我个人而言,我会使用一些不太冗长的东西,例如 protobuf-net,但自从我写了它以来,我有点偏见;p

但是,请注意,在使用套接字时,您通常会发送多个(单独的)消息作为对话。为了使每一端都能正确、方便地从流中读取每条消息的正确数据,通常为每条消息添加长度(字节数)前缀 - 例如,作为一个小 - endian int 占用固定的 4 个字节。所以读取过程是:

  • 读取 4 个字节
  • 将其解释为 int,比方说 n
  • 读取 n 字节
  • 通过解串器运行该数据,也许通过MemoryStream
  • 处理消息

,发送过程可能是:

  • 序列化消息,也许到MemoryStream
  • 查询长度并形成4字节前缀
  • 写入前缀
  • 写入数据

Any time you are sending data between machines (or between processes), you are already talking binary data (you could argue that everything is binary data, but when working with it as an OO model it doesn't feel like it). The point here is that all you need to do is serialize your messages. There are a myriad of serialization frameworks - some aimed at convenience, some aimed to range-of-features, and som can use aimed at performance (minimum bandwidth etc).

Even the inbuilt ones would work - for example, you can use XmlSerializer etc to write to a Stream (such as the NetworkStream you typically use with a socket). But please - not BinaryFormatter (it will hurt you, honest). Personally I'd use something a bit less verbose such as protobuf-net, but I'm somewhat biased since I wrote it ;p

However, note that when working with sockets, you are typically sending multiple (separate) messages as part of the conversation. In order for each end to correctly and conveniently read the right data per-message from the stream, it is common to prefix each message with the length (number of bytes) - for example, as a little-endian int taking a fixed 4 bytes. So the reading process is:

  • read 4 bytes
  • interpret that as an int, let's say n
  • read n bytes
  • run that data through your deserializer, perhaps via MemoryStream
  • process the message

and the sending process might be:

  • serialize the message, perhaps to a MemoryStream
  • query the length and form the 4 byte prefix
  • write the prefix
  • write the data
还给你自由 2024-11-16 02:59:29

当然,你不必达到像 Socket 这样低的水平,除非你只是想学习。

我建议使用WCF。它将完全满足您的要求,并且非常灵活,您不必直接处理套接字。

WCF是一个很大的框架,但是你可以很快学会如何使用它。

WCF 简介

在这里您可以找到如何在 Windows 中检索和显示文件图标。
如果您的客户端由Windows托管,则不需要传输图标,您只需按文件名/扩展名检索客户端上的图像即可。

You don't have to go to as low level as Socket, unless you just want to learn of course.

I suggest using WCF. It will completely meet your requirements, will be flexible and you will not have to deal with sockets directly.

WCF is a big framework, but you can quickly learn how to use it.

Introduction to WCF

Here you can find how to retrieve and show file icon in Windows.
If you client is hosted by Windows, you don't need to transfer icons, you can just retrieve image on the client by filename/extension.

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