C:recv()偶尔返回非常大的数字

时间:2018-10-26 05:13:14

标签: c sockets segmentation-fault recv

我正在为网络类进行简单的“代理服务器”硬件分配。我现在要实现的目标:客户端连接到服务器并发送消息,服务器从网址中获取文件,服务器将文件大小发送给客户端,然后发送整个文本文件。客户端将文本保存到txt文档中。

它在大多数情况下都能正常工作,但偶尔会遇到随机分段错误(11),该错误似乎是由recv()引起的,返回的确是一个很大的数字。我在Google周围搜索,找不到任何RECV返回如此大数字的示例。所以我很困惑。

这是成功运行与发生段故障时得到的代码和输出。这个问题一直使我发疯,并导致我尝试一些古怪的方法来解决它,所以我为有些混乱的代码表示歉意。

服务器代码:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>

#define SIZE 1024
char buf[SIZE];
char buf2[SIZE];

#define TIME_PORT 1234 

int main(int argc, char *argv[]){
    printf("test");
    fflush(stdout);
    printf("test");
    fflush(stdout);
    FILE * fp;
    printf("test");
    fflush(stdout);
    FILE * sp;
    printf("test");
    fflush(stdout);
    int sockfd,client_sockfd;

    int nread,len;

    struct sockaddr_in serv_addr,client_addr;

    int badReq=0;

    char filesizeStr[50];

    long unsigned int filesize;



/* create endpoint */
    printf("creating endpoint\n");
    fflush(stdout);
    if((sockfd = socket(AF_INET,SOCK_STREAM,0))<0){
        perror(NULL);
        exit(2);
    }
/* bind address */
    printf("binding address\n");
    fflush(stdout);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(TIME_PORT);
    if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))<0){
        perror(NULL);
        exit(3);
    }
/* specify queue */
    printf("specify queue\n");
    fflush(stdout);
    len = sizeof(client_addr);
    listen(sockfd,5);
    for(;;){
        badReq=0;
        printf("call accept\n");
        fflush(stdout);
        client_sockfd = accept(sockfd,(struct sockaddr *) &client_addr,&len);
        printf("accept done\n");
        fflush(stdout);

        if(client_sockfd==-1){
            perror(NULL);
            continue;
        }

        nread = read(client_sockfd,buf2,SIZE);
        write(1,buf2,nread);

        if(badReq){
            strncpy(filesizeStr, "Bad Request", 20);
            filesize=0;
        }
        else{
            sp=popen("curl -sI http://user.engineering.uiowa.edu/%7Ejwryan/Communication_Networks/war.txt | grep -i Content-Length | awk '{print $2}'","r");
            fp=popen("curl http://user.engineering.uiowa.edu/%7Ejwryan/Communication_Networks/war.txt", "r");

            fgets(filesizeStr,50,sp);
            sscanf(filesizeStr,"%lu",&filesize);
        }

        /* transfer data */
        printf("%s\n",filesizeStr);
        printf("transferring data\n");
        fflush(stdout);
        int check=write(client_sockfd,filesizeStr,strlen(filesizeStr));
        if(check<0){
            printf("sending error");
            fflush(stdout);
            exit(EXIT_FAILURE);
        }
        ssize_t total_bytes_written = 0;
        while(total_bytes_written<filesize){
            memset(buf, 0, 1024);
            fread(buf,sizeof(char),1024,fp);
            ssize_t bytes_written = write(client_sockfd,buf,strlen(buf)<1024?strlen(buf):1024);
            total_bytes_written += bytes_written;
            printf("wrote %d bytes\n",total_bytes_written);
            fflush(stdout);
            if (bytes_written == -1){
                /* Report failure and exit. */
                printf("failed\n");
                break;
            }
        }
        close(client_sockfd);
    }
}

客户代码:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>

#define SIZE 1024
char buf[SIZE];
char buf2[SIZE]="war.txt";
#define TIME_PORT 1234 

