得到()没有得到excuted [英] gets() doesn't get excuted

查看:154
本文介绍了得到()没有得到excuted的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的源代码codeS,还有下面的代码片段:

in my source codes, there is the following snippet:

    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;
            }   
    }   

当我运行该程序,输出结果是:

when I run the program, the output is:

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

:无效的参数

BTW:在 send_ack send_rst 函数使用原始套接字来发送数据包。
 似乎获得()功能没有得到excuted,有什么不对呢?
谢谢!

BTW: the send_ack, send_rst functions use raw socket to send packets. it seems that the gets() function doesn't get excuted, what is wrong with this? thanks!

推荐答案

呼叫的getchar(); 你打电话之前获得。既然这样,你输入两个字符,命令号和一个换行符。因此,获得读入一个空行,剥离换行,阵列存储在一个空字符串。

Call getchar(); before you call gets. As it stands, you input two characters, the command number and a newline. So gets reads a blank line, strips the newline, and stores an empty string in your array.

正如在其他的答案指出,获得是因为它的安全风险pcated德$ P $,但是这并不涉及您的问题。

As noted in the other answers, gets is deprecated because of its security risk, but this doesn't relate to your problem.

这篇关于得到()没有得到excuted的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