1
我想從服務器發送文件的內容到客戶端,我正在逐行讀取文件行,使用fgets並逐行寫入套接字描述符,客戶端,我在一個while循環中,讀取發送的內容。我無法終止服務器發送序列,即客戶端不斷讀取緩衝區,下一個程序行不執行,我認爲他們有什麼問題我發送或接收的方式。這裏是代碼:在套接字描述符上讀/寫linux c
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,peer name不會被執行,直到我終止服務器)