int main(int argc,char *argv[]){
    FILE * file;
    int filesize;
    int sockfd;
    int nread;
    struct sockaddr_in serv_addr;
    struct hostent *h;
    if(argc!=2){
        fprintf(stderr,"usage: %s IPaddr\n",argv[0]);
        exit(1);
    }
/* create endpoint */
    if((sockfd = socket(AF_INET,SOCK_STREAM,0))<0){
        perror(NULL);
        exit(2);
    }
/* connect to server */
    serv_addr.sin_family = AF_INET;
    h = gethostbyname(argv[1]);
    bcopy(h->h_addr,(char *) &serv_addr.sin_addr,h->h_length);
    serv_addr.sin_port = htons(TIME_PORT);
    if(connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr))<0){
        perror(NULL);
        exit(3);
    }
/* transfer data */
    write(sockfd,buf2,sizeof("GET %7Ejwryan/Communication_Networks/war.txt HTTP/1.0\\r\\n\\r\\n")); //meaningless at this time

    recv(sockfd, buf, SIZE, 0);
    filesize = atoi(buf);
    printf("%d",filesize);
    fflush(stdout);
    file = fopen("test.txt", "wb");
    if (file == NULL)
    {
        printf("file error");
        fflush(stdout);
        exit(EXIT_FAILURE);
    }
    char buf3[filesize+1000];
    ssize_t len=0;
    int total_rec=0;
    int flag=0;
    while(total_rec<filesize){
        len = recv(sockfd, buf, SIZE, 0);
        strcat(buf3,buf);
        total_rec+=len;
        if(len<0){
            printf("error1, len:%zi\n",len);
            fflush(stdout);
        }
        else if(len<1024){
            printf("len\n");
            fflush(stdout);
            //break;
        }
        else if(len>1024){
            printf("error2, len:%zi\n",len);
            fflush(stdout);
        }
        printf("total: %d \n, len: %zi\n",total_rec,len);
        fflush(stdout);
    }
    printf("test");
    fflush(stdout);
    //printf("%s",buf3);
    fwrite(buf3, sizeof(char), filesize, file);
    fclose(file);
    close(sockfd);
    exit(0);
}

服务器输出(成功):

    testtesttesttestcreating endpoint
binding address
specify queue
call accept
accept done
war.txt  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0363705

