gpt4 book ai didi

c - gets() 没有被执行

转载 作者:行者123 更新时间:2023-11-30 14:20:16 27 4
gpt4 key购买 nike

在我的源代码中,有以下片段:

    while ((cmd=getchar()) != EOF)
{
switch(cmd)
{
case '1':
printf("pls input the data to be sent: \n");
char data[100];
gets(data);
send_data(sd_cli, data, strlen(data), &svr_addr);
pcap_packet = pcap_next(pcap_handler, &pcap_header);
if(pcap_packet !=NULL)
printf("capture one packet with length of %d\n", pcap_header.len);
analyze_pcap_pkt(pcap_packet, &ipid, &temp_port1, &temp_port2, &seq, &ack_seq);
temp_seq = seq;
seq = ack_seq;
ack_seq = temp_seq;

ipid++;
break;
case '2':
printf("old ack is %x\n", ack_seq);
printf("pls input the seq plus amount: \n");
char amount[6];
gets(amount);
ack_seq= ack_seq+atoi(amount);
printf("new akc is %x\n", ack_seq);
send_ack(sd_raw, &svr_addr, lo_ipaddr, svr_ipaddr, htons(src_port), htons(dst_port), htons(ipid), htonl(seq), htonl(ack_seq));
ipid++;
break;
case '4':
send_rst(sd_raw, &svr_addr, lo_ipaddr, svr_ipaddr, htons(ipid), htons(src_port), htons(dst_port), htonl(seq), htonl(ack_seq));
break;
}
}

当我运行程序时,输出是:

old ack_seq is ab2429c6
pls input the seq plus amount:
new ack_seq is ab2429c6
sendto ack packet

:参数无效

顺便说一句:send_acksend_rst函数使用原始套接字发送数据包。 似乎 gets() 函数没有被执行,这是怎么回事?谢谢!

最佳答案

在调用 gets 之前调用 getchar();。现在,您输入两个字符:命令号和换行符。因此,gets 读取一个空行,删除换行符,并将一个空字符串存储在数组中。

正如其他答案中所述,gets 由于存在安全风险而已被弃用,但这与您的问题无关。

关于c - gets() 没有被执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15612699/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com