Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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: 
");
                            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
", 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
", ack_seq);
                            printf("pls input the seq plus amount: 
");
                            char amount[6];
                            gets(amount);
                            ack_seq= ack_seq+atoi(amount);
                            printf("new akc is %x
", 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

: Invalid argument

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
234 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...