Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
read()返回正数,但recvline中没有任何内容_C_Sockets - Fatal编程技术网

read()返回正数,但recvline中没有任何内容

read()返回正数,但recvline中没有任何内容,c,sockets,C,Sockets,我在C中使用socket,我想问,正常读取返回正值,但不读取 #define MAXLINE 1234 .... upload(){ .... long long unsigned int byteNum = 0, count = 0; ssize_t nbyte; char sendline[MAXLINE], recvline[MAXLINE]; .... while((count += byteNum) < filesize) { bz

我在C中使用socket,我想问,正常读取返回正值,但不读取

#define MAXLINE 1234
....
upload(){
....
    long long unsigned int byteNum = 0, count = 0;
    ssize_t nbyte;
    char sendline[MAXLINE], recvline[MAXLINE];
....
    while((count += byteNum) < filesize) {
        bzero(sendline, MAXLINE);
        if((byteNum = fread(&sendline, sizeof(char), MAXLINE, fp)) != MAXLINE ){
            printf("End of file or Error\n");
        }
        if((nbyte = write(sockfd, sendline, byteNum)) < 0){
            printf("upload write error at count = %llu\n", count);
            exit(1);
        }read(sockfd, recvline, MAXLINE);
    }
....

}

download(){
....
    long long unsigned int filesize, count;
    ssize_t nbyte;
    char sendline[MAXLINE], recvline[MAXLINE];
....
        while((count += n) < filesize){
                bzero(recvline, MAXLINE);
                if((nbyte = read(sockfd, recvline, MAXLINE)) < 0){
                    printf("download read fail at %llu\n", count);
                    exit(1);
                }if(nbyte == 0){
                    printf("nbyte = 0!!\n");
                    return;
                }if(strlen(recvline) == 0){
                    printf("nbyte = %zd recvline = 0\n", nbyte);
                    return;
                }
                n = strlen(recvline);
                if(fwrite(&recvline, sizeof(char), nbyte, fp) != nbyte)
                    printf("end of file or error\n");
                write(sockfd, "ok", 2);                 
            }
....
}
只有当文件大小较大时(在测试用例中,大约为5GB),才会发生这种情况,但在大约50MB的情况下,它工作得很好

可能有什么问题


我已更改了nbyte的类型(从无符号long long更改为ssize_t)

但结果仍然是一样的

我在C中使用套接字,我想问,这是正常的读取返回吗 正值,但什么也不读

#define MAXLINE 1234
....
upload(){
....
    long long unsigned int byteNum = 0, count = 0;
    ssize_t nbyte;
    char sendline[MAXLINE], recvline[MAXLINE];
....
    while((count += byteNum) < filesize) {
        bzero(sendline, MAXLINE);
        if((byteNum = fread(&sendline, sizeof(char), MAXLINE, fp)) != MAXLINE ){
            printf("End of file or Error\n");
        }
        if((nbyte = write(sockfd, sendline, byteNum)) < 0){
            printf("upload write error at count = %llu\n", count);
            exit(1);
        }read(sockfd, recvline, MAXLINE);
    }
....

}

download(){
....
    long long unsigned int filesize, count;
    ssize_t nbyte;
    char sendline[MAXLINE], recvline[MAXLINE];
....
        while((count += n) < filesize){
                bzero(recvline, MAXLINE);
                if((nbyte = read(sockfd, recvline, MAXLINE)) < 0){
                    printf("download read fail at %llu\n", count);
                    exit(1);
                }if(nbyte == 0){
                    printf("nbyte = 0!!\n");
                    return;
                }if(strlen(recvline) == 0){
                    printf("nbyte = %zd recvline = 0\n", nbyte);
                    return;
                }
                n = strlen(recvline);
                if(fwrite(&recvline, sizeof(char), nbyte, fp) != nbyte)
                    printf("end of file or error\n");
                write(sockfd, "ok", 2);                 
            }
....
}
不,这不正常——如果成功,read()总是返回它放入缓冲区的字节数

可能有什么问题

看起来您正在尝试处理二进制数据,就像它是ASCII文本数据一样。这是行不通的,因为二进制数据中可能包含NUL/zero字节,如果您试图将其视为文本数据(例如,通过调用strlen()/strcmp()/strcpy()/etc),这些函数将被NUL字节的存在所愚弄,因为这些函数期望NUL字节指示字符串的结尾


特别是,您看到的是,有时写入recvline的第一个字节是0,这就是为什么strlen(recvline)返回0,即使read()将一个或多个有效字节写入recvline。

不要发布外部链接和/或文本图像!这种行为如何与函数的文档相矛盾?请发表一篇文章。您确定
nbyte
是有符号类型吗?
nbyte 65535
图像中的
-1
值非常类似于分配给无符号16位整数的
printf(“nbyte=%llu recvline=0\n”,nbyte)。。。那怎么能打印负值呢?请注意,
read()
不会将缓冲区终止为null。在您刚刚将
read()
读入的缓冲区上调用
strlen()
,可能是未定义的行为。