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

标签 c++ c sockets proxy network-programming

所以我正在尝试创建一个基于 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/

相关文章:

c++ - 迭代器类型中使用的 vector 元素类型

c - 如何向菜单项添加工具提示

python - 通过 SWIG : can't get void** parameters to hold their value 从 C 到 Python

javascript - 在 laravel 5.2 中使用 socket.io 建立连接时 io.on 不工作

ruby - Heroku worker dyno 进程间的 TCP Socket 通信

c# - C#UdpClient服务器和客户端问题

c++ - Qt Creator 需要一个编译器设置来构建

c++ - JSONNODE 未在此范围内声明

c++ - 使用BFS的迷宫路线输出错误

c - for 循环赋值溢出到另一个变量