BUF在c语言中的作用,在C语言中使用gets(buf);有警告是怎么回事?

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 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#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\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);//关闭套接字

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值