通过套接字编程发送图像文件(jpeg、png)的提示/示例?

发布于 2024-08-12 08:17:35 字数 260 浏览 5 评论 0原文

我听说我们可以通过套接字发送二进制图像文件...... 但我不知道如何将图像文件转换为二进制文件,或者甚至不知道如何考虑通过套接字发送它......

希望有人可以发布一个简单的例子吗?或者为我指明正确的方向:) 我也使用 QT 来实现我的 gui,但不使用 QT 套接字编程。

非常感谢:DI 真的很感激


问题@djc:

如何获取图像的目录路径,并以某种方式在该图像上使用发送命令?我基本上使用C++。但这也是我有一段时间的疑问。

I've heard that we can somehow send an image file with binary over a socket...
But I have no idea on how to convert an image file into binary or how to even think of sending it over a socket...

Was hoping if someone could post a simple example? or point me in the right direction :) I am also using QT for just my gui, but not using QT socket programming.

Thanks so much :D I really appreciate it


Question @ djc:

How would you get the directory path for an image, and somehow use the send command on that image? I'm basically using C++. But this is also a question I've had for awhile.

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

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

发布评论

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

评论(2

葬花如无物 2024-08-19 08:17:36

您拥有的任何图像文件都已经是二进制的。您可以通过套接字发送它们。

Any image files you have are already binary. You can just send them over the socket.

櫻之舞 2024-08-19 08:17:36

您需要知道或让用户告诉您可以找到图像文件的路径。

一旦完成,您就可以逻辑地打开文件,将其读入缓冲区,然后通过套接字写入该缓冲区,最后关闭文件(始终关闭您打开的文件并释放您分配的文件)。但是,还有一些细节需要排序 - 例如接收端如何知道后面的数据是图像以及它有多大(因此它知道您何时发送了全部数据)。您的协议可能会定义一个位模式(一个或两个字节),将消息标识为图像,然后可能使用四个字节来指定图像的大小,后跟正确的字节数。您可以使用基于 POSIX 的 stat() 系统调用来查找文件的大小。或者,您可以发送一系列包含图像部分的数据包(再次使用类型 - 这次是“图像数据包”类型而不是“图像”类型)加上数据包的长度(可能只是 16 位)无符号整数,最大大小为 65535 字节),加上最后一段的“结束图像数据包”。这对于发件人来说可能更容易;如果数据直接写入文件,对于接收者来说很容易,但如果接收者需要内存中的图像,则很混乱。

You will need to know, or have the user tell you, a path that will find the image file.

Once you have that, then you logically open the file, read it into a buffer, and then write that buffer over the socket, and finally close the file (always close what you open and free what you allocate). However, there are details to be sorted - like how does the receiving end know that the data that follows is an image and how big it is (so it knows when you've sent it all). Your protocol will, presumably, define a bit pattern (one or two bytes) that identifies the message as an image, and then probably use four bytes to specify the size of the image, followed by the correct number of bytes. You can find the size of a file using the POSIX-based stat() system call. Alternatively, you can send a series of packets containing parts of the image (again with a type - this time, of type 'image packet' instead of 'image') plus the length of the packet (which might only be a 16-bit unsigned integer, for a maximum size of 65535 bytes), plus an 'end image packet' for the last segment. This is perhaps easier for the sender; it is easy for the receiver if the data goes direct to file, but messy if the receiver needs the image in memory.

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