gpt4 book ai didi

c# - 将图像从 C# 客户端发送到 C 服务器

转载 作者:太空宇宙 更新时间:2023-11-04 02:14:05 24 4
gpt4 key购买 nike

如果我发送纯文本就没有问题。一切都好。但是,如果我尝试从 C# 客户端发送图像,服务器会收到正确的字节数,但当我将缓冲区保存到文件(二进制模式 - wb)时,它总是有 4 个字节。

我通过 C# 客户端使用函数 File.ReadAllBytes() 发送它。

我的保存代码看起来像

   FILE * pFile;
char *buf = ReceiveMessage(s);
pFile = fopen (fileName , "wb");
fwrite(buf, sizeof(buf[0]), sizeof(buf)/sizeof(buf[0]), pFile);
fclose (pFile);
free(buf);

我的接收函数是这样的

static unsigned char *ReceiveMessage(int s)
{
int prefix;
recv(s, &prefix, 4, 0);
int len = prefix;
char *buffer= (char*)malloc(len + 1);
int received = 0, totalReceived = 0;
buffer[len] = '\0';
while (totalReceived < len)
{
if (len - totalReceived > BUFFER_SIZE)
{
received = recv(s, buffer + totalReceived, BUFFER_SIZE, 0);
}
else
{
received = recv(s, buffer + totalReceived, len - totalReceived, 0);
}
totalReceived += received;
}

return buffer;
}

最佳答案

您的 C 代码需要从 ReceiveMessage() 函数传回 len

char *buf = ReceiveMessage(s);  // buf is a char*
... sizeof(buff) // sizeof(char*) is 4 or 8

所以你需要类似的东西

 static unsigned char *ReceiveMessage(int s, int* lenOut)
{
...
*lenOut = totalReceived ;
}

关于c# - 将图像从 C# 客户端发送到 C 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10142757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com