Web浏览器不跨preT通过一个无符号的字符数组传递图像 [英] Web browser does not interpret the image passed via an unsigned char array

查看:143
本文介绍了Web浏览器不跨preT通过一个无符号的字符数组传递图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所试图做的基本上是通过套接字来捕获浏览器的HTTP GET请求并发送请求到互联网,然后捕获的答复,并将其发送回浏览器。当传输只包含文字,每一件事情完美的作品。但是,在下载图像时。浏览器给出了错误着的显示图像,因为它包含的错误。任何帮助将大大AP preciated。

 的#includecc352.h
#包括LT&; ARPA / inet.h>
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;INT
主(INT ARGC,字符** argv的)
{
INT listenfd,connfd,weblin,的WebCon,webwrite中,n,X,Y,W,W1;
socklen_t的LEN,WLEN;
结构SOCKADDR_IN servaddr,cliaddr,webservad,webcliad;
unsigned char型的buff [3072] buff2 [3072]转[5],wbuff [100000]。
time_t的蜱;
INT是= 1;
为const char * PTR;
如果((listenfd =插座(AF_INET,SOCK_STREAM,0))小于0){
    fprintf中(标准错误,套接字创建失败\\ n);
    出口(1);
     }bzero(安培; servaddr,sizeof的(servaddr));
servaddr.sin_family = AF_INET;如果(inet_pton(AF_INET,127.0.0.1,&安培; servaddr.sin_addr)LT = 0){
    的printf(%s的inet_pton错误,ARGV [1]);
    返回1;
     }
servaddr.sin_port = htons(4619);如果((绑定(listenfd,(SA *)及servaddr,sizeof的(servaddr)))小于0){
    fprintf中(标准错误,绑定失败\\ n);
    出口(1);
    fprintf中(标准输出,bindd完成\\ n);
     }
如果((weblin =插座(AF_INET,SOCK_STREAM,0))小于0){
    fprintf中(标准错误,套接字创建失败\\ n);
    出口(1);
     }
的printf(weblin插座创建\\ n);bzero(安培; webservad,sizeof的(webservad));
webservad.sin_family = AF_INET;
webservad.sin_port = htons(80);如果(inet_pton(AF_INET208.80.152.211,&放大器; webservad.sin_addr)下; = 0){
    的printf(%s的inet_pton错误,ARGV [1]);
    返回1;
     }
如果(连接(weblin,(SA *)及webservad,sizeof的(webservad))小于0){
    的printf(weblin连接错误);
    返回1;
     }
的printf(weblin连接\\ n);
如果(听(listenfd,LISTENQ)小于0){
    fprintf中(标准错误,倾听失败\\ n);
    出口(1);
    fprintf中(标准错误,listning \\ n);
     }
    LEN = sizeof的(cliaddr);
    如果((connfd =接受(listenfd,(SA *)及cliaddr,和放大器; LEN))小于0){
    fprintf中(标准错误,接受失败\\ n);
    出口(1);
     }
fprintf中(标准输出,连接已接受\\ n);
        INT D = 0;           阅读(connfd,&安培; BUFF,3071); //读取获得浏览器请求保存到阵列的buff
           unsigned char型支气管[strlen的(BUFF)];           为(D = 0; D< = sizeof的(勒布朗),D ++){
           支气管并[d] =抛光轮并[d];
           }
           写(weblin,布龙,sizeof的(勒布朗)); //将数据发送到互联网
           的printf(%S \\ n,布龙);
           而(D =读(weblin,&安培; wbuff,100000)0){//读取来自互联网的答复并将其保存到wbuff
           unsigned char型wron [strlen的(wbuff)];           为(D = 0; D< = sizeof的(wron),D ++){
            wron并[d] = wbuff并[d];
            }
           写(connfd,wbuff,strlen的(wbuff)); //写入应答到浏览器
           的printf(%S \\ n,wron);
    }
 关闭(connfd);
 关闭(lis​​tenfd);
 关闭(weblin);
}


解决方案

