C-fwrite()没有从网络缓冲区写入第一个数据卡盘



我正在尝试通过制作一个程序来练习我的C编程,该程序基本上对家庭网络上的专用服务器执行"cp"命令。

通信似乎很好,但由于某种原因,通过导线发送的前8kb数据没有使用fwrite写入文件。。。但是PNG报头被部分地发送。

Client.c

uint32_t buffersize = BUFFERSIZE_M; // 8192
size_t file_index;
last_iteration = 0;
char confirmation[BUFFERSIZE_M] = {0};
if(filelen > buffersize)
{
// Read full buffersize
// subtract buffersize from filesize to determine remainder
// if remainder is > buffersize -> repeat; else read last segment of file.
// fileindex is used to guide the filepointer to where the buffer endpoint was/is.
fread(buffer, 1, buffersize, file);
file_index = buffersize;
fseek(file, 0, file_index);
#ifdef DEBUG
printf("filesize: %ldttbuffersize: %dn", filelen, buffersize);
#endif
// Send buffer full of file contents
getchar(); // Only here so I can visually step through sends
send(sock, buffer, strlen(buffer), 0);

filelen = filelen - buffersize;
file_index = file_index + buffersize;
// Read socket for confirmation to continue
// read(sock, confirmation, BUFFERSIZE_S);
recv(sock, confirmation, BUFFERSIZE_S, 0);
if(strncmp(confirmation, filename, strlen(filename)) == 0)
continue;
else
{
free(buffer);
close(sock);
fclose(file);
return -1;
}
}

服务器.c

while(done == FALSE)
{
// if((s_read = read(s_socket, f_buffer, BUFFERSIZE_XL)) != 0)
if((s_read = read(s_socket, f_buffer, BUFFERSIZE_M)) != 0)
{
if(strncmp(f_buffer, "DONE", strlen("DONE")) == 0)
{
done = TRUE; 
}    
else
{
printf("strlen(buffer): %ldts_read: %ldn", strlen(f_buffer), s_read);
fwrite(f_buffer, s_read, 1, output_file);
send(s_socket, filename, strlen(filename), 0);
}    
}
else
{
send(s_socket, filename, strlen(filename), 0); 
} 
memset(f_buffer, 0, s_read);   
}

正确PNG的Hexdump

0000000 5089 474e 0a0d 0a1a 0000 0d00 4849 5244
0000010 0000 6c05 0000 8303 0608 0000 2800 48cb
* Normal file contents until 0x2000 *
0002000 **75cc 65f8 6678** 00e9 e732 adf2 e47d 83ee

丢失8kb区块的Hexdump

0000000 5089 474e 0a0d 0a1a **75cc 65f8 6678** 0ae9
0000010 de20 8651 eaca 14e3 f7d4 e816 a67f f41f

然后,损坏的PNG文件的其余部分丢失了部分,正如您可以从损坏的PNG中的0ae9中看到的那样,它应该是

00e9有人能给我指出一些C网络缓冲资源的方向吗?除了人们在讨论如何创建套接字之外,我在Youtube上似乎找不到其他东西。

来自@Barmar的评论:

问题就在这里:send(sock,buffer,strlen(buffer(,0(;

这解决了问题,现在一切都很顺利。

相关内容

最新更新