gpt4 book ai didi

c++ - 我无法在基于 C 的代理中显示图像

转载 作者:行者123 更新时间:2023-11-28 07:07:32 26 4
gpt4 key购买 nike

所以我正在尝试创建一个基于 C 的代理系统,但我在读取通过连接的图像数据时遇到了问题。大多数情况下我可以正常访问页面,但是当我尝试将图像数据从服务器发送回浏览器时,我看不到图像。下面是用于从服务器接收数据然后将消息发送到浏览器的代码。我希望有人能提供帮助我的线索,谢谢! *代理阻止显示奇数字节内容,这是那里代码的一部分,但我要加载的图像是偶数字节数。

    do {

//Receive message from server
bytes = recv(clientsockfd, messagein, MAX_MESSAGE_LENGTH, 0);

if(bytes > 0)
strncpy(messageout,messagein,bytes);

else if(bytes == 0) {
printf("\nConnection closed\n");
strcpy(messageout,"HTTP/1.1 200 OK\r\n\r\n");
}

printf("Answer received from server: \n");
printf("%s\n\nTotal Bytes: %d\n\n", messagein, bytes);

//Copy the messagein to a tempStr so we can leave original message untouched
strncpy(tempStr,messagein,bytes);

//Check to see if the content is even or odd only if the message in has a content length field, later we will determine if we do anything with it
if(strstr(messagein,"Content-Length:") != NULL)
evenContent = checkContent(tempStr);

//If the content is odd then we want to change the message we'll send back to the browser
if(!evenContent) {
//If the page is odd then I redirect the user to carey's error page he made
if(strstr(messagein,"text/html") != NULL)
strcpy(messageout,"HTTP/1.1 302 Found\r\nLocation: http://pages.cpsc.ucalgary.ca/~carey/CPSC441/errorpage.html\r\n\r\n");
else if(strstr(messagein,"image/") != NULL) {
//If the image is odd then I just send back empty data to the browser
strcpy(messageout,"HTTP/1.1 200 OK\r\n\r\n");
}
}
#ifdef DEBUG
printf("\n\nAbout to send message to the web browser:\n%s",messageout);
#endif
/* send the result message back to the browser */
send(childsockfd, messageout, bytes, 0);

/* initialize message strings just to be safe (null-terminated) */
for( i = 0; i < MAX_MESSAGE_LENGTH; i++ )
{
messagein[i] = '\0';
messageout[i] = '\0';
tempStr[i] = '\0';
}

} while (bytes != 0);

最佳答案

您在这里假设 header 和内容是通过一次调用 recv 一起获得的。但对于可能不真实的大请求(图像通常很大)。要进行正确的解析,您应该调用 recv 直到您解析了包含 content-length 的 header ,然后调用 recv 直到您收到了 content-length 字节数。

关于c++ - 我无法在基于 C 的代理中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21558150/

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