问题
I developed an application which contains a small http server.
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).
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.
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);
}
What I'm doing wrong in my http server?
回答1:
As I said in a comment it could be the fact that the OS is still seeing the port busy from your executable because you killed it and it didn't release the resource.
Googling a little bit I've found this question that is similar to what your problem is.
Now, it seems that the problem is not in your code but in how you manage the kill..
Try using kill instead of kill -9 or try catching the sig so that the executable is able to release its resources by itself
回答2:
since I don't have response for my question. I would like to share with you the response for that problem.
The acsd service take the port because the socket is not closed and the option SO_REUSEADDR
is set
来源:https://stackoverflow.com/questions/28237517/my-linux-application-port-is-taken-by-another-service-when-its-stopped