当它停在我的Linux应用程序的端口已被其他服务 [英] My linux application port is taken by another service when it's stopped

查看:111
本文介绍了当它停在我的Linux应用程序的端口已被其他服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我公司开发,其中包含一个小型的http服务器的应用程序。

I developed an application which contains a small http server.

我的应用程序在开机启动。如果我杀了它(与杀死-9 为例),HTTP服务器端口将直接由另一个守护采取(acsd用来自Broadcom)。

my application is launched in the boot. If I kill it (with kill -9 for example), the http server port will be taken directly by another daemon(acsd from broadcom).

我试过用drop-承担相同的行为,但问题没有重现。如果我杀了下拉式熊则acsd不走它的端口。

I tried the same behavior with drop-bear, but the problem is not reproduced. If I kill drop-bear the acsd does not take its port.

在这里我的服务器的code后:

here after the code of my server:

void http_server_init(void)
{
    struct sockaddr_in server;
    int cr_port;

    for(;;) {
        cr_port = conf.port;
        int i = (DEFAULT_PORT == cr_port)? 1 : 0;
        //Create socket
        cr_socket_desc = socket(AF_INET , SOCK_STREAM , 0);
        if (cr_socket_desc == -1)
        {
            LOG (ERROR,"Could not open server socket, Error no is : %d, Error description is : %s", errno, strerror(errno));
            sleep(1);
            continue;
        }

        /* enable SO_REUSEADDR */
        int reusaddr = 1;
        if (setsockopt(cr_socket_desc, SOL_SOCKET, SO_REUSEADDR, &reusaddr, sizeof(int)) < 0) {
            LOG (WARNING,"setsockopt(SO_REUSEADDR) failed");
        }

        //Prepare the sockaddr_in structure
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = INADDR_ANY;
        for(;;i++) {
            server.sin_port = htons(cr_port);
            //Bind
            if( bind(cr_socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
            {
                //print the error message
                LOG (ERROR,"Could not bind server socket on the port %d, Error no is : %d, Error description is : %s", cr_port, errno, strerror(errno));
                cr_port = DEFAULT_PORT + i;
                LOG (INFO,"Trying to use another port: %d", cr_port);
                continue;
            }
            break;
        }
        break;
    }
    LOG (INFO,"server initiated with the port: %d", cr_port);
}

我在我的http服务器做错了什么?

What I'm doing wrong in my http server?

推荐答案

因为我没有为我的问题的回答。我想与大家分享了这个问题的反应。

since I don't have response for my question. I would like to share with you the response for that problem.

该则acsd服务在端口因为套接字未关闭的选项 SO_REUSEADDR 设置

The acsd service take the port because the socket is not closed and the option SO_REUSEADDR is set

这篇关于当它停在我的Linux应用程序的端口已被其他服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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