【发布时间】:2012-04-13 15:08:15
【问题描述】:
我正在尝试将文件的内容从服务器发送到客户端,我正在使用 fgets 逐行读取文件并逐行写入套接字描述符,在客户端,我在一个while 循环,读取发送的内容。我无法终止服务器发送序列,即客户端继续读取缓冲区并且下一个程序行没有执行,我认为我的发送或接收方式有问题。这是代码:
server :
filefd = fopen("clients.txt","a+");
while(fgets(filcont,300,filefd) != NULL)
{// write whole file contents to client
n=write(newsockfd,filcont,strlen(filcont));
if(n==0) break;
memset(filcont,'\0',300);
}
fclose(filefd);
client side :
while(n>0){
n = read(sockfd,buffer,sizeof(buffer)-1);
if(n==0) break;
printf("%s\nbytes read :%d \n",buffer,n);
memset(buffer,'\0',256);
}
printf("输入对端名称(你的除外)发送连接请求:\n"); 上述行( printf ,在我终止服务器之前不会执行对等名称)
【问题讨论】: