如何通过将图像保存在 ANSI C 的字符字符串中来发送()图像?铸造问题
我正在用 ANSI C(在 Linux 上)编写自己的服务器,并且在将图像发送到 Web 浏览器时遇到一个问题。我使用函数“send()”来发送它,它需要一个 char *。所以我正在逐个字符地读取文件(使用 fgetc),我使用了 fread 和 fgets,但是将内容(大多数情况下为 int)转换为 bufor 时遇到了问题 - 仍然缺少一些字节(例如 2008 被发送) 2020 年)。我认为转换存在一些问题,但我使用了 sprintf 和 wchar_t 但它仍然无法正常工作。
我有一个函数:
void send_www(char *buff, int cd){
if(send (cd, buff, strlen(buff), 0) < 0) {
perror ("send()");
exit (1);
我在这里使用它:
// while (fread(znak, sizeof(char), i, handler) != 0) {
for (j = 0; j < i; j++) {
a = fgetc(handler);
sprintf(znak, "%ls", a);
send_www(znak, cd);
}
其中 i 是图像文件的长度,znak 是 char* (在此版本中为 wchar_t*)。您还可以查看我之前使用 fread 的尝试。
I'm writing my own server in ANSI C (on Linux) and I've got one problem with sending images to a web browser. I'm using the function "send()" to send it and it required a char *. So I was reading a file char by char (using fgetc), I used fread and fgets, but everything had a problem with casting the content(int in most cases) to a bufor - some bytes still were missing (e.g 2008 was send instead of 2020). I think there is some problem with conversion, but I used sprintf and wchar_t and it is still not working.
I've got a function:
void send_www(char *buff, int cd){
if(send (cd, buff, strlen(buff), 0) < 0) {
perror ("send()");
exit (1);
which I use here:
// while (fread(znak, sizeof(char), i, handler) != 0) {
for (j = 0; j < i; j++) {
a = fgetc(handler);
sprintf(znak, "%ls", a);
send_www(znak, cd);
}
where i is the length of the image file, znak is the char* (in this version wchar_t*). You can also see my earlier try of using fread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果没有更多细节,很难说 - 你能发布一些代码吗?
一件事是确保使用二进制选项打开图像文件,例如 fopen(filename, "rb");
如果您只是读取二进制文件并将其发送到另一端需要二进制文件的套接字,则不需要任何转换或 sprintf。
您似乎将字符类型与字符串混淆了。 AC 字符串是以零结尾的字符序列。您的图像文件是包含字符序列的二进制文件,但它不是字符串。 send() 函数采用指向字节序列的指针,而不是字符串。为了说明这一点,请看一下这个字符(或字节)序列:
在某些图像文件格式中,这可以是三个 RGB 像素,即白色、黑色、白色。 9 字节。可以保存在字符缓冲区[9]中。如果对此调用 strlen(),您将得到结果 3. strlen() 会一直计数,直到看到零字符。如果您从没有二进制标志的文件中读取这些字节,它可能会被转换,并且您可能会获得比文件中实际字节数更少或更多的字节(取决于内容、操作系统等)。
粗略的解释性代码如下:
要从二进制文件中读取数据并将数据发送到套接字,您需要执行类似于上面代码片段的操作。您可能会继续阅读直到到达文件末尾,该片段只读取一次。另外,在实际代码中,您应该检查实际发送了多少字节(在 send 的返回值中)并继续调用 send 直到发送完所有数据或发生错误。
It's hard to say without more details - can you post some code?
One thing is to make sure you open the image file with the binary option, e.g. fopen(filename, "rb");
If you're just reading a binary file and sending it to a socket where the other end is expecting binary you should not need any casting or sprintf.
You seem to be confusing char type with strings. A C string is a sequence of characters that is zero terminated. Your image file is a binary file containing a sequence of characters but it is not a string. The send() function takes a pointer to a sequence of bytes, not strings. To illustrate this look at this sequence of chars (or bytes):
In some image file formats this can be three RGB pixels, so white, black white. 9 bytes. Can be held in a char buffer[9] . If you call strlen() on this you will get the result 3. strlen() counts until it sees a zero character. If you read these bytes from a file without the binary flag it may get transformed and you may get less or more bytes than are really in the file (depending on the contents, the OS etc.).
Rough explanatory code follows:
To read from a binary file and send the data out to a socket you need to do something like the snippet above. You would probably keep reading until you reach the end of file, the snippet only reads once. Also in real code you should check to see how many bytes were actually sent (in the return value from send) and keep calling send until you've sent all the data or an error has occured.
您正在使用
strlen()
。strlen()
在达到\0
字节后停止并返回大小。图像和二进制数据在任何地方都允许有一个字节值为零,因此 strlen() 在这些情况下是无用的。您需要将您的函数修改为:
并将图像大小传递给它,以便您可以安全地
send()
它具有适当的大小。在您的情况下,您似乎确定它是一个
wchar_t
字符串,请使用适当的函数wcslen()
而不是strlen()
: )You are using
strlen()
.strlen()
stops and returns the size after it reaches a\0
byte. Images and binary data are allowed to have a byte valued zero anywhere, sostrlen()
is useless in those cases.You need to modify your function to:
And pass the image size to it, so you can safely
send()
it with the appropriate size.In your case, it seems you are sure it is a
wchar_t
string, use the appropriate function,wcslen()
, notstrlen()
:)我猜问题是您的图像数据包含字符 (
\0
),因此它们不会被发送,因为您使用strlen()
来计算输出要发送多少数据。I'd guess that the problem is that your image data contain characters (
\0
), so they aren't being sent because you're usingstrlen()
to figure out how much data to send.