的strlen 是为C字符串,是 0 终止。 0 是一个二进制图像文件的中间完全有效的。所以,当你得到了你可能不发送尽可能多的数据。 (甚至的更多的数据比你得到的,可能比您的缓冲区应该持有,如果图像发生的的包含一个更是0

决不做你传递给缓冲读什么,如果你不检查调用成功。您的需求的使用的返回值,因为这是告诉你,你有多少数据得到的唯一的事情。

您是假定你会读一个呼叫整个请求。没有什么保证。

(我不明白你想用 wron 布隆在非要做什么必要在你的code,并有一个的memcpy 功能,使用它,而不是滚动您自己。)

(你应该检查你的所有的调用的返回code)

What I am trying to do basically is to capture the HTTP GET request of the browser via a socket and send that request to the Internet, then capture the reply and send it back to the browser. When the transfer consist only of text, every thing works perfectly. But when downloading an image. browser gives the error "cant display the image because it contains errors". any help would be greatly appreciated.

#include "cc352.h"
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int
main(int argc, char **argv)
{
int                 listenfd, connfd,weblin,webcon,webwrite,n,x,y,w,w1;
socklen_t           len,wlen;
struct sockaddr_in  servaddr, cliaddr , webservad , webcliad;
unsigned char       buff[3072] , buff2[3072] ,ext[5] ,wbuff[100000];
time_t              ticks;
int                 yes = 1;
const char          *ptr;


if ( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){
    fprintf(stderr, "socket creation failed\n");
    exit (1);
     }

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family      = AF_INET;

if (inet_pton(AF_INET,"127.0.0.1", &servaddr.sin_addr) <= 0){
    printf("inet_pton error for %s", argv[1]);
    return 1;
     }
servaddr.sin_port        = htons(4619); 

if ( (bind(listenfd, (SA *) &servaddr, sizeof(servaddr))) < 0) {
    fprintf(stderr, "bind failed\n");
    exit (1);
    fprintf(stdout, "bindd completed\n");
     }


if ( (weblin = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){
    fprintf(stderr, "socket creation failed\n");
    exit (1);
     }
printf("weblin socket created \n");

bzero(&webservad, sizeof(webservad));
webservad.sin_family      = AF_INET;
webservad.sin_port        = htons(80); 

if (inet_pton(AF_INET,"208.80.152.211", &webservad.sin_addr) <= 0){
    printf("inet_pton error for %s", argv[1]);
    return 1;
     }


if (connect(weblin, (SA *) &webservad, sizeof(webservad)) < 0) {
    printf("weblin connect error");
    return 1; 
     }
printf("weblin connected \n");


if ( listen(listenfd, LISTENQ) < 0) {
    fprintf(stderr, "listen failed\n");
    exit (1);
    fprintf(stderr, "listning\n");
     }


    len = sizeof(cliaddr);
    if ( (connfd = accept(listenfd, (SA *) &cliaddr, &len)) < 0 ) {
    fprintf(stderr, "accept failed\n");
    exit (1);
     }
fprintf(stdout, "Connection accepted\n");


        int d=0;

           read(connfd,&buff, 3071); // Reads GET request from browser save it to array buff
           unsigned char bron[strlen(buff)];

           for(d=0 ; d<=sizeof(bron) ; d++){
           bron[d]=buff[d];
           }
           write(weblin,bron, sizeof(bron)); // Send the data to Internet
           printf("%s \n",bron);


           while(d=read(weblin,&wbuff, 100000)>0){ //Reads the reply from Internet and save it to wbuff
           unsigned char wron[strlen(wbuff)];

           for(d=0 ; d<=sizeof(wron) ; d++ ){
            wron[d]=wbuff[d];
            }
           write(connfd,wbuff,strlen(wbuff)); //Writes the reply to the browser
           printf("%s \n",wron);
    }


 close(connfd);
 close(listenfd);
 close(weblin);
}

解决方案

strlen is meant for C strings, that are 0 terminated. 0 is perfectly valid in the middle of a binary image file. So you're probably not sending as much data as you got. (Or even more data than you got, possibly even more than what your buffer is supposed to hold, if the image happens not to contain a 0.)

Never do anything with the buffer you've passed to read if you don't check that the read call was successful. You need to use read's return value because that's the only thing that tells you how much data you got.

You're assuming that you'll read the whole request in a single read call. Nothing guarantees that.

(I don't understand what you're trying to do with wron, and bron in un-necessary in your code. And there's a memcpy function. Use it rather than rolling your own.)

(And you should be checking the return code of all your write calls.)

这篇关于Web浏览器不跨preT通过一个无符号的字符数组传递图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