【发布时间】:2016-06-02 15:23:14
【问题描述】:
我在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);
}
....
}
结果
nbyte = 1234 recvline = 0
只有当文件很大时才会发生(在测试用例中,它大约为 5GB),但在大约 50MB 时效果很好。
可能出了什么问题?
我已经改变了 nbyte 的类型(从 unsigned long long 变成了 ssize_t)
但结果保持不变
【问题讨论】:
-
请勿发布外部链接和/或文字图片!这种行为与函数的文档有何矛盾?
-
请发帖Minimal, Complete, and Verifiable example。你确定
nbyte是签名类型吗? -
图像中的
nbyte 65535看起来非常像分配给无符号 16 位整数的-1的 hte 值。 -
printf("nbyte = %llu recvline = 0\n", nbyte);...那怎么能打印出负值? -
请注意,
read()不会以空值终止缓冲区。在您刚刚进入read()的缓冲区上调用strlen()可能是未定义的行为。