transferring data
wrote 1024 bytes
wrote 2048 bytes
wrote 3072 bytes
wrote 4096 bytes
wrote 5120 bytes
wrote 6144 bytes
wrote 7168 bytes
wrote 8192 bytes
wrote 9216 bytes
wrote 10240 bytes
wrote 11264 bytes
wrote 12288 bytes
wrote 13312 bytes
wrote 14336 bytes
wrote 15360 bytes
wrote 16384 bytes
wrote 17408 bytes
wrote 18432 bytes
wrote 19456 bytes
wrote 20480 bytes
wrote 21504 bytes
wrote 22528 bytes
wrote 23552 bytes
wrote 24576 bytes
wrote 25600 bytes
wrote 26624 bytes
wrote 27648 bytes
wrote 28672 bytes
wrote 29696 bytes
wrote 30720 bytes
wrote 31744 bytes
wrote 32768 bytes
wrote 33792 bytes
wrote 34816 bytes
wrote 35840 bytes
wrote 36864 bytes
wrote 37888 bytes
wrote 38912 bytes
wrote 39936 bytes
wrote 40960 bytes
wrote 41984 bytes
wrote 43008 bytes
wrote 44032 bytes
wrote 45056 bytes
wrote 46080 bytes
wrote 47104 bytes
wrote 48128 bytes
wrote 49152 bytes
wrote 50176 bytes
wrote 51200 bytes
wrote 52224 bytes
wrote 53248 bytes
wrote 54272 bytes
wrote 55296 bytes
wrote 56320 bytes
wrote 57344 bytes
wrote 58368 bytes
wrote 59392 bytes
wrote 60416 bytes
wrote 61440 bytes
wrote 62464 bytes
wrote 63488 bytes
wrote 64512 bytes
wrote 65536 bytes
wrote 66560 bytes
wrote 67584 bytes
wrote 68608 bytes
wrote 69632 bytes
wrote 70656 bytes
wrote 71680 bytes
wrote 72704 bytes
wrote 73728 bytes
wrote 74752 bytes
wrote 75776 bytes
wrote 76800 bytes
wrote 77824 bytes
wrote 78848 bytes
wrote 79872 bytes
wrote 80896 bytes
wrote 81920 bytes
wrote 82944 bytes
wrote 83968 bytes
wrote 84992 bytes
wrote 86016 bytes
wrote 87040 bytes
wrote 88064 bytes
wrote 89088 bytes
wrote 90112 bytes
wrote 91136 bytes
wrote 92160 bytes
wrote 93184 bytes
wrote 94208 bytes
wrote 95232 bytes
wrote 96256 bytes
wrote 97280 bytes
wrote 98304 bytes
wrote 99328 bytes
wrote 100352 bytes
wrote 101376 bytes
wrote 102400 bytes
wrote 103424 bytes
wrote 104448 bytes
wrote 105472 bytes
wrote 106496 bytes
wrote 107520 bytes
wrote 108544 bytes
wrote 109568 bytes
wrote 110592 bytes
wrote 111616 bytes
wrote 112640 bytes
wrote 113664 bytes
wrote 114688 bytes
wrote 115712 bytes
wrote 116736 bytes
wrote 117760 bytes
wrote 118784 bytes
wrote 119808 bytes
wrote 120832 bytes
wrote 121856 bytes
wrote 122880 bytes
wrote 123904 bytes
wrote 124928 bytes
wrote 125952 bytes
wrote 126976 bytes
wrote 128000 bytes
wrote 129024 bytes
wrote 130048 bytes
wrote 131072 bytes
wrote 132096 bytes
wrote 133120 bytes
wrote 134144 bytes
wrote 135168 bytes
wrote 136192 bytes
wrote 137216 bytes
wrote 138240 bytes
wrote 139264 bytes
wrote 140288 bytes
wrote 141312 bytes
wrote 142336 bytes
wrote 143360 bytes
wrote 144384 bytes
wrote 145408 bytes
wrote 146432 bytes
wrote 147456 bytes
wrote 148480 bytes
wrote 149504 bytes
wrote 150528 bytes
wrote 151552 bytes
wrote 152576 bytes
wrote 153600 bytes
wrote 154624 bytes
wrote 155648 bytes
wrote 156672 bytes
wrote 157696 bytes
wrote 158720 bytes
wrote 159744 bytes
wrote 160768 bytes
wrote 161792 bytes
wrote 162816 bytes
wrote 163840 bytes
 48  355k   48  173k    0     0  3528k      0 --:--:-- --:--:-- --:--:-- 3474kwrote 164864 bytes
wrote 165888 bytes
wrote 166912 bytes
wrote 167936 bytes
wrote 168960 bytes
wrote 169984 bytes
wrote 171008 bytes
wrote 172032 bytes
wrote 173056 bytes
wrote 174080 bytes
wrote 175104 bytes
wrote 176128 bytes
wrote 177152 bytes
wrote 178176 bytes
wrote 179200 bytes
wrote 180224 bytes
wrote 181248 bytes
wrote 182272 bytes
wrote 183296 bytes
wrote 184320 bytes
wrote 185344 bytes
wrote 186368 bytes
wrote 187392 bytes
wrote 188416 bytes
wrote 189440 bytes
wrote 190464 bytes
wrote 191488 bytes
wrote 192512 bytes
wrote 193536 bytes
wrote 194560 bytes
wrote 195584 bytes
wrote 196608 bytes
wrote 197632 bytes
wrote 198656 bytes
wrote 199680 bytes
wrote 200704 bytes
wrote 201728 bytes
wrote 202752 bytes
wrote 203776 bytes
wrote 204800 bytes
wrote 205824 bytes
wrote 206848 bytes
wrote 207872 bytes
wrote 208896 bytes
wrote 209920 bytes
wrote 210944 bytes
wrote 211968 bytes
wrote 212992 bytes
wrote 214016 bytes
wrote 215040 bytes
wrote 216064 bytes
wrote 217088 bytes
wrote 218112 bytes
wrote 219136 bytes
wrote 220160 bytes
wrote 221184 bytes
wrote 222208 bytes
wrote 223232 bytes
wrote 224256 bytes
wrote 225280 bytes
wrote 226304 bytes
wrote 227328 bytes
wrote 228352 bytes
wrote 229376 bytes
wrote 230400 bytes
wrote 231424 bytes
wrote 232448 bytes
wrote 233472 bytes
wrote 234496 bytes
wrote 235520 bytes
wrote 236544 bytes
wrote 237568 bytes
wrote 238592 bytes
wrote 239616 bytes
wrote 240640 bytes
wrote 241664 bytes
wrote 242688 bytes
wrote 243712 bytes
wrote 244736 bytes
wrote 245760 bytes
wrote 246784 bytes
wrote 247808 bytes
wrote 248832 bytes
wrote 249856 bytes
wrote 250880 bytes
wrote 251904 bytes
wrote 252928 bytes
wrote 253952 bytes
wrote 254976 bytes
wrote 256000 bytes
wrote 257024 bytes
wrote 258048 bytes
wrote 259072 bytes
wrote 260096 bytes
wrote 261120 bytes
wrote 262144 bytes
wrote 263168 bytes
wrote 264192 bytes
wrote 265216 bytes
wrote 266240 bytes
wrote 267264 bytes
wrote 268288 bytes
wrote 269312 bytes
wrote 270336 bytes
wrote 271360 bytes
wrote 272384 bytes
wrote 273408 bytes
wrote 274432 bytes
wrote 275456 bytes
wrote 276480 bytes
wrote 277504 bytes
wrote 278528 bytes
wrote 279552 bytes
wrote 280576 bytes
wrote 281600 bytes
wrote 282624 bytes
wrote 283648 bytes
wrote 284672 bytes
wrote 285696 bytes
wrote 286720 bytes
10wrote 287744 bytes
0  3wrote 288768 bytes
55k wrote 289792 bytes
 10wrote 290816 bytes
