亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在C語言中使用gets(buf);有警告是怎么回事?

在C語言中使用gets(buf);有警告是怎么回事?

qq_莫非 2017-09-22 12:57:14
?warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]? ? ? ? ?gets(buf);//獲取用戶輸入的字符串,存放到buf中? ? ? ? ?^/tmp/ccnxjoDy.o:在函數‘main’中:tcpclient.c:(.text+0x149): 警告: the `gets' function is dangerous and should not be used.#include?<stdio.h> #include?<unistd.h> #include?<strings.h> #include?<sys/types.h> #include?<sys/socket.h> #include?<netinet/in.h> #include?<netdb.h> #include?<stdlib.h> #include?<string.h> #define?PORT?1234 #define?MAXDATASIZE?100 int?main(int?argc,char?*argv[]) { ????//定義變量 ????int?sockfd,num,numw; ????char?buf[MAXDATASIZE]; ????struct?hostent?*he; ????struct?sockaddr_in?server; ????if(argc!=2) ????{ ????????printf("Usage:%s<IP?Address>\n",argv[0]); ????????exit(1); ????} ???? ????//域名解析 ????if((he=gethostbyname(argv[1]))==NULL) ????{ ????????printf("gethostbyname()?error\n"); ????????exit(1);???? ????} ????//創建套接字 ????if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) ????{ ????????printf("socket()?error\n"); ????????exit(1);???? ????} ????//初始化 ????bzero(&server,sizeof(server)); ????server.sin_family=AF_INET; ????server.sin_port=htons(PORT); ????server.sin_addr=*((struct?in_addr?*)he->h_addr); ????//建立與服務器的連接 ????if(connect(sockfd,(struct?sockaddr?*)&server,sizeof(server))==-1) ????{ ????????printf("connect()?error\n"); ????????exit(1); ????} ????while(1){ ????????printf("Please?enter?a?string:\n"); ????????gets(buf);//獲取用戶輸入的字符串,存放到buf中 ????????if((numw=write(sockfd,buf,MAXDATASIZE))==-1){//將buf中的數據發送給服務器 ????????????printf("write?error.\n"); ????????????exit(1); ????????} ????????if((num=recv(sockfd,buf,MAXDATASIZE,0))==-1)//接收從服務器發回來的已經反轉的字符串 ????????{ ????????????printf("recv()?error\n"); ????????????exit(1); ????????} ????????if(!strcmp(buf,"tuiq")) ????????????break; ????????buf[num-1]='\0'; ????????printf("The?reverse?message:\n%s\n",buf); ????} ????close(sockfd);//關閉套接字 }
查看完整描述

1 回答

已采納
?
_夕晨踏雪

TA貢獻40條經驗 獲得超16個贊

因為gets函數可以無限讀取,除非遇到換行或者文件結束,緩存很容易溢出,因此是不安全的

查看完整回答
2 反對 回復 2017-09-22
  • 1 回答
  • 1 關注
  • 3892 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號