我有一个任务,我必须从服务器下载一个文件..但我有这个东西,有时数据被发送,有时他们只是没有!
服务器 :
printf("Reading and sending the File.\n");
memset(buff, 0, sizeof(buff));
while((n=read(fp, buff, 1024)) > 0 )
{
printf ("%s \n", buff); //no problem, coz it reads the file correctly
write(connfd, buff, n);
memset(buff, 0, sizeof(buff));
}
客户:
printf("Start downloading the file.\n");
bzero(&data,sizeof(data));
n = 0;
while (1)
{
n = read(sockfd , data, 1024);
if (n > 0)
{
data[n] = 0;
r = write (fp, data, n);
if (r < 0)
{
printf("Write to file !!!!!!\n");
exit(1);
}
}
printf ("\n%s\n", data);
bzero (&data, sizeof (data));
if (n < 1024)
break;
}
printf("Done downloading the file\n");
close (fp);