0  355k    0  wrote 291840 bytes
   0  1301k    wrote 292864 bytes
  0 wrote 293888 bytes
--:-wrote 294912 bytes
-:-- --:wrote 295936 bytes
--:-- -wrote 296960 bytes
-:-wrote 297984 bytes
-:-wrote 299008 bytes
- 1wrote 300032 bytes
301kwrote 301056 bytes

wrote 302080 bytes
wrote 303104 bytes
wrote 304128 bytes
wrote 305152 bytes
wrote 306176 bytes
wrote 307200 bytes
wrote 308224 bytes
wrote 309248 bytes
wrote 310272 bytes
wrote 311296 bytes
wrote 312320 bytes
wrote 313344 bytes
wrote 314368 bytes
wrote 315392 bytes
wrote 316416 bytes
wrote 317440 bytes
wrote 318464 bytes
wrote 319488 bytes
wrote 320512 bytes
wrote 321536 bytes
wrote 322560 bytes
wrote 323584 bytes
wrote 324608 bytes
wrote 325632 bytes
wrote 326656 bytes
wrote 327680 bytes
wrote 328704 bytes
wrote 329728 bytes
wrote 330752 bytes
wrote 331776 bytes
wrote 332800 bytes
wrote 333824 bytes
wrote 334848 bytes
wrote 335872 bytes
wrote 336896 bytes
wrote 337920 bytes
wrote 338944 bytes
wrote 339968 bytes
wrote 340992 bytes
wrote 342016 bytes
wrote 343040 bytes
wrote 344064 bytes
wrote 345088 bytes
wrote 346112 bytes
wrote 347136 bytes
wrote 348160 bytes
wrote 349184 bytes
wrote 350208 bytes
wrote 351232 bytes
wrote 352256 bytes
wrote 353280 bytes
wrote 354304 bytes
wrote 355328 bytes
wrote 356352 bytes
wrote 357376 bytes
wrote 358400 bytes
wrote 359424 bytes
wrote 360448 bytes
wrote 361472 bytes
wrote 362496 bytes
wrote 363520 bytes
wrote 363705 bytes
call accept

客户端输出(成功):

