Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么这段代码会得到';端口已在使用中';错误?_C_Linux_Gdb - Fatal编程技术网

C 为什么这段代码会得到';端口已在使用中';错误?

C 为什么这段代码会得到';端口已在使用中';错误?,c,linux,gdb,C,Linux,Gdb,这是我的代码: int main(int argc, char * argv[]) { if (1 == argc) { // if nothing go in printf("Usage: %s <port>\n", argv[0]); exit(-0); } int port = strtol(argv[1], NULL, 10); //pass if (0 == port) { perror(

这是我的代码:

int main(int argc, char * argv[])
{
    if (1 == argc) {  // if nothing go in 
        printf("Usage: %s <port>\n", argv[0]);
        exit(-0);
    }

    int port = strtol(argv[1], NULL, 10); //pass
    if (0 == port) {
        perror("Invalid port");
        exit(-1);
    }

    struct sigaction sa;
    sa.sa_handler = &handle_sigchld;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
    if (sigaction(SIGCHLD, &sa, 0) == -1) {
        perror("Unable to register sigaction");
        exit(-2);
    }

    int s = socket(AF_INET, SOCK_STREAM, 0);
    if (-1 == s) {
        perror("Unable to create server socket");
        exit(-3);
    }

    int optval = 1;
    if (0 != setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))) {
        perror("Unable to setsockopt");
        exit(-4);
    }

    struct sockaddr_in bind_addr = {
        .sin_family = AF_INET,
        .sin_port = htons(port)
    };

    if (0 != bind(s, (struct sockaddr *) &bind_addr, sizeof(bind_addr))) {
        perror("Unable to bind socket");
        exit(-5);
    }

    if (0 != listen(s, 10)) {
        perror("Unable to listen");
        exit(-6);
    }

    while (1) {
        int s_ = accept(s, NULL, NULL); 

        sleep(BRUTE_FORCE_TIMEOUT); //here is the stop !!!(but after I delete this, it still stops but in the first line of this while loop)

        if (-1 == s_) {
            perror("Unable to accept");
            continue;
        }

        pid_t child_pid = fork();
        if (-1 == child_pid) {
            perror("Unable to fork");
            goto accept_cleanup;
        }

        if (0 == child_pid) {
            close(s);
            handle(s_);
            exit(0);
        }

accept_cleanup:
        close(s_);
    }

    exit(0);
}

void handle(int s)
{
    char inbuf[4096];
    dup2(s, 0);
    dup2(s, 1);
    setbuf(stdout, NULL);
    alarm(ALARM_TIMEOUT_SEC);
    printf("crackme> ");
    if (NULL == fgets(inbuf, sizeof(inbuf), stdin))
    {
        return;
    }
    right_trim(inbuf);
    if(is_correct(inbuf))
    {
        printf("Good job!\n");
    }
}
问题是,当我在端口已经运行的情况下运行它时,我得到一个错误,即端口已经在使用中。 如果我尝试使用端口0运行它,我会得到一个错误,该端口无效:success。 如果我只使用一个随机端口运行它,Linux下降行将不会发生任何事情

当我在gdb中调试它时,它会在
sleep
行上停止,所以我删除了这一行,但它会再次停止,但现在它会在while循环的第一行(
while(1){int s\=…


为什么会发生这种情况?

您是说两种错误情况(a)端口已在使用中,还是(b)端口无效,是因为……它们是错误情况?抱歉,但这没有多大意义。“我的程序在运行时报告错误,因为这些错误情况……”??你知道TCP/IP是如何工作的。对吗?你不能让两个套接字绑定到同一个地址(IP地址/端口对)。在我的程序中,我使用了两个绑定到同一地址的套接字?不,我写了这个软件,我试着理解别人写的这个程序。如果你有任何线索,请举手
./my_file 6000