363705total: 1024
, len: 1024
total: 2048
, len: 1024
total: 3072
, len: 1024
total: 4096
, len: 1024
total: 5120
, len: 1024
total: 6144
, len: 1024
total: 7168
, len: 1024
total: 8192
, len: 1024
total: 9216
, len: 1024
total: 10240
, len: 1024
total: 11264
, len: 1024
total: 12288
, len: 1024
total: 13312
, len: 1024
total: 14336
, len: 1024
total: 15360
, len: 1024
total: 16384
, len: 1024
total: 17408
, len: 1024
total: 18432
, len: 1024
total: 19456
, len: 1024
total: 20480
, len: 1024
total: 21504
, len: 1024
total: 22528
, len: 1024
total: 23552
, len: 1024
total: 24576
, len: 1024
total: 25600
, len: 1024
total: 26624
, len: 1024
total: 27648
, len: 1024
total: 28672
, len: 1024
total: 29696
, len: 1024
total: 30720
, len: 1024
total: 31744
, len: 1024
total: 32768
, len: 1024
total: 33792
, len: 1024
total: 34816
, len: 1024
total: 35840
, len: 1024
total: 36864
, len: 1024
total: 37888
, len: 1024
total: 38912
, len: 1024
total: 39936
, len: 1024
total: 40960
, len: 1024
total: 41984
, len: 1024
total: 43008
, len: 1024
total: 44032
, len: 1024
total: 45056
, len: 1024
total: 46080
, len: 1024
total: 47104
, len: 1024
total: 48128
, len: 1024
total: 49152
, len: 1024
total: 50176
, len: 1024
total: 51200
, len: 1024
total: 52224
, len: 1024
total: 53248
, len: 1024
total: 54272
, len: 1024
total: 55296
, len: 1024
total: 56320
, len: 1024
total: 57344
, len: 1024
total: 58368
, len: 1024
total: 59392
, len: 1024
total: 60416
, len: 1024
total: 61440
, len: 1024
total: 62464
, len: 1024
total: 63488
, len: 1024
total: 64512
, len: 1024
total: 65536
, len: 1024
total: 66560
, len: 1024
total: 67584
, len: 1024
total: 68608
, len: 1024
total: 69632
, len: 1024
total: 70656
, len: 1024
total: 71680
, len: 1024
total: 72704
, len: 1024
total: 73728
, len: 1024
total: 74752
, len: 1024
total: 75776
, len: 1024
total: 76800
, len: 1024
total: 77824
, len: 1024
total: 78848
, len: 1024
total: 79872
, len: 1024
total: 80896
, len: 1024
total: 81920
, len: 1024
total: 82944
, len: 1024
total: 83968
, len: 1024
total: 84992
, len: 1024
total: 86016
, len: 1024
total: 87040
, len: 1024
total: 88064
, len: 1024
total: 89088
, len: 1024
total: 90112
, len: 1024
total: 91136
, len: 1024
total: 92160
, len: 1024
total: 93184
, len: 1024
total: 94208
, len: 1024
total: 95232
, len: 1024
total: 96256
, len: 1024
total: 97280
, len: 1024
total: 98304
, len: 1024
total: 99328
, len: 1024
total: 100352
, len: 1024
total: 101376
, len: 1024
total: 102400
, len: 1024
total: 103424
, len: 1024
total: 104448
, len: 1024
total: 105472
, len: 1024
total: 106496
, len: 1024
total: 107520
, len: 1024
total: 108544
, len: 1024
total: 109568
, len: 1024
total: 110592
, len: 1024
total: 111616
, len: 1024
total: 112640
, len: 1024
total: 113664
, len: 1024
total: 114688
, len: 1024
total: 115712
, len: 1024
total: 116736
, len: 1024
total: 117760
, len: 1024
total: 118784
, len: 1024
total: 119808
, len: 1024
total: 120832
, len: 1024
total: 121856
, len: 1024
total: 122880
, len: 1024
total: 123904
, len: 1024
total: 124928
, len: 1024
total: 125952
, len: 1024
total: 126976
, len: 1024
total: 128000
, len: 1024
total: 129024
, len: 1024
total: 130048
, len: 1024
total: 131072
, len: 1024
total: 132096
, len: 1024
total: 133120
, len: 1024
total: 134144
, len: 1024
total: 135168
, len: 1024
total: 136192
, len: 1024
total: 137216
, len: 1024
total: 138240
, len: 1024
total: 139264
, len: 1024
total: 140288
, len: 1024
total: 141312
, len: 1024
total: 142336
, len: 1024
total: 143360
, len: 1024
total: 144384
, len: 1024
total: 145408
, len: 1024
total: 146432
, len: 1024
total: 147456
, len: 1024
total: 148480
, len: 1024
total: 149504
, len: 1024
total: 150528
, len: 1024
total: 151552
, len: 1024
total: 152576
, len: 1024
total: 153600
, len: 1024
total: 154624
, len: 1024
total: 155648
, len: 1024
total: 156672
, len: 1024
total: 157696
, len: 1024
total: 158720
, len: 1024
total: 159744
, len: 1024
total: 160768
, len: 1024
total: 161792
, len: 1024
total: 162816
, len: 1024
total: 163840
, len: 1024
total: 164864
, len: 1024
total: 165888
, len: 1024
total: 166912
, len: 1024
total: 167936
, len: 1024
total: 168960
, len: 1024
total: 169984
, len: 1024
total: 171008
, len: 1024
total: 172032
, len: 1024
total: 173056
, len: 1024
total: 174080
, len: 1024
total: 175104
, len: 1024
total: 176128
, len: 1024
total: 177152
, len: 1024
total: 178176
, len: 1024
total: 179200
, len: 1024
total: 180224
, len: 1024
total: 181248
, len: 1024
total: 182272
, len: 1024
total: 183296
, len: 1024
total: 184320
, len: 1024
total: 185344
, len: 1024
total: 186368
, len: 1024
total: 187392
, len: 1024
total: 188416
, len: 1024
total: 189440
, len: 1024
total: 190464
, len: 1024
total: 191488
, len: 1024
total: 192512
, len: 1024
total: 193536
, len: 1024
total: 194560
, len: 1024
total: 195584
, len: 1024
total: 196608
, len: 1024
total: 197632
, len: 1024
total: 198656
, len: 1024
total: 199680
, len: 1024
total: 200704
, len: 1024
total: 201728
, len: 1024
total: 202752
, len: 1024
total: 203776
, len: 1024
total: 204800
, len: 1024
total: 205824
, len: 1024
total: 206848
, len: 1024
total: 207872
, len: 1024
total: 208896
, len: 1024
total: 209920
, len: 1024
total: 210944
, len: 1024
total: 211968
, len: 1024
total: 212992
, len: 1024
total: 214016
, len: 1024
total: 215040
, len: 1024
total: 216064
, len: 1024
total: 217088
, len: 1024
total: 218112
, len: 1024
total: 219136
, len: 1024
total: 220160
, len: 1024
total: 221184
, len: 1024
total: 222208
, len: 1024
total: 223232
, len: 1024
total: 224256
, len: 1024
total: 225280
, len: 1024
total: 226304
, len: 1024
total: 227328
, len: 1024
total: 228352
, len: 1024
total: 229376
, len: 1024
total: 230400
, len: 1024
total: 231424
, len: 1024
total: 232448
, len: 1024
total: 233472
, len: 1024
total: 234496
, len: 1024
total: 235520
, len: 1024
total: 236544
, len: 1024
total: 237568
, len: 1024
total: 238592
, len: 1024
total: 239616
, len: 1024
total: 240640
, len: 1024
total: 241664
, len: 1024
total: 242688
, len: 1024
total: 243712
, len: 1024
total: 244736
, len: 1024
total: 245760
, len: 1024
total: 246784
, len: 1024
total: 247808
, len: 1024
total: 248832
, len: 1024
total: 249856
, len: 1024
total: 250880
, len: 1024
total: 251904
, len: 1024
total: 252928
, len: 1024
total: 253952
, len: 1024
total: 254976
, len: 1024
total: 256000
, len: 1024
total: 257024
, len: 1024
total: 258048
, len: 1024
total: 259072
, len: 1024
total: 260096
, len: 1024
total: 261120
, len: 1024
total: 262144
, len: 1024
total: 263168
, len: 1024
total: 264192
, len: 1024
total: 265216
, len: 1024
total: 266240
, len: 1024
total: 267264
, len: 1024
total: 268288
, len: 1024
total: 269312
, len: 1024
total: 270336
, len: 1024
total: 271360
, len: 1024
total: 272384
, len: 1024
total: 273408
, len: 1024
total: 274432
, len: 1024
total: 275456
, len: 1024
total: 276480
, len: 1024
total: 277504
, len: 1024
total: 278528
, len: 1024
total: 279552
, len: 1024
total: 280576
, len: 1024
total: 281600
, len: 1024
total: 282624
, len: 1024
total: 283648
, len: 1024
total: 284672
, len: 1024
total: 285696
, len: 1024
total: 286720
, len: 1024
total: 287744
, len: 1024
total: 288768
, len: 1024
total: 289792
, len: 1024
total: 290816
, len: 1024
total: 291840
, len: 1024
total: 292864
, len: 1024
total: 293888
, len: 1024
total: 294912
, len: 1024
total: 295936
, len: 1024
total: 296960
, len: 1024
total: 297984
, len: 1024
total: 299008
, len: 1024
total: 300032
, len: 1024
total: 301056
, len: 1024
total: 302080
, len: 1024
total: 303104
, len: 1024
total: 304128
, len: 1024
total: 305152
, len: 1024
total: 306176
, len: 1024
total: 307200
, len: 1024
total: 308224
, len: 1024
total: 309248
, len: 1024
total: 310272
, len: 1024
total: 311296
, len: 1024
total: 312320
, len: 1024
total: 313344
, len: 1024
total: 314368
, len: 1024
total: 315392
, len: 1024
total: 316416
, len: 1024
total: 317440
, len: 1024
total: 318464
, len: 1024
total: 319488
, len: 1024
total: 320512
, len: 1024
total: 321536
, len: 1024
total: 322560
, len: 1024
total: 323584
, len: 1024
total: 324608
, len: 1024
total: 325632
, len: 1024
total: 326656
, len: 1024
total: 327680
, len: 1024
total: 328704
, len: 1024
total: 329728
, len: 1024
total: 330752
, len: 1024
total: 331776
, len: 1024
total: 332800
, len: 1024
total: 333824
, len: 1024
total: 334848
, len: 1024
total: 335872
, len: 1024
total: 336896
, len: 1024
total: 337920
, len: 1024
total: 338944
, len: 1024
total: 339968
, len: 1024
total: 340992
, len: 1024
total: 342016
, len: 1024
total: 343040
, len: 1024
total: 344064
, len: 1024
total: 345088
, len: 1024
total: 346112
, len: 1024
total: 347136
, len: 1024
total: 348160
, len: 1024
total: 349184
, len: 1024
total: 350208
, len: 1024
total: 351232
, len: 1024
total: 352256
, len: 1024
total: 353280
, len: 1024
total: 354304
, len: 1024
total: 355328
, len: 1024
total: 356352
, len: 1024
total: 357376
, len: 1024
total: 358400
, len: 1024
total: 359424
, len: 1024
total: 360448
, len: 1024
total: 361472
, len: 1024
total: 362496
, len: 1024
total: 363520
, len: 1024
len
total: 363705
, len: 185

客户端输出(段错误):

363705total: 1024 
, len: 1024
total: 2048 
, len: 1024
total: 3072 

...

total: 362504 
, len: 1024
len
total: 362689 
, len: 185
error2, len:3338047398741108835
total: -827422507 
, len: 3338047398741108835
Segmentation fault: 11

无论服务器是否失败,服务器的输出都是相似的。服务器的某些输出是意外的,我不知道该怎么做。它看起来与初始curl命令的输出类似,但是我不知道如何在不执行curl命令的while循环内打印此输出。

在这一点上,我只想继续讲下去,但是我很好奇可能导致这种现象的原因。任何建议深表感谢。

1 个答案:

答案 0 :(得分:4)

    recv(sockfd, buf, SIZE, 0);
    filesize = atoi(buf);

您忽略了recv的返回值,因此您不知道缓冲区中有多少字节。然后,您在缓冲区上调用atoi,而不必确保正确终止接收到的数据。假设大小为12,但是recv仅返回一个字节,即“ 1”。您对atoi的调用将把以前缓冲区中的所有垃圾解释为大小的一部分。哎呀。

您也在其他地方重复了该错误:

    len = recv(sockfd, buf, SIZE, 0);
    strcat(buf3,buf);

糟糕。您在len中存储了接收到的数据的长度,但是随后只是将缓冲区传递给strcatstrcat应该如何知道有多少字节要追加到buf